你如何输出从命名空间的界面?

问题描述 投票:1回答:1

你如何从打字稿命名空间导出接口?这是仅限于申报文件?这里是什么,我试图做一个例子:

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

更新(感谢提香):

我的主要目标是出口这种类型,我解决了这个用提香的建议是:

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

export const type Bar = Foo.Bar // now exportable
typescript
1个回答
1
投票

您正在尝试使用该接口在一个预期值,一个位置。类是两种类型的和值(见values vs types),这就是为什么它的工作原理。

如果你在一个类型标注使用的接口会按预期工作:

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

Foo.Baz // Works just fine
let bar : Foo.Bar // ok
© www.soinside.com 2019 - 2024. All rights reserved.