Skip to content

Presence of a binding pattern changes inference for callback function parameters #43605

Description

@yifanwww

Bug Report

🕗 Version & Regression Information

This bugs occurs in typescript 4.2.3, 4.2.4 and 4.3.0-beta, I didn't test the older typescripts.

⏯ Playground Link

Playground link with relevant code

💻 Code

// type definitions simplified from `@reduxjs/toolkit`, `react` and `react-redux`typeDispatch<A={type: any;[extraProps: string]: any}>={<TextendsA>(action: T): T};declarefunctionuseMemo<T>(factory: ()=>T,deps: ReadonlyArray<any>|undefined): T;declarefunctionuseDispatch<TDispatch=Dispatch<any>>(): TDispatch;// IDestructuring & DestructuringtypeIFuncs={readonly[key: string]: (...p: any)=>void};typeIDestructuring<TextendsIFuncs>={readonly[keyinkeyofT]?: (...p: Parameters<T[key]>)=>void};typeDestructuring<TextendsIFuncs,UextendsIDestructuring<T>>=(dispatch: Dispatch<any>,funcs: T)=>U;// functionsconstfuncs1={funcA: (a: boolean): void=>{},funcB: (b: string,bb: string): void=>{},funcC: (c: number,cc: number,ccc: boolean): void=>{},};constfuncs2={funcD: (d: boolean): void=>{},funcE: (e: string,ee: string): void=>{},funcF: (f: number,ff: number,fff: boolean): void=>{},};typeTFuncs1=typeoffuncs1;typeTFuncs2=typeoffuncs2;// react hooksfunctionuseReduxDispatch1<TextendsIDestructuring<TFuncs1>>(destructuring: Destructuring<TFuncs1,T>): T{returnuseMemo(()=>({ ...destructuring(useDispatch(),funcs1)}),[]);}functionuseReduxDispatch2<T1extendsIDestructuring<TFuncs1>,T2extendsIDestructuring<TFuncs2>>(destructuring1: Destructuring<TFuncs1,T1>,destructuring2: Destructuring<TFuncs2,T2>,): T1&T2{returnuseMemo(()=>({ ...destructuring1(useDispatch(),funcs1), ...destructuring2(useDispatch(),funcs2)}),[]);}// test examplesuseReduxDispatch1((d,f)=>({funcA: (...p)=>d(f.funcA(...p)),funcC: (...p)=>d(f.funcC(...p))}));const_=useReduxDispatch1((d,f)=>({funcA: (...p)=>d(f.funcA(...p)),funcB: (...p)=>d(f.funcB(...p))}));const{ funcA, funcB, funcC }=useReduxDispatch1((d,f)=>({funcA: (...p)=>d(f.funcB(...p)),funcB: (...p)=>d(f.funcB(...p)),funcC: (...p)=>d(f.funcC(...p))}));useReduxDispatch2((d,f)=>({funcB: (...p)=>d(f.funcB(...p)),funcC: (...p)=>d(f.funcC(...p))}),(d,f)=>({funcD: (...p)=>d(f.funcD(...p)),funcF: (...p)=>d(f.funcF(...p))}),);const{funcA: _funcA,funcC: _funcC, funcE, funcF }=useReduxDispatch2((d,f)=>({funcA: (...p)=>d(f.funcA(...p)),funcC: (...p)=>d(f.funcC(...p))}),(d,f)=>({funcE: (...p)=>d(f.funcE(...p)),funcF: (...p)=>d(f.funcF(...p))}),);

🙁 Actual behavior

In the function call on line 55, typescript cannot infer the parameters of array functions funcA, funcB and funcC, actually it thinks the parameters ...args is any[].

But this bugs doesn't occur in function calls on line 46, 50, 58 and 63.

🙂 Expected behavior

No compilation errors.

Typescript should infer the parameters ...args of array function funcA, funcB and funcC correctly.

Metadata

Metadata

Assignees

Labels

BugA bug in TypeScriptFix AvailableA PR has been opened for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions