区别1:
type 可以为基本类型,联合类型 或 元组 甚至any等等 赋值定义别名,interface 明显办不到
type A = string;
type B = string | unknown;
type C = B | [ 1, 2 ,3 ,4]; // 赋值当变量用
let test: C = '';
区别2:
interface 定义重名了会合并属性,type 办不到(会报错提醒 重复定义) // interface 定义重名了会合并属性,很多库ts源码里面都用到过类似方法作为扩展
interface A {
name: string;
}
interface A {
age: number;
}
const aObj: A = {
name: '', // 必须
age: 233 // 必须
};