Last active
May 18, 2019 15:14
-
-
Save stephenh/23e5f01d0e8053b87d4a870589cda2d5 to your computer and use it in GitHub Desktop.
doWhileMounted
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
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