Created
January 29, 2018 19:22
-
-
Save chriseppstein/421c8c700622058d1794d281dfb14e55 to your computer and use it in GitHub Desktop.
This is my syntax idea for generating a subset of TypeScript properties for more interesting mapped types.
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 Fn<RV, ARGS = any[]> = (...args: ARGS) => RV; | |
type Maybe<T> = { | |
// * Mapped types would no longer be limited to a single property map expression. But for each key, only the | |
// first expression that matches the key would be used. | |
// * `keyof SOURCE_TYPE extending NARROW_TYPE` is narrowing expression to limit which keys of SOURCE_TYPE | |
// are enumerated. Only keys having a type that can extend the NARROW_TYPE are enumerated. | |
// * The syntax for a mapped property type would be extended to allow a generic type declaration, this allows | |
// the generic to represent the actual concrete type of the key and then be passed along to the type of the | |
// mapped property. | |
// * `unmatched keyof` allows the type to define properties based on any keys not enumerated by another property map. | |
<ARGS>[P in keyof T extending Fn<void, ARGS>]: Fn<void, ARGS>; | |
<RV, ARGS>[P in keyof T extending Fn<RV, ARGS>]: Fn<Maybe<RV>, ARGS>; | |
[P in unmatched keyof T]: Maybe<T[P]>; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
With some boilerplate can get close with conditional types