Created
April 17, 2019 20:22
-
-
Save maxnorth/41385a4adbb1d17170e14a67c40afcb5 to your computer and use it in GitHub Desktop.
TypeScript Keys Helper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type KeysOf<T> = { [x in Exclude<keyof T, keyof KeyHelper<T>>]: null } | |
abstract class KeyHelper<T> { | |
abstract $keys: KeysOf<T>; | |
getKeys(): (keyof T)[] { | |
return Object.keys(this.$keys) as (keyof T)[]; | |
} | |
} | |
class Greeter extends KeyHelper<Greeter> { | |
$keys: KeysOf<Greeter> = { | |
greeting: null, | |
greet: null, | |
who: null, | |
} | |
who: string; | |
greeting: string; | |
greet() { | |
return "Hello, " + this.greeting; | |
} | |
} | |
let greeter = new Greeter(); | |
alert(greeter.getKeys().join(", ")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment