-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
NoInfer does not work in function parameters #60071
Comments
Sounds like a duplicate of #56311. |
It might be related but that issue is old and does not include |
But |
@jcalz I do not understand what do you mean by:
Clearly generic declare function fn<T>(cb: () => T): T;
fn(() => '123');
// We are getting: function fn<string>(cb: () => string): string Here is this example in a playground. Am I missing something here? |
You are expecting declare function fn<T>(cb: (arg: T) => T): T;
fn(() => '123'); // string
declare function fn2<T>(cb: (data?: NoInfer<T>) => T): T;
fn2(() => '123'); // string TypeScript looks at It looks like you ran into the problem of context-sensitive generic interactions, such as that discussed in #47599, tried to use And there might never be a solution. I think they've declined previous suggestions to have TS determine whether the return type of a function implementation is independent of any of its unannotated parameter types, which is what you'd need here. The easy cases where someone could simply drop a parameter can be done by the user (e.g., write |
Agree with @jcalz on this. Fundamentally there are two cases here:
Despite appearances, there isn't a third case where |
Thanks @jcalz for detailed explanation it actually now makes sense why it's not related to the My use-case is like the first one that @RyanCavanaugh mentioned - I have a function that accepts previous data and creates a new one based on it, something like a reducer, with some minor props access inside. So it's kind of recursive but it makes sense to type the function based on the return type when the type is defined in the return type of the function it calls internally: function makeReducer<T>(fn: (prev?: T) => T): T;
// Here I do not really want to type function parameter but rather infer it from used inner function's return type
makeReducer(prev => processState(prev.state));
// The function typing is defined here
function processState(state?: State): FullState; It seems that the underlying issues is that the inner function here has different input and output types while the function parameter expects both input and output types to be the same - so we cannot simply pass fully typed function as a parameter but need to define intermediate function which translates from out input type to another, but it currently requires us to specify full types of that intermediate function even though it's in theory possible to infer it's signature as we have all the necessary information. I'm wondering if this use-case would be considered in the #47599 which you mentioned above or should I maybe create another issue to track it? |
Maybe not #47599; Looks more like #56311 as @MartinJohns mentioned (and thus #49618). I don't know if your case is distinct enough from those to open a new issue, but it looks like everyone in those threads are saying it's probably too complex for the case where it actually matters, like |
Thanks for clarification, I will close this issue. |
The provided example // Here I do not really want to type function parameter but rather infer it from used inner function's return type
makeReducer(prev => processState(prev.state)); is probably a "further down the rabbit hole" bullet item on #47599 where one of the deferral cases is functions whose return expressions are all calls to un-overloaded non-generic functions. |
π Search Terms
NoInfer does not work in function parameters
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?ts=5.6.2#code/CYUwxgNghgTiAEAzArgOzAFwJYHtVNQB4AVAPgAowAjALnnOCgygH46A5HASVURBhKkAlPAC8peMSF1iAbgBQ8xKnIMmUEePgByAIwAmAMzahCgPRn4XeAHMQGOinTY8BQmgDWqHAHdUFajo1ZjZ4T28-TQlw31RpMNQvWPkLK3gfKFQHJDRMXHxlQgBnDBgsVBsA2npGELoSsoqo+Abym3jWivkgA
π» Code
In some cases I need to infer generic only from the function return but still use it to type the same function parameter:
π Actual behavior
I get the type:
function fn<unknown>(cb: (data?: unknown) => unknown): unknown
π Expected behavior
I expect the type:
function fn<string>(cb: (data?: string) => string): string
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: