Typescript: Inference of generic type

Created on 3 Apr 2017  ·  2Comments  ·  Source: microsoft/TypeScript

Currently a generic class such as

class A<B extends C<D>, D>

Must have both B and D passed when instantiated, but it would be nice if the type system could infer the type D from type B. Because if I pass in type B which extends type C, then type D is known from C. Why do I have to pass type D separately as well?

Or am I approaching this the wrong way? I'm just trying to avoid us having to unnecessarily import interfaces all over the place!

Question

Most helpful comment

In the nightly build there is some nice new syntactic sugar around default type arguments that allows you to omit type them from consumption sites in a number of places as well as pass down defaults which can, optionally, be themselves derived from other type arguments.

That said, I think you are after higher-kinded types, or perhaps existential types.

All 2 comments

Say that I define a generic class as such

class A<B<C>>

Is it therefore possible in any form or fashion to be able to extract type C from generic type B? Or is this impossible?

It would be incredibly, immensely useful to be able to extract the type of a generic type, especially when type mapping (another issue that I have opened).

In the nightly build there is some nice new syntactic sugar around default type arguments that allows you to omit type them from consumption sites in a number of places as well as pass down defaults which can, optionally, be themselves derived from other type arguments.

That said, I think you are after higher-kinded types, or perhaps existential types.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antony-Jones picture Antony-Jones  ·  3Comments

kyasbal-1994 picture kyasbal-1994  ·  3Comments

blendsdk picture blendsdk  ·  3Comments

uber5001 picture uber5001  ·  3Comments

fwanicka picture fwanicka  ·  3Comments