TS-1类型声明

2023-03-08  本文已影响0人  RickyWu585
  1. Object 范围太广
  2. object :Object除去基本类型之外的类型
  3. 索引签名
  4. Record
  5. Class or Constructor
type A = {
[k:string] : number
}
type Record<K extends number | string | symbol, T> = {
  [k in K] : T
}

const person: Record<string,number> = {
  name: 'frank',
  age: 12
} 
type Person = {
  username: string
  age: number
  sayHi: FnWithThis
}

type FnWithThis = (this:Person,name) => void

const sayHi:FnWithThis = function(){
  console.log(this.username)
}

const person:Person = {
  username: 'frank',
  age: 12,
  sayHi: sayHi
}

// 调用必须指定this
person.sayHi()  // 'frank'
or
sayHi.call(person) // 'frank'
type A = string | number | boolean

let a:A 

if(typeof a === 'string'){
  a.indexOf
}else if(typeof a === "number"){
 a.toFixed
}else if(typeof a === "boolean"){
  a.valueOf
}else{
  a // type is never
}
上一篇下一篇

猜你喜欢

热点阅读