《type 和 interface 的区别》

59 阅读1分钟

image.png

1.组合方式

interface A {
    a: string
}
interface B extends A {
    b:string
}
const b: B = {
    a: 'hi',
    b: 'hi'
}

type C = {
    c: string
}
type D = {
    d: sting
} & C
const d: D = {
    c: 'hello1',
    d: 'hello2'
}

2.扩展方式

interface A {
    a: string
}
interface A {
    b: string
}
const a: A = {
    a: 'hi',
    b: 'hi'
}

type C = {
    c: string
}
type C = {
    //报错
    d: string
}

3.命名方式

type X = number 
const x: X = 1
type Y = typeof x   //  Y === number 而不是 Y === X

4.范围不同

type C = string
// interface 不行