yunliuyan / type-challenges

typescript-challenges
0 stars 2 forks source link

00030-hard-classpublickeys #30

Open yunliuyan opened 11 months ago

yunliuyan commented 11 months ago

ClassPublicKeys hard #utils

by jiangshan @jiangshanmeta

Take the Challenge

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'

Back Share your Solutions Check out Solutions
yunliuyan commented 11 months ago

完结

这个是给大伙开心的,hhh

代码实现

type ClassPublicKeys<T> = keyof T
Naparte commented 11 months ago

// 实现一个通用Last<T>,它接受一个数组T并返回其最后一个元素的类型。
type ClassPublicKeys<T> = keyof T;

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

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

let a: publicKyes = 'bool'
Janice-Fan commented 10 months ago
type ClassPublicKeys<T> = keyof T;

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