// Tๆฏ็ฑป็ๅฏน่ฑก๏ผkeyof่ฟๅๅฎ็ๅ
ฌๅ
ฑๅฑๆง
type ClassPublicKeys<T> = keyof T
Solution by ouzexi #34301
type ClassPublicKeys<T> = keyof T;
Solution by so11y #27224
I am shocked to my core that this challenge is in the "hard" difficulty set. It should literally be in the "easy" difficulty set. Oh well. I think this is the shortest answer to any of the type challenges (literally). Prepare for a laugh on this one.
// ============= Test Cases =============
import type { Equal, Expect } from './test-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 A1 = ClassPublicKeys<A>;
type B1 = 'str' | 'getNum';
type C1 = Expect<Equal<A1, B1>>;
// ============= Your Code Here =============
// @zhaoyao91 explanation:
//
// In TypeScript, when you wrote a class A,
// you defined two types in its type system:
//
// 1. `A` is the type of the instance of class `A`
//
// 2. `typeof A` is the type of the class object
// say `class A`
//
// So, any instance of class A is just an object,
// keyof keyword return its public fields.
type ClassPublicKeys<T> = keyof T;
// ============== Alternatives ==============
type ClassPublicKeys<T, P = keyof T> =
P extends keyof T
? P
: never;
For more video solutions to other challenges: see the umbrella list! https://github.com/type-challenges/type-challenges/issues/21338
Solution by dimitropoulos #25353
type ClassPublicKeys<T> = keyof T;
Solution by E-uler #24973
type ClassPublicKeys<T> = keyof T
Solution by NeylonR #24598
// your answers
type ClassPublicKeys<T> = keyof T
Solution by jxhhdx #24337
// your answers
type ClassPublicKeys<A> = keyof A
Solution by snakeUni #23919
type ClassPublicKeys<A> = keyof A
In typescript, when you wrote a class A
, you defined two types in its type system:
A
is the type of the instance of class A
typeof A
is the type of the class object, sayclass A
So, any instance of class A
is just an object, keyof
keyword return its public fields.
Solution by zhaoyao91 #21570
// your answers
//this is i have resolve question that the easiest and the the most ingenious
type ClassPublicKeys<T> = keyof T
Solution by YqxLzx #21448
type ClassPublicKeys<T> = keyof T
Solution by so11y #21250
// your answers
type publicKyes1<A1, k = keyof A1> = k extends keyof A1 ? k : never;
Solution by fengjinlong #20297
type ClassPublicKeys<T> = keyof T;
Solution by CaoXueLiang #19471
// your answers
type ClassPublicKeys<T> = keyof T;
Solution by jiaaoMario #17753
type UnionToFunc
type ClassPublicKeys
Solution by my5201314zwl #16697
// your answers
type ClassPublicKeys<T> = keyof T
Solution by humandetail #16486
// your answers
type ClassPublicKeys<T> = keyof T ;
Solution by justBadProgrammer #15913
type ClassPublicKeys<T extends any> = keyof T;
Solution by sromic #15855
// your answers
type ClassPublicKeys<A> = keyof A
Solution by liuxing95 #13120
type ClassPublicKeys<TClass> = keyof TClass
Solution by michaltarasiuk #12408
// your answers
type ClassPublicKeys<T> = keyof T;
Solution by SeptEarlyMorning #12164
type ClassPublicKeys<T extends Record<string, any>, K extends keyof T = keyof T> = K
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()
}
}
// public
type testPublic = 'str' extends keyof A ? true : false
// protected
type testProtected = 'num' extends keyof A ? true : false
// private
type testPrivate = 'bool' extends keyof A ? true : false
type testClass = ClassPublicKeys<A>
Solution by kongmingLatern #11351
class A {
public str: string
public greetings: string
protected num: number
private bool: boolean
constructor() {
this.str = 'naive'
this.greetings = 'hello'
this.num = 19260917
this.bool = true
}
getNum() {
return Math.random()
}
}
type ClassPublicKeys<T, K = keyof T> = K extends keyof T
? K
: never
type cases = [Expect<Equal<ClassPublicKeys<A>, 'str' | 'greetings' | 'getNum'>>]
Solution by adnasa #10697
type ClassPublicKeys<T, K = keyof T> = K extends keyof T ? K : never
Solution by ProsperBao #10643
type ClassPublicKeys<A> = keyof A; // ๐
Solution by XkSuperCool #10005
type ClassPublicKeys<T extends any,K = keyof T> = K extends keyof T? T extends Pick<T,K>?K:never : never;
Solution by jiangshanmeta #2846
// :D
type ClassPublicKeys<A> = keyof A
Solution by hauseyo #2843