首页 > 解决方案 > 从另一个文件导入 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[]
}

项目结构:

抱歉,我无法发布 repo,但这是我们的设置。我们正在使用 lerna,所以我们在index.d.ts文件中声明模块,例如declare module "@shared/branch". 看起来 tslint 找不到正确的接口导出所以抱怨。代码按预期编译和运行

标签: javascripttypescriptecmascript-6interfacelerna

解决方案


推荐阅读