打字稿:省略'export'关键字似乎使界面全局化?

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

最近我在一个打字稿项目的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的错误。当然这是预期的行为。但是我很快意外地从export中删除了another-interface.ts关键字 - VS代码停止了抱怨并且可以正确地解决该类型。

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

typescript visual-studio-code
1个回答
5
投票

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

这是预期的行为。如果文件中没有exportimport,则它是脚本而不是模块。然后,在脚本中,所有成员都是全局成员。

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

© www.soinside.com 2019 - 2024. All rights reserved.