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
Whenever we use a recursive generic to alter an array of functions, then use this type as a function's arguments, parameter arrow functions do not have their arguments inferred by TypeScript.
I've been trying to get this properly working to implement a pipe typings (related #30370, #25660). Which lead me to investigate why the arguments in the suggested solution are not inferred. I nailed this down to recursion type modification and made a minimal reproduction (see the code below).
If this is solved the mentioned above issues would also resolve (and probably more).
🔎 Search Terms
recursion, flow, pipe, chain, inference
🕗 Version & Regression Information
The issue is present since version 4.1.5 (playground) when circular references were introduced that made possible to implement recursions. And still preset until 4.8.0-beta (playground).
/** Recursively replaces all items in array T with U */typeRecursiveReplace<T,U>=Textends[any, ...infer Tail]
? [U, ...RecursiveReplace<Tail,U>]
: T;/** Example of recursive replacement */typeExample=RecursiveReplace<[1,2,3],3>// ^? type Example = [3, 3, 3]/** This should transform any function signatures passed to it to `(_: number) => void` */functiontest<Textendsany[]>(..._: RecursiveReplace<T,(_: number)=>void>){}/** This works fine */test((_: number)=>{})/** TypeScript complains about the wrong signature. This is OK too */test((_: string)=>{}// ERROR: Type 'number' is not assignable to type 'string')/** ------------------------------------------ ↓ THE BUG ↓ ------------------------------------------- *//** TypeScript complains about an implicit `any`. However the argument should be inferred as `number`! */test((_)=>{},// ^? (parameter) _: any);/** -------------------------------------------------------------------------------------------------- */
🙁 Actual behavior
Recursive modification of type signatures breaks argument inference in functions.
🙂 Expected behavior
Function parameters should be properly inferred even after type signature was recursively modified.
The text was updated successfully, but these errors were encountered:
If you make the example need generics in the first place, it quickly becomes apparent that you need an unbounded number of inference rounds to type these things properly -- see #30134
Bug Report
Whenever we use a recursive generic to alter an array of functions, then use this type as a function's arguments, parameter arrow functions do not have their arguments inferred by TypeScript.
I've been trying to get this properly working to implement a pipe typings (related #30370, #25660). Which lead me to investigate why the arguments in the suggested solution are not inferred. I nailed this down to recursion type modification and made a minimal reproduction (see the code below).
If this is solved the mentioned above issues would also resolve (and probably more).
🔎 Search Terms
recursion, flow, pipe, chain, inference
🕗 Version & Regression Information
4.1.5
(playground) when circular references were introduced that made possible to implement recursions. And still preset until4.8.0-beta
(playground).💻 Code
Playground link with relevant code
🙁 Actual behavior
Recursive modification of type signatures breaks argument inference in functions.
🙂 Expected behavior
Function parameters should be properly inferred even after type signature was recursively modified.
The text was updated successfully, but these errors were encountered: