Reputation: 2171
TS introduced default parameters types for generics in v2.3 Generic parameter defaults
So I can define a generic type as follows:
interface MyGenericType<T = any, U = any, V = any, W = any> { }
And then I can use it as follows:
declare const xyz: MyGenericType<any, any, any, Date>;
I am trying to get rid of these default types and only specify the ones I want to define by names as follows?
declare const xyz: MyGenericType<{ W: Date }>;
However that did not work as expected because { W: Date }
is replacing T
the first param not replacing W
.
Is that thing not doable in Typescript?
Is there any workarounds other than defining any
in the generic type declaration?
Upvotes: 2
Views: 251
Reputation: 20043
Partially inferring generic type arguments is not currently possible. The discussion for this feature can be found here:
https://github.com/microsoft/TypeScript/issues/26242
In there you'll also find the link to a now closed issue that discussed referring to the type arguments by name (as in your example) which was closed because this is a rather big change as it would make the name of the type argument external to the type itself, which has rather big implications.
Upvotes: 2
Reputation: 2182
This is a work around. You can create another interface and use it like this.
interface MyGenericType<T = any, U = any, V = any, W = any> { }
interface MyExtendedGenericType<W> extends MyGenericType<any, any, any, W> { }
declare const xyz: MyExtendedGenericType<Date>;
Upvotes: 1