javascript - 从另一个文件在 Typescript 中导入接口
如何从另一个文件导入接口?
如果我使用 type,我可以执行以下操作:
// branch.ts
export type Branch = {
colour: string
}
// tree.ts
import {Branch} from "./branch"
type Tree = {
branch: Branch[]
}
而使用 interface 时,它会抱怨它不是一个命名空间 [ts] Cannot use namespace 'Branch' as a type.:
// branch.ts
export interface Branch {
colour: string
}
// tree.ts
import {Branch} from "./branch"
interface Tree {
branch: Branch[]
}
项目结构:
我不能发布这个仓库,抱歉,但这是我们目前的设置。我们使用 lerna,所以我们在一个 index.d.ts 文件中声明模块,例如 declare module "@shared/branch"。看起来 tslint 无法正确找到接口导出,所以会报错。不过代码可以按预期编译和运行。