【问题标题】:How do you export an interface from a namespace?如何从命名空间中导出接口?
【发布时间】:2019-06-29 19:40:37
【问题描述】:

如何从 typescript 中的命名空间导出接口?这仅限于声明文件吗?这是我正在尝试做的一个示例:

namespace Foo {
  export interface Bar {}
  export class Baz {}
}

export const { Baz } = Foo; // Works just fine
export const { Bar } = Foo; // Type 'typeof Foo' has no property 'Bar' and no string index signature.

打字稿 3.3.1

值得注意的是,官方文档中有这个用例,所以当我看到它不起作用时我很困惑:https://www.typescriptlang.org/docs/handbook/namespaces.html

更新(感谢提香):

我的主要目标是导出这种类型,我使用 Titian 的建议解决了这个问题:

namespace Foo {
    export interface Bar {}
    export class Baz {}
}

export const type Bar = Foo.Bar // now exportable

【问题讨论】:

    标签: typescript


    【解决方案1】:

    您正在尝试在预期值的位置使用接口。类既是类型又是值(参见values vs types),这就是它起作用的原因。

    如果您在类型注释中使用接口,它将按预期工作:

    namespace Foo {
        export interface Bar {}
        export class Baz {}
    }
    
    Foo.Baz // Works just fine
    let bar : Foo.Bar // ok
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-26
      • 1970-01-01
      • 2016-06-10
      • 1970-01-01
      • 2021-07-02
      • 2014-11-02
      • 2019-03-18
      • 1970-01-01
      相关资源
      最近更新 更多