interface
和 type
区别说明
interface
可扩展type
不好扩展,后者是不方便扩展interface
扩展说明
比如说我们现在正在编写一个公共类库,允许程序员来基础上进行扩展,或者是自定义,这个时候就需要使用 interface
interface
一样的类型,里面添加上自己需要的属性就可以了interface Product {
title: string
price: number
isStock: boolean
}
// 扩展 interface 添加一个新的属性数量
interface product {
cont?: number
}
// 使用扩展过后的
let product: Product = {
title: '纯棉秋裤',
price: 200,
isStock: true,
cont: 100
}
type
定义的话type Product = {
title: string
}
// 此时这里就会报错,说不能定义重复的 type
type Procuct = {
price: number
}
总结
在我们日常开发中推荐首选 interface
,因为他可扩展性好