我可以轻松地使用多个值作为导出默认值:
class Car {...}
class Bus {...}
export default { Car, Bus }
我也可以轻松拥有类型的导出默认值
export default interface Airplane {...}
但是我不能默认导出多种类型
interface Airplane {...}
interface Motorcycle {...}
// 'Airplane' only refers to a type, but is being used as a value here. ts(2693)
export default { Airplane, Motorcycle }
或多种类型和值的混合。
class Car {...}
interface Motorcycle {...}
// 'Airplane' only refers to a type, but is being used as a value here. ts(2693)
export default { Car, Airplane }
我该如何实现?
答案 0 :(得分:2)
考虑export =
语法。
export =
语法指定从模块导出的单个对象。这可以是类,接口,名称空间,函数或枚举。
Vehicle.ts
namespace Vehicle {
export class Car {
// ...
}
export class Bus {
// ...
}
}
export = Vehicle;
Test1.ts
import Vehicle from './Vehicle';
const car = new Vehicle.Car();
const bus = new Vehicle.Bus();
Test2.ts
import { Bus, Car } from './Vehicle';
const car = new Car();
const bus = new Bus();
请参见https://www.typescriptlang.org/docs/handbook/modules.html#export--and-import--require
答案 1 :(得分:0)
实际上,通过这样导出:
class Car {...}
interface Motorcycle {...}
// 'Airplane' only refers to a type, but is being used as a value here. ts(2693)
export default { Car, Airplane }
您要做的是**导出具有两个属性的默认对象*。*
稍后,您可以在代码中将其导入,如下所示:
// name is as you like, its default import
import transport from transport;
并像这样使用它:
....new transport.Car()
....new transport.Plane()