【发布时间】:2021-12-08 11:45:05
【问题描述】:
我的项目中有一个可选且依赖于平台的依赖项,我想像这样有条件地导入:
import os from 'os';
export default async function doSomething(): Promise<foo | null> {
if(os.type() === 'some os') {
// "platform-dependent-module" exposes type "foo"
const module = await import("platform-dependent-module");
// do stuff
return bar; // bar is of type foo
}
return null;
}
但是这不起作用,因为编译器Cannot find name 'foo'。是否可以在不必静态导入模块的情况下公开类型定义?
【问题讨论】:
标签: javascript node.js typescript es6-modules dynamic-import