interface

2022-08-25  本文已影响0人  苍老师的眼泪

定义 interface 类型和用 type 定义类型别名是类似的,注意 interface 定义类型不需要等号:

type person = {
    name: string,
    age: number,
    alias?: string,
}

interface people {
    name: string,
    age: number,
    alias?: string,
}

// 向现有结构添加属性

interface people {
    gender: 0 | 1
}

如果是 type 的话,会提示重复定义

接口继承接口

interface Animal {
    gender: 0 | 1
}

interface Colorful {
    color: string
}

// 很显然 ts 支持多重扩展
interface Human extends Animal, Colorful {
    speak: () => void
}

let edison: Human = {
    gender: 1,
    speak() {
        console.log('Are you ok?')
    },
    color: 'blue'
}
上一篇 下一篇

猜你喜欢

热点阅读