[Typescript] 117. Hard - ClassPublicKeys

Implement the generic ClassPublicKeys<T> which returns all public keys of a class.

For example:

class A {
  public str: string
  protected num: number
  private bool: boolean
  getNum() {
    return Math.random()
  }
}

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

 

/* _____________ Your Code Here _____________ */

type ClassPublicKeys<C> = keyof C


/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'

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 cases = [
  Expect<Equal<ClassPublicKeys<A>, 'str' | 'getNum'>>,
]

 

posted @ 2022-11-26 16:14  Zhentiw  阅读(16)  评论(0)    收藏  举报