在TypeScript中,我以為 "宣告 "會向編譯器提示這是在其他地方創建的。 這兩種看似作業相同的 "型別 "實際上有何不同。是不是因為如果它在其他地方沒有找到它,就會使用當前的型別?
例子:
SomeTypes.ts
export type FooBarType = 'Foo'/span> | 'Bar'/span>;
export declare type FooBarDeclareType = 'Foo' | 'Bar';
兩者都有預期的IDE警告:
Type "This is not foo or Bar" is not assignable to type 'FooBarType'/p>
import SomeTypes.ts
span class="hljs-keyword">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'/span>。
const getFooOrBarDeclareType_bar=()。FooBarDeclareType => 'Bar'/span>;
const getFooOrBarType_foo =()。FooBarType => 'Foo'/span>。
const getFooOrBarDeclareType_foo=()。FooBarDeclareType => 'Foo'/span>。
uj5u.com熱心網友回復:
絕對沒有。declare只是說明你要宣告的成員是在一個環境背景關系中。環境背景關系 "基本上只是意味著它對運行時并不重要,它只是用來打字的。 因此,types 和 interfaces 已經處于環境背景關系中,所以它不會改變任何東西。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/307921.html
標籤:
下一篇:MongoDB32位GUI客戶端
