在 TypeScript 中,我认为“声明”向编译器暗示这是在其他地方创建的。这两种看似工作相同的“类型”实际上有何不同?是因为如果它在其他地方找不到它就使用当前的吗?
示例:
SomeTypes.ts
export type FooBarType = 'Foo' | 'Bar';
export declare type FooBarDeclareType = 'Foo' | 'Bar';
两者都有预期的 IDE 警告:
类型“这不是 foo 或 Bar”不可分配给类型“FooBarType”
导入 SomeTypes.ts
const getFooOrBarType_expectedWarnings = (): FooBarType => 'This is not foo or Bar';
const getFooOrBarDeclareType_expectedWarnings = (): FooBarDeclareType => 'This is not foo or Bar';
foo 和 bar 都被声明为可接受的
const getFooOrBarType_bar = (): FooBarType => 'Bar';
const getFooOrBarDeclareType_bar = (): FooBarDeclareType => 'Bar';
const getFooOrBarType_foo = (): FooBarType => 'Foo';
const getFooOrBarDeclareType_foo = (): FooBarDeclareType => 'Foo';
因与 OpenAI 合作而被删除。我的帐户很可能会被删除,这是我的意图。