Type
1 | type Point = { |
Interface
1 | interface Point { |
Interface都可允许拓展(extends)
Interface extends Type
1 | type Person{ |
Interface extends Interface
1 | interface Person{ |
type extends type
1 | type Person{ |
type extends Interface
1 | interface Person{ |
實現 implements
類別可以實作interface以及type(除聯合類型外)
1 | interface ICat{ |
type 無法對聯合類型Person進行實現
無法對聯合類型Person進行實現
error: A class can only implement an object type or intersection of object types with statically known members.
1 | type Person = { name: string; } | { setName(name:string): void }; |
type 可以宣告基本型別別名,聯合型別類型,元組等型別,而 interface 不行
type可以宣告基本型別別名,聯合型別類型,元組等型別
1 | // 基本類型别名 |
type 語句中還可以使用 typeof 獲取實例的 類型進行赋值
1 | // 當你想獲取一個變量的類型時,使用 typeof |
1 | type StringOrNumber = string | number; |
interface 可以聲明合併而 type 不行
interface 能够聲明合併
1 | interface User { |