Skip to content

Instantly share code, notes, and snippets.

@maxnorth
Created April 17, 2019 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maxnorth/41385a4adbb1d17170e14a67c40afcb5 to your computer and use it in GitHub Desktop.
Save maxnorth/41385a4adbb1d17170e14a67c40afcb5 to your computer and use it in GitHub Desktop.
TypeScript Keys Helper
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