Skip to content

TS2345 when generic function calls itself. #41714

Closed
@lae0901

Description

@lae0901

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);

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions