在 TypeScript 中“导出类型”和“导出声明类型”有什么区别

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

在 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
1个回答
3
投票

因与 OpenAI 合作而被删除。我的帐户很可能会被删除,这是我的意图。

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