【问题标题】:In TypeScript what is the difference between 'export type' and 'export declare type'在 TypeScript 中,“导出类型”和“导出声明类型”有什么区别
【发布时间】: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


    【解决方案1】:

    绝对没有。 declare 只是声明您声明的成员处于环境上下文中。 “环境上下文”基本上只是意味着它与运行时无关,它只是用于打字。因此,types 和 interfaces 已经在环境上下文中,所以它不会改变任何东西。

    【讨论】:

      猜你喜欢
      • 2020-11-29
      • 1970-01-01
      • 2017-10-20
      • 1970-01-01
      • 2018-12-10
      • 2012-12-30
      • 2016-03-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多