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
When a generic function expresses its return type in terms of an argument constraint, the originally given argument type should be preserved/passed-through when the function is called.
Example
interfaceHasId{id: string;}interfaceSpecialArray<D>{getSpecialItem(): D;}functiongo<TextendsArray<D>,D>(x: T): SpecialArray<D>{returnxasany;}constv: Array<HasId>=[{id: 'one'}];// We expect a return type of `SpecialArray<HasId>` but instead we// get `SpecialArray<{}>`.constw=go(v);
My primary use-case for this has been compositional mixins. I want to be able to take some class with a type like HasItems<SomeItem> and augment it with a mixin to add a behavior HasActiveItem<D>, where the mixin has recognizes the SomeItem passed in and propagates it through to satisfy subsequent mixin calls.
This can be done today by omitting one of the generics from the function signature and duplicating those bits of typing in both the parameters and the return type; the remaining generic only captures the "Item" part of the signature.
Checklist
This wouldn't be a breaking change in existing TypeScript/JavaScript code
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.)
Typescript will not infer type parameters if they are not used in the parameters of the function. Other type parameters are not (as far as I know) an inference site for type parameters.
There are several ways of getting the code above to work. You could just use D and remove T:
Or use T and use a conditional type to extract the item type:
functiongo<Textendsany[]>(x: T): SpecialArray<TextendsArray<infer D> ? D : never>{returnxasany;}constv: Array<HasId>=[{id: 'one'}];constw=go(v);//SpecialArray<HasId>
While it would be nice if your code would work as is, since there are already ways to achieve the same thing I'm not sure the extra complexity to infer one type parameter from the other is justified.
Without use cases that require new functionality, there's not really a suggestion here. Generic functions are already inferred in many places where appropriate, but the function described in OP doesn't need that.
Search Terms
generic, propagate, multiple, preserve, argument, parameter, pass-through
Suggestion
When a generic function expresses its return type in terms of an argument constraint, the originally given argument type should be preserved/passed-through when the function is called.
Example
(playground)
Use Cases
My primary use-case for this has been compositional mixins. I want to be able to take some class with a type like
HasItems<SomeItem>
and augment it with a mixin to add a behaviorHasActiveItem<D>
, where the mixin has recognizes theSomeItem
passed in and propagates it through to satisfy subsequent mixin calls.This can be done today by omitting one of the generics from the function signature and duplicating those bits of typing in both the parameters and the return type; the remaining generic only captures the "Item" part of the signature.
Checklist
The text was updated successfully, but these errors were encountered: