logoProsperBao

ClassPublicKeys

2022-05-25 10

题目来源(type-challenges)

问题

实现 ClassPublicKeys<T> 拿出类中允许访问的属性

class A {
  public str: string
  protected num: number
  private bool: boolean
  constructor() {
    this.str = 'naive'
    this.num = 19260917
    this.bool = true
  }

  getNum() {
    return Math.random()
  }
}

type case1 = ClassPublicKeys<A> // 'str' | 'getNum'

解答

type ClassPublicKeys<T, K = keyof T> = K extends keyof T ? K : never

拆分

  1. keyof T 会过只拿出允许访问的属性