Skip to content

Instantly share code, notes, and snippets.

@stephenh
Last active May 18, 2019 15:14
Show Gist options
  • Save stephenh/23e5f01d0e8053b87d4a870589cda2d5 to your computer and use it in GitHub Desktop.
Save stephenh/23e5f01d0e8053b87d4a870589cda2d5 to your computer and use it in GitHub Desktop.
doWhileMounted
class Foo {
a = 1;
b(): void {}
c(): boolean {
return true;
}
}
// https://medium.com/dailyjs/typescript-create-a-condition-based-subset-types-9d902cea5b8c
type FilterFlags<Base, Condition> = {
[Key in keyof Base]: Base[Key] extends Condition ? Key : never
};
type AllowedNames<Base, Condition> = FilterFlags<Base, Condition>[keyof Base];
type SubType<Base, Condition> = Pick<Base, AllowedNames<Base, Condition>>;
type Constructor<T> = new () => T;
function doWhileMounted<P, K extends AllowedNames<P, () => void>>(
Cstr: Constructor<P>,
functionName: K,
): P {
return new Cstr();
}
// doWhileMounted(Foo, 'a'); // TypeError
doWhileMounted(Foo, 'b');
doWhileMounted(Foo, 'c');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment