【问题标题】:Can I export only part of the interfaces inside .d.ts?我可以只导出 .d.ts 中的部分接口吗?
【发布时间】:2021-08-12 14:00:33
【问题描述】:
// config.d.ts

export interface Config {
    NAV_STRUCTURE: Node[];
}

interface Node {
    title: string;
}

请看上面的例子。在此示例中,我只想导出 Config。但是,我发现我也可以从外部 .ts 导入 Node 吗?有什么办法限制Node的导出?

提前致谢。

【问题讨论】:

  • 我认为默认接口是public?为什么需要私有类型定义?
  • 我有一个非常复杂的类型,我不想暴露所有的接口,因为有些接口仅供内部使用。

标签: javascript typescript import interface export


【解决方案1】:

您可以在模块文件中定义typeinterface?不确定这是否是推荐的做法……但这些类型在该模块之外不可用。

EXAMPLE.TS

type Test = string
interface Person {name:string}

export class Example {
    private p:Person           // <- Person type is allowed here
    private t:Test             // <- Test type is allowed here
}

APP.TS

export class App {
    private p:Person           // <- Person type is NOT allowed here
    private t:Test             // <- Test type is NOT allowed here
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-04-18
    • 2012-05-15
    • 1970-01-01
    • 2017-03-26
    • 2023-03-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多