gpt4 book ai didi

typescript :遗漏 'export' 关键字似乎使接口(interface)成为全局接口(interface)?

转载 作者:搜寻专家 更新时间:2023-10-30 20:55:57 26 4
gpt4 key购买 nike

最近在 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 代码向我显示了找不到类型 AnotherInterface 的错误。当然,这是预期的行为。 但是一旦我不小心从 another-interface.ts 中删除了 export 关键字 - VS code 停止了提示并且可以正确解析类型。

所以有人知道这里发生了什么吗?这是预期的行为还是 typescript 或 vs 代码的错误?

最佳答案

So do anyone know what's going on here? Is this expected behavior or rather a bug of either typescript or vs code?

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

另请参阅:Classic scripts v/s module scripts in Javascript

关于 typescript :遗漏 'export' 关键字似乎使接口(interface)成为全局接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51174283/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com