Skip to content

Commit df5ffc0

Browse files
Remove special any assignability for numeric index signatures (#41660)
* Only enable special assignability rule on string index signatures to 'any'. * Accepted baselines. * Added test. * Accepted baselines. * Renamed test files. * Add non-erroring version of bclas'subClassThisTypeAssignable01.ts' * Accepted baselines. Co-authored-by: TypeScript Bot <[email protected]>
1 parent 2c5cee5 commit df5ffc0

20 files changed

+392
-36
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18948,7 +18948,7 @@ namespace ts {
1894818948
return indexTypesIdenticalTo(source, target, kind);
1894918949
}
1895018950
const targetType = getIndexTypeOfType(target, kind);
18951-
if (!targetType || targetType.flags & TypeFlags.Any && !sourceIsPrimitive) {
18951+
if (!targetType || targetType.flags & TypeFlags.Any && !sourceIsPrimitive && kind === IndexKind.String) {
1895218952
// Index signature of type any permits assignment from everything but primitives
1895318953
return Ternary.True;
1895418954
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/functionAssignabilityWithArrayLike01.ts(2,7): error TS2322: Type '() => void' is not assignable to type 'ArrayLike<any>'.
2+
Index signature is missing in type '() => void'.
3+
4+
5+
==== tests/cases/compiler/functionAssignabilityWithArrayLike01.ts (1 errors) ====
6+
function func() {}
7+
const array: ArrayLike<any> = func;
8+
~~~~~
9+
!!! error TS2322: Type '() => void' is not assignable to type 'ArrayLike<any>'.
10+
!!! error TS2322: Index signature is missing in type '() => void'.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
//// [functionAssignabilityWithArrayLike01.ts]
2+
function func() {}
3+
const array: ArrayLike<any> = func;
4+
5+
//// [functionAssignabilityWithArrayLike01.js]
6+
function func() { }
7+
var array = func;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/functionAssignabilityWithArrayLike01.ts ===
2+
function func() {}
3+
>func : Symbol(func, Decl(functionAssignabilityWithArrayLike01.ts, 0, 0))
4+
5+
const array: ArrayLike<any> = func;
6+
>array : Symbol(array, Decl(functionAssignabilityWithArrayLike01.ts, 1, 5))
7+
>ArrayLike : Symbol(ArrayLike, Decl(lib.es5.d.ts, --, --))
8+
>func : Symbol(func, Decl(functionAssignabilityWithArrayLike01.ts, 0, 0))
9+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/functionAssignabilityWithArrayLike01.ts ===
2+
function func() {}
3+
>func : () => void
4+
5+
const array: ArrayLike<any> = func;
6+
>array : ArrayLike<any>
7+
>func : () => void
8+
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
tests/cases/compiler/functionAssignabilityWithArrayLike01.ts(2,7): error TS2322: Type '() => void' is not assignable to type 'ArrayLike<any>'.
2+
Index signature is missing in type '() => void'.
3+
4+
5+
==== tests/cases/compiler/functionAssignabilityWithArrayLike01.ts (1 errors) ====
6+
function func() {}
7+
const array: ArrayLike<any> = func;
8+
~~~~~
9+
!!! error TS2322: Type '() => void' is not assignable to type 'ArrayLike<any>'.
10+
!!! error TS2322: Index signature is missing in type '() => void'.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//// [functionAssignabilityWithArrayLike01.ts]
2+
function func() {}
3+
const array: ArrayLike<any> = func;
4+
5+
//// [functionAssignabilityWithArrayLike01.js]
6+
"use strict";
7+
function func() { }
8+
var array = func;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
=== tests/cases/compiler/functionAssignabilityWithArrayLike01.ts ===
2+
function func() {}
3+
>func : Symbol(func, Decl(functionAssignabilityWithArrayLike01.ts, 0, 0))
4+
5+
const array: ArrayLike<any> = func;
6+
>array : Symbol(array, Decl(functionAssignabilityWithArrayLike01.ts, 1, 5))
7+
>ArrayLike : Symbol(ArrayLike, Decl(lib.es5.d.ts, --, --))
8+
>func : Symbol(func, Decl(functionAssignabilityWithArrayLike01.ts, 0, 0))
9+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
=== tests/cases/compiler/functionAssignabilityWithArrayLike01.ts ===
2+
function func() {}
3+
>func : () => void
4+
5+
const array: ArrayLike<any> = func;
6+
>array : ArrayLike<any>
7+
>func : () => void
8+

tests/baselines/reference/subclassThisTypeAssignable.errors.txt

Lines changed: 0 additions & 34 deletions
This file was deleted.

0 commit comments

Comments
 (0)