-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make the type-level typeof
aware of generic arguments
#28931
Comments
I"d expect this to compile: type T<V> = typeof valueProducer<V> // TS2304: Cannot find name "V".
// or
type T<V> = (typeof valueProducer)<V> and I"d expect |
typeof
aware of generic argumentstypeof
aware of generic arguments
typeof
aware of generic argumentstypeof
aware of generic arguments
Or, I"d expect: type T = typeof valueProducer // Error - Type `T` is not generic
type T<A> = typeof valueProducer // Compiles fine, `A` is bound to `V` in `valueProducer` |
The two types are actually different. The way you have declared |
@calebsander Exactly, I propose to actually make the type-level Otherwise there"s an inconsistency in the language. You are saying that
In other words, its a "normal" type, not a generic. Yet it is possible to provide a type argument for the Why is it so, if the type of |
I think this is related: #17574 |
@jack-williams it is, yes, though the scope of the latter is broader than this ticket. Using the terminology from #17574, it can be formulated as: function id<V> (v : V) { return v }
type Id2<V> = (v: V) => V
type Id3 = <V>(v: V) => V
type TypeOfId = typeof id Which one of the Id2 and Id3 should be returned by |
The type of
type Id<V> = (v: V) => V
const f: Id<boolean> = (x: boolean) => !x; // not parametric this does not mean that f is, or ever was, parametric in the type of its argument. However the following f is, hence why I can instantiate it freely. type Id<V> = (v: V) => V
const f: Id<boolean> = <X>(x: X) => x; // parametric Following your suggestion to return type NumberPicker = (picker: <X>(x: X, y: X) => X, x: number) => number
const constantPicker1 = <X>(x: X, y: X) => x;
const constantPicker2 = <X>(x: X, y: X) => y;
declare const x: typeof constantPicker1;
declare const y: typeof constantPicker2;
const pick: NumberPicker = (picker, x) => picker(x, -x);
// ok as x and y are parametric
pick(x, 3);
pick(y, 4); I think what you really want is to separate type application from function application, so it can be done partially, and then have that syntax in type queries. Like: type T<V> = (typeof valueProducer<V>) |
This will work for my purposes, yes! Any chance for this to be implemented? |
This issue has been marked "Working as Intended" and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
@jack-williams FYI, I"ve filed the feature request for solution you suggested: #29043 It seems to be small, non-breaking and very useful feature, hope to see it implemented. |
The
typeof
on type-level is not aware of the generic type arguments. Consider:Intuitively,
valueProducer
has type ofValueProducer
:valueProducer : ValueProducer
, however, when we do:we get this type back instead
The latter type is not generic, as there"s no "free" type variables in it.
In other words, the
valueProducer<V>
is an "open" generic type, that can be specialized by a single type argument. But thetypeof valueProducer
is a "closed" generic type, that can not be specialized.The text was updated successfully, but these errors were encountered: