ts基础

85 阅读1分钟

readonly

type User = {
  name: string,
  age: number
  readonly six: '男'|'女'
}

const jack: User = {
  name: 'jack',
  age: 18,
  six: '男'
}

jack.name = 'tom'   
jack.six = '女'   // 此时会报错, 性别无法修改

in

in 可以遍历枚举类型

type Keys = 'father' | 'mother' | 'son'

type Person = {
    [k in Keys]: string
}

const jack: Person = {
    father: 'tony',
    mother: 'lily',
    son: 'tom'
}

keyof

keyof 可以获取一个对象接口的所有 key 值


interface Person {
    name: string
    age: number
    six: boolean
}

type Keys = keyof Person
// 相当于
// type Keys = 'name' | 'age' | 'six'

interface

interface Girl {
    name : string;
    age  : number;
    bust : number;
    waistline ?: number;
    [propname:string]:any;
}