Closed
Description
TypeScript Version: 4.1.x and 4.2.0-dev.20201123
Works on 4.0.x
Search Terms: TS7053
Code
export type ComparablePrimitives = undefined | null | boolean | string | number;
export type ComparableArray = Array< Comparable >;
export type ComparableObject = { [ key: string ]: Comparable; };
export type Comparable =
| ComparablePrimitives
| ComparableArray
| ComparableObject;
export function isEqual< T extends Comparable >( a: T, b: T ): boolean;
export function isEqual< T extends Comparable, U extends Comparable >( a: T, b: U ): false;
export function isEqual< T extends Comparable, U extends Comparable >( a: T, b: U ): boolean
{
if ( Array.isArray( a ) && Array.isArray( b ) )
return !a.some( ( value, index ) => !isEqual( value, b[ index ] ) );
return false;
}
(the function body in this example is not my real code, but shows the compilation error)
Expected behavior: should compile
Actual behavior: fails with:
TS7053: Element implicitly has an 'any' type because expression of type 'number' can't be used to index type '(undefined & (U extends readonly any[] ? unknown extends U ? never : readonly any[] : any[])) | (null & (U extends readonly any[] ? unknown extends U ? never : readonly any[] : any[])) | (string & (U extends readonly any[] ? unknown extends U ? never : readonly any[] : any[])) | ... 4 more ... | (ComparableObject & ...'.
No index signature with a parameter of type 'number' was found on type '(undefined & (U extends readonly any[] ? unknown extends U ? never : readonly any[] : any[])) | (null & (U extends readonly any[] ? unknown extends U ? never : readonly any[] : any[])) | (string & (U extends readonly any[] ? unknown extends U ? never : readonly any[] : any[])) | ... 4 more ... | (ComparableObject & ...'.
return !a.some( ( value, index ) => !isEqual( value, b[ index ] ) );
~~~~~~~~~~
Related Issues: