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
{{ message }}
Repository navigation
Add type operators that can answer "given function type T, what is the type of its return value when arguments P are passed in?" #40179
This is a proposal that would help with overloaded function issues like #26591 and would help my type testing library be able to support checks like expectTypeOf(fn).toBeCallableWith(args).
Note that though ValueForKey could be implemented as:
typeValueForKey<T,KextendskeyofT>=Textends{[KeyinK]: infer U} ? U : never;
The following will only work for non-overloaded functions:
typeValueForArguments<TextendsCallable,PextendsParameters<T>>=Textends(...args: P)=> infer R ? R : never;
since both the inference from Parameters<T> and the type definition will only consider one of the overloads.
What I'm asking is not for inference to consider multiple overloads, which would be nice but might complexify the implementation of type inference, but direct support for paramsof and T(A1, A2, ...A) type syntax which behaves correctly with overloaded functions.
Note that though ValueForKey could be implemented as:
typeValueForKey<T,KextendskeyofT>=Textends{[KeyinK]: infer U} ? U : never;
I have to rescind that statement as I found an example that doesn't currently work.
typeTest={x: boolean;[K: string]: number|boolean;}letboolean: ValueForKey<Test,'x'>=true;// @ts-expect-errorboolean=42// Type 'boolean' is not assignable to type 'never'.(2322)letshouldBeNumberOrBoolean: ValueForKey<Test,'y'>=true^^^^^^^^^^^^^^^^^^^^^^^// Type 'number' is not assignable to type 'never'.(2322)shouldBeNumberOrBoolean=42;^^^^^^^^^^^^^^^^^^^^^^^letnumberOrBoolean: Test['y']=true;numberOrBoolean=42;
The implication being that T[...] syntax provides unique semantics that cannot be produced in other ways.
functionusesOverloadedFunction<T>(arg: T)// : inferred return type is whatever the last overload of// `overloadedFunction` is when applied to `...args: [T]`. {returnoverloadedFunction(arg);}
Would love to see a solution to this. this is one of the reasons why i don't like to work with the standard NodeJS EventEmitter. these often easily have 10+ overloads for all the eventnames which were all manually typed. That's great when you are trying to harcode an event, but any kind of elegant type inference impossible at the moment.
Specific example are events.on and events.once they are two to-level function for interacting with emitters easily via promises, eg.
event.once(emitter,name[,options])// => return a Promiseevent.on(emitter,name[,options])// => return an AsyncIterable
Both of them are great when you are using a lot of async/await, it saves all the annoying callback nesting when you have to wait for some init/close-event like the net.Serverlistening and close. it results in some realy nice, linear code.
but typescript can't give you any completion help for any events that are present on any emitter, which really makes these two function cumbersome to use if your not very familiar with the emitter
server.listen(8124)awaitonce(server,'listening')
server.close()awaitonce(server,'close')
A solution to this migth also be a stepping stone for problems related to generic functions, smarter ReturnType and bind(...args) operations. I would love to finally be able to use Currying accurately with generics.
Activity
lazytype commentedon Aug 21, 2020
I have to rescind that statement as I found an example that doesn't currently work.
The implication being that
T[...]syntax provides unique semantics that cannot be produced in other ways.HitalloExiled commentedon Sep 6, 2020
That was as close as I got to a solution using the current state.
It works in most cases.
But has several problems in overloads with optional parameters.
lazytype commentedon Sep 20, 2020
Here's another use-case:
Actual:
Expected:
KilianKilmister commentedon Oct 26, 2020
Would love to see a solution to this. this is one of the reasons why i don't like to work with the standard NodeJS EventEmitter. these often easily have 10+ overloads for all the eventnames which were all manually typed. That's great when you are trying to harcode an event, but any kind of elegant type inference impossible at the moment.
Specific example are
events.onandevents.oncethey are two to-level function for interacting with emitters easily via promises, eg.Both of them are great when you are using a lot of
async/await, it saves all the annoying callback nesting when you have to wait for some init/close-event like thenet.Serverlisteningandclose. it results in some realy nice, linear code.but typescript can't give you any completion help for any events that are present on any emitter, which really makes these two function cumbersome to use if your not very familiar with the emitter
A solution to this migth also be a stepping stone for problems related to generic functions, smarter
ReturnTypeandbind(...args)operations. I would love to finally be able to use Currying accurately with generics.jcalz commentedon Oct 28, 2020
duplicate of #26043 but that one was closed as a duplicate of #6606 which was ultimately declined; not sure about this one
jcalz commentedon Dec 5, 2022
Cross-linking #17961
Autumn-one commentedon Jul 13, 2023
From the user's point of view, this function is very useful, I hope the official can be more from the user's point of view
jcalz commentedon Aug 20, 2023
Cross-linking to #52035 (comment) and specifically the term "call types"