Skip to content

TS2345 when generic function calls itself. #41714

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
lae0901 opened this issue Nov 28, 2020 · 1 comment
Closed

TS2345 when generic function calls itself. #41714

lae0901 opened this issue Nov 28, 2020 · 1 comment
Labels
Duplicate An existing issue was already created

Comments

@lae0901
Copy link

lae0901 commented Nov 28, 2020

TypeScript Version: 4.1.2

Code

/**
 * function illustrates typescript compile error. Generic function recursively
 * calls itself.  Get error TS2345 on statement where function calls itself. 
 * This code compiles without error using typescript version 4.0.5 or lower.
 * The compile fails at typescript version 4.1.2
 * @param arr1 
 * @param arr2 
 */
export function genericArrayProblem<T>(arr1: T[], arr2: T[]): boolean 
{
  for( let ix = 0 ; ix < arr1.length ; ++ix )
  {
    if ( ix < arr2.length )
    {
      const item1 = arr1[ix];
      const item2 = arr2[ix];
      
      if (Array.isArray(item1) && Array.isArray(item2))
      {
        const res = genericArrayProblem(item1, item2);
      }
    }
  }

  return true ;
}

Expected behavior:
Expecting code to compile. It compiles successfully using typescript version 4.0.5 or lower.

Actual behavior:
get compile error:

error TS2345: Argument of type 'T & (T extends readonly any[] ? unknown extends T 
? never : readonly any[] : any[])' is not assignable to parameter of type 'any[]'.

     const res = genericArrayProblem(item1, item2);
@RyanCavanaugh
Copy link
Member

Evaluating isArray breaks at #41808

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants