2

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?

0

2 Answers 2

2

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>;
Sign up to request clarification or add additional context in comments.

Comments

2

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.