Typescript泛型使用

// 使用泛型之前
class DataManager {
  constructor(private data: string[] | number[]) {}
  getItem(index: number) {
    return this.data[index]
  }
}

// 使用泛型
class DataManager2 {
  constructor(private data: T[]) {}
  getItem(index: number): T {
    return this.data[index]
  }
}

interface Item {
  name: string
  gender?: number
}

// 泛型继承
class DataManager3 {
  constructor(private data: T[]) {}
  getItem(index: number): string {
    return this.data[index].name
  }
}

// 泛型类型约束
class DataManager4 {
  constructor(private data: T[]) {}
  getItem(index: number): T {
    return this.data[index]
  }
}

// 匿名函数泛型【如何使用泛型作为一个具体的类型注解】
function hello(param: T): T {
  return param
}
const fn: () => string = () => '11'
const fn2: (param: T) => T = hello
const fn3: () => string = () => {
  return '123456'
}

const data = new DataManager(['111'])
console.log(data.getItem(0))
const data2 = new DataManager2(['222'])
console.log(data2.getItem(0))
const data3 = new DataManager3([
  {
    name: '豹子头-林冲'
  }
])
console.log(data3.getItem(0))
const data4 = new DataManager4(['444'])
console.log(data4.getItem(0))

你可能感兴趣的:(typescript)