You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[feature request] not sure how to word this: "infer generic arg type/constraint in wrapping type" #29469
Sometimes we want to wrap an existing type, where the existing type has generic type args that have constraints using extends.
Use Cases
For example, this is material-ui's WithStyles type:
and I can wrap it like this to make one of it's properties optional:
// omit a set of keys from a typetypeOmit<T,KextendskeyofT>=Pick<T,Exclude<keyofT,K>>// like Partial, but choose which keys are optional instead of making all of them optionaltypeOptionalize<T,KextendskeyofT>=Omit<T,K>&Partial<Pick<T,K>>// my wrapper type:exporttypeWithStyles_OptionalClasses<Styleextendsstring|StyleRules|StyleRulesCallback=string,IncludeThemeextendsboolean|undefined=false>=Optionalize<WithStyles<Style,IncludeTheme>,'classes'>
Examples
But it'd be nice if we could just pass along the args, not having to re-define them. Maybe it would look like the following, and achieve the same thing:
and it would take advantage of the fact that WithStyles already has the constraint, so if we pass something into WithStyles_OptionalClasses that doesn't meet the constraint defined by WithStyles, then it could throw an error just like when using WithStyles by itself.
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code not sure
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
This'd require flowing the constraints backwards from usages, which isn't exactly something we do right now - however I do understand just how verbose it is to keep repeating yourself when you start nesting generic aliases like this.
Activity
trusktr commentedon Jan 17, 2019
Could the infer keyword be used for this? Maybe
?
weswigham commentedon Jan 18, 2019
This'd require flowing the constraints backwards from usages, which isn't exactly something we do right now - however I do understand just how verbose it is to keep repeating yourself when you start nesting generic aliases like this.