元组
元组,可以理解为:已知元素数量和类型的数组
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| const xjj:(string | number)[] = ['a',22,'b']
const xjj1: [string, number, number] = ['a', 22, 33]
const xjj2: [string, number, number][] = [ ['a', 22, 33], ['a', 22, 33] ]
|
接口
接口,可以理解为对象属性的类型描述。和类型别名
类似,不同的是 接口必须是一个对象,而别名可以直接是一个类型,如 type Girl = string
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| interface Girl { readonly name: string; age: number; waistline?: number; [propname: string]: any; say(): string; }
interface Teacher extends Girl { teach(): string; }
const girl = { name: '大脚', age: 18, sex: '女', say() { return '欢迎光临' }, teach() { return '教' } }
const screenResume = ({ name, age, bust, sex }: Girl) => { console.log(name, age, bust, sex) } const getResume = ({ name, age, bust, teach}: Teacher) => { teach(); console.log(name, age, bust) } screenResume(girl) getResume(girl)
class xiaojjj implements Girl { name = "xiaojj" age = 18 bust = 98 sex = '女' say() { return '欢迎光临' } }
|
可索引的类型
1 2 3 4 5 6
| interface Arr { [index: number]:string }
const myArr:Arr = ['1','2','3'] const myStr:string = myArr[1]
|