Type & Interface
在 TypeScript 中,interface 和 type 都可以用来定义对象的结构,但它们之间有一些重要的区别和使用场景。以下是对两者的详细比较:
I. interface 和 type 的相同点
interface 和 type 的相同点// 使用 interface
interface User {
id: number;
name: string;
}
// 使用 type
type User = {
id: number;
name: string;
};II. interface和type的区别
interface和type的区别区别 1:扩展方式不同
区别 2:interface 支持声明合并,type 不支持
interface 支持声明合并,type 不支持区别3:type可以定义更复杂的类型
type可以定义更复杂的类型区别4:类型别名type 可以用于基本类型和元组
type 可以用于基本类型和元组区别 5:interface 可以被类实现,type 不行
interface 可以被类实现,type 不行总结:interface vs type
interface vs type特性
interface
type
何时使用interface 和 type
interface 和 type使用场景
推荐使用
💡总结建议:
最后更新于