Skip to content

Commit 1a4c15f

Browse files
authored
handle generic types in getArrayifiedType (microsoft#30606)
1 parent 2eea216 commit 1a4c15f

File tree

6 files changed

+94
-1
lines changed

6 files changed

+94
-1
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20443,7 +20443,7 @@ namespace ts {
2044320443

2044420444
function getArrayifiedType(type: Type) {
2044520445
if (forEachType(type, t => !(t.flags & (TypeFlags.Any | TypeFlags.Instantiable) || isArrayType(t) || isTupleType(t)))) {
20446-
return createArrayType(getIndexTypeOfType(type, IndexKind.Number) || errorType);
20446+
return createArrayType(getIndexedAccessType(type, numberType));
2044720447
}
2044820448
return type;
2044920449
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/restTypeRetainsMappyness.ts(7,8): error TS2345: Argument of type 'Foo<T>[number][]' is not assignable to parameter of type 'Foo<T>'.
2+
3+
4+
==== tests/cases/compiler/restTypeRetainsMappyness.ts (1 errors) ====
5+
type Foo<T extends any[]> = {
6+
[P in keyof T]: T[P]
7+
}
8+
9+
function test<T extends any[]>(fn: (...args: Foo<T>) => void) {
10+
const arr: Foo<T> = {} as any
11+
fn(...arr) // Error: Argument of type 'any[]' is not assignable to parameter of type 'Foo<T>'
12+
~~~~~~
13+
!!! error TS2345: Argument of type 'Foo<T>[number][]' is not assignable to parameter of type 'Foo<T>'.
14+
}
15+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
//// [restTypeRetainsMappyness.ts]
2+
type Foo<T extends any[]> = {
3+
[P in keyof T]: T[P]
4+
}
5+
6+
function test<T extends any[]>(fn: (...args: Foo<T>) => void) {
7+
const arr: Foo<T> = {} as any
8+
fn(...arr) // Error: Argument of type 'any[]' is not assignable to parameter of type 'Foo<T>'
9+
}
10+
11+
12+
//// [restTypeRetainsMappyness.js]
13+
function test(fn) {
14+
var arr = {};
15+
fn.apply(void 0, arr); // Error: Argument of type 'any[]' is not assignable to parameter of type 'Foo<T>'
16+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
=== tests/cases/compiler/restTypeRetainsMappyness.ts ===
2+
type Foo<T extends any[]> = {
3+
>Foo : Symbol(Foo, Decl(restTypeRetainsMappyness.ts, 0, 0))
4+
>T : Symbol(T, Decl(restTypeRetainsMappyness.ts, 0, 9))
5+
6+
[P in keyof T]: T[P]
7+
>P : Symbol(P, Decl(restTypeRetainsMappyness.ts, 1, 5))
8+
>T : Symbol(T, Decl(restTypeRetainsMappyness.ts, 0, 9))
9+
>T : Symbol(T, Decl(restTypeRetainsMappyness.ts, 0, 9))
10+
>P : Symbol(P, Decl(restTypeRetainsMappyness.ts, 1, 5))
11+
}
12+
13+
function test<T extends any[]>(fn: (...args: Foo<T>) => void) {
14+
>test : Symbol(test, Decl(restTypeRetainsMappyness.ts, 2, 1))
15+
>T : Symbol(T, Decl(restTypeRetainsMappyness.ts, 4, 14))
16+
>fn : Symbol(fn, Decl(restTypeRetainsMappyness.ts, 4, 31))
17+
>args : Symbol(args, Decl(restTypeRetainsMappyness.ts, 4, 36))
18+
>Foo : Symbol(Foo, Decl(restTypeRetainsMappyness.ts, 0, 0))
19+
>T : Symbol(T, Decl(restTypeRetainsMappyness.ts, 4, 14))
20+
21+
const arr: Foo<T> = {} as any
22+
>arr : Symbol(arr, Decl(restTypeRetainsMappyness.ts, 5, 9))
23+
>Foo : Symbol(Foo, Decl(restTypeRetainsMappyness.ts, 0, 0))
24+
>T : Symbol(T, Decl(restTypeRetainsMappyness.ts, 4, 14))
25+
26+
fn(...arr) // Error: Argument of type 'any[]' is not assignable to parameter of type 'Foo<T>'
27+
>fn : Symbol(fn, Decl(restTypeRetainsMappyness.ts, 4, 31))
28+
>arr : Symbol(arr, Decl(restTypeRetainsMappyness.ts, 5, 9))
29+
}
30+
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
=== tests/cases/compiler/restTypeRetainsMappyness.ts ===
2+
type Foo<T extends any[]> = {
3+
>Foo : Foo<T>
4+
5+
[P in keyof T]: T[P]
6+
}
7+
8+
function test<T extends any[]>(fn: (...args: Foo<T>) => void) {
9+
>test : <T extends any[]>(fn: (...args: Foo<T>) => void) => void
10+
>fn : (...args: Foo<T>) => void
11+
>args : Foo<T>
12+
13+
const arr: Foo<T> = {} as any
14+
>arr : Foo<T>
15+
>{} as any : any
16+
>{} : {}
17+
18+
fn(...arr) // Error: Argument of type 'any[]' is not assignable to parameter of type 'Foo<T>'
19+
>fn(...arr) : void
20+
>fn : (...args: Foo<T>) => void
21+
>...arr : any
22+
>arr : Foo<T>
23+
}
24+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
type Foo<T extends any[]> = {
2+
[P in keyof T]: T[P]
3+
}
4+
5+
function test<T extends any[]>(fn: (...args: Foo<T>) => void) {
6+
const arr: Foo<T> = {} as any
7+
fn(...arr) // Error: Argument of type 'any[]' is not assignable to parameter of type 'Foo<T>'
8+
}

0 commit comments

Comments
 (0)