【发布时间】:2021-09-15 23:27:02
【问题描述】:
在 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';
【问题讨论】:
标签: typescript types declare