使用React和TypeScript,對type
interface
具有更大的靈活性,充當類型的類型
type
一個關鍵優勢:
type Pessoa = { nome: string; idade: number; };
type
,反過來,它是定義對象結構並通過interface
支持繼承的理想選擇,從而促進代碼重複使用。 定義對象的合同
type Status = "sucesso" | "erro" | "carregando"; interface Status = "sucesso" | "erro" | "carregando"; // ❌ Erro
2。繼承:interface
extends
interface Usuario { nome: string; idade: number; }
使用相交(
)結合類型
interface
extends
interface Funcionario extends Pessoa { email: string; }
使用對象時,需要擴展性
當您需要類型的類型,相交,功能或API的類型時,請使用type
&
簡而言之,
type Funcionario = Pessoa & { email: string; };
感謝您的閱讀! 歡迎評論和互動!
以上是打字稿:類型和接口之間的區別的詳細內容。更多資訊請關注PHP中文網其他相關文章!