使用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中文网其他相关文章!