【问题标题】:Typescript: leaving out 'export' keyword appears to make an interface global?打字稿:省略'export'关键字似乎使界面全局化?
【发布时间】:2018-07-04 12:58:10
【问题描述】:

最近我在一个typescript项目的VSCode中遇到如下情况:

文件:some-interface.ts

// no import statements have been skipped. This is the whole file:
export interface SomeInterface {
    property: AnotherInterface;         
}

和文件:another-interface.ts:

export interface AnotherInterface {
    // ...
}

好的 - 因为some-interface.ts VS 代码中没有 import 语句,所以告诉我找不到类型 AnotherInterface 的错误。当然,这是预期的行为。 但是我不小心从another-interface.ts 中删除了export 关键字 - VS 代码停止抱怨并且可以正确解析类型。

那么有人知道这里发生了什么吗?这是预期的行为还是打字稿或 vs 代码的错误?

【问题讨论】:

    标签: typescript visual-studio-code


    【解决方案1】:

    那么有人知道这里发生了什么吗?这是预期的行为还是打字稿或 vs 代码的错误?

    这是预期的行为。如果文件中既没有export 也没有import,那么它就是一个脚本 而不是一个模块。然后,在一个脚本中,所有成员都是全局的。

    另见:Classic scripts v/s module scripts in Javascript

    【讨论】:

      猜你喜欢
      • 2022-11-22
      • 1970-01-01
      • 2021-12-04
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      相关资源
      最近更新 更多