Skip to content

Commit 61ffce0

Browse files
authored
Fixed reported errors for variadic element mismatches (#58708)
1 parent e6add98 commit 61ffce0

File tree

7 files changed

+162
-12
lines changed

7 files changed

+162
-12
lines changed

src/compiler/checker.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23665,7 +23665,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2366523665
const sourceArity = getTypeReferenceArity(source);
2366623666
const targetArity = getTypeReferenceArity(target);
2366723667
const sourceRestFlag = isTupleType(source) ? source.target.combinedFlags & ElementFlags.Rest : ElementFlags.Rest;
23668-
const targetRestFlag = target.target.combinedFlags & ElementFlags.Rest;
23668+
const targetHasRestElement = !!(target.target.combinedFlags & ElementFlags.Variable);
2366923669
const sourceMinLength = isTupleType(source) ? source.target.minLength : 0;
2367023670
const targetMinLength = target.target.minLength;
2367123671
if (!sourceRestFlag && sourceArity < targetMinLength) {
@@ -23674,13 +23674,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2367423674
}
2367523675
return Ternary.False;
2367623676
}
23677-
if (!targetRestFlag && targetArity < sourceMinLength) {
23677+
if (!targetHasRestElement && targetArity < sourceMinLength) {
2367823678
if (reportErrors) {
2367923679
reportError(Diagnostics.Source_has_0_element_s_but_target_allows_only_1, sourceMinLength, targetArity);
2368023680
}
2368123681
return Ternary.False;
2368223682
}
23683-
if (!targetRestFlag && (sourceRestFlag || targetArity < sourceArity)) {
23683+
if (!targetHasRestElement && (sourceRestFlag || targetArity < sourceArity)) {
2368423684
if (reportErrors) {
2368523685
if (sourceMinLength < targetMinLength) {
2368623686
reportError(Diagnostics.Target_requires_0_element_s_but_source_may_have_fewer, targetMinLength);
@@ -23695,7 +23695,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2369523695
const targetTypeArguments = getTypeArguments(target);
2369623696
const targetStartCount = getStartElementCount(target.target, ElementFlags.NonRest);
2369723697
const targetEndCount = getEndElementCount(target.target, ElementFlags.NonRest);
23698-
const targetHasRestElement = target.target.hasRestElement;
2369923698
let canExcludeDiscriminants = !!excludedProperties;
2370023699
for (let sourcePosition = 0; sourcePosition < sourceArity; sourcePosition++) {
2370123700
const sourceFlags = isTupleType(source) ? source.target.elementFlags[sourcePosition] : ElementFlags.Rest;

tests/baselines/reference/variadicTuples1.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ variadicTuples1.ts(62,5): error TS2345: Argument of type '[]' is not assignable
55
variadicTuples1.ts(131,9): error TS2344: Type 'V' does not satisfy the constraint 'unknown[]'.
66
The type 'readonly unknown[]' is 'readonly' and cannot be assigned to the mutable type 'unknown[]'.
77
variadicTuples1.ts(149,5): error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...T]'.
8-
Target requires 2 element(s) but source may have fewer.
8+
Source provides no match for variadic element at position 1 in target.
99
variadicTuples1.ts(151,5): error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...U]'.
10-
Target requires 2 element(s) but source may have fewer.
10+
Source provides no match for variadic element at position 1 in target.
1111
variadicTuples1.ts(152,5): error TS2322: Type '[string, ...T]' is not assignable to type '[string, ...U]'.
1212
Type at position 1 in source is not compatible with type at position 1 in target.
1313
Type 'T' is not assignable to type 'U'.
@@ -22,7 +22,7 @@ variadicTuples1.ts(170,5): error TS2322: Type 'T' is not assignable to type '[..
2222
variadicTuples1.ts(171,5): error TS4104: The type 'readonly [...T]' is 'readonly' and cannot be assigned to the mutable type '[...T]'.
2323
variadicTuples1.ts(181,5): error TS2322: Type 'T' is not assignable to type '[...U]'.
2424
Type 'string[]' is not assignable to type '[...U]'.
25-
Target requires 1 element(s) but source may have fewer.
25+
Source provides no match for variadic element at position 0 in target.
2626
variadicTuples1.ts(182,5): error TS2322: Type '[...T]' is not assignable to type '[...U]'.
2727
Type 'T' is not assignable to type 'U'.
2828
'T' is assignable to the constraint of type 'U', but 'U' could be instantiated with a different subtype of constraint 'string[]'.
@@ -211,12 +211,12 @@ variadicTuples1.ts(411,7): error TS2322: Type '[boolean, false]' is not assignab
211211
y = x; // Error
212212
~
213213
!!! error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...T]'.
214-
!!! error TS2322: Target requires 2 element(s) but source may have fewer.
214+
!!! error TS2322: Source provides no match for variadic element at position 1 in target.
215215
y = z;
216216
z = x; // Error
217217
~
218218
!!! error TS2322: Type '[string, ...unknown[]]' is not assignable to type '[string, ...U]'.
219-
!!! error TS2322: Target requires 2 element(s) but source may have fewer.
219+
!!! error TS2322: Source provides no match for variadic element at position 1 in target.
220220
z = y; // Error
221221
~
222222
!!! error TS2322: Type '[string, ...T]' is not assignable to type '[string, ...U]'.
@@ -268,7 +268,7 @@ variadicTuples1.ts(411,7): error TS2322: Type '[boolean, false]' is not assignab
268268
~~
269269
!!! error TS2322: Type 'T' is not assignable to type '[...U]'.
270270
!!! error TS2322: Type 'string[]' is not assignable to type '[...U]'.
271-
!!! error TS2322: Target requires 1 element(s) but source may have fewer.
271+
!!! error TS2322: Source provides no match for variadic element at position 0 in target.
272272
t2 = t1; // Error
273273
~~
274274
!!! error TS2322: Type '[...T]' is not assignable to type '[...U]'.

tests/baselines/reference/variadicTuples2.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ variadicTuples2.ts(73,8): error TS2345: Argument of type '["abc", "def", true]'
3838
variadicTuples2.ts(76,5): error TS2322: Type '[number, number]' is not assignable to type '[number, ...T]'.
3939
Source provides no match for variadic element at position 1 in target.
4040
variadicTuples2.ts(77,5): error TS2322: Type '[number, ...number[]]' is not assignable to type '[number, ...T]'.
41-
Target requires 2 element(s) but source may have fewer.
41+
Source provides no match for variadic element at position 1 in target.
4242
variadicTuples2.ts(78,5): error TS2322: Type '[number, ...T]' is not assignable to type '[number, number]'.
4343
Type '[number, ...unknown[]]' is not assignable to type '[number, number]'.
4444
Target requires 2 element(s) but source may have fewer.
@@ -208,7 +208,7 @@ variadicTuples2.ts(139,25): error TS2345: Argument of type '["blah2", 1, 2, 3]'
208208
x = z; // Error
209209
~
210210
!!! error TS2322: Type '[number, ...number[]]' is not assignable to type '[number, ...T]'.
211-
!!! error TS2322: Target requires 2 element(s) but source may have fewer.
211+
!!! error TS2322: Source provides no match for variadic element at position 1 in target.
212212
y = x; // Error
213213
~
214214
!!! error TS2322: Type '[number, ...T]' is not assignable to type '[number, number]'.
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
variadicTuples3.ts(5,3): error TS2322: Type 'any[]' is not assignable to type '[...T, ...P]'.
2+
Source provides no match for variadic element at position 0 in target.
3+
variadicTuples3.ts(10,3): error TS2322: Type '[any, any]' is not assignable to type '[...T, ...P]'.
4+
Source provides no match for variadic element at position 0 in target.
5+
variadicTuples3.ts(15,3): error TS2322: Type '[any, any, any]' is not assignable to type '[...T, ...P]'.
6+
Source provides no match for variadic element at position 0 in target.
7+
8+
9+
==== variadicTuples3.ts (3 errors) ====
10+
// https://github.com/microsoft/TypeScript/issues/58697
11+
12+
function test1<T extends any[], P extends any[]>(): [...T, ...P] {
13+
let x: any[] = [];
14+
return x;
15+
~~~~~~
16+
!!! error TS2322: Type 'any[]' is not assignable to type '[...T, ...P]'.
17+
!!! error TS2322: Source provides no match for variadic element at position 0 in target.
18+
}
19+
20+
function test2<T extends any[], P extends any[]>(): [...T, ...P] {
21+
let x: [any, any] = [null, null];
22+
return x;
23+
~~~~~~
24+
!!! error TS2322: Type '[any, any]' is not assignable to type '[...T, ...P]'.
25+
!!! error TS2322: Source provides no match for variadic element at position 0 in target.
26+
}
27+
28+
function test3<T extends any[], P extends any[]>(): [...T, ...P] {
29+
let x: [any, any, any] = [null, null, null];
30+
return x;
31+
~~~~~~
32+
!!! error TS2322: Type '[any, any, any]' is not assignable to type '[...T, ...P]'.
33+
!!! error TS2322: Source provides no match for variadic element at position 0 in target.
34+
}
35+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
//// [tests/cases/conformance/types/tuple/variadicTuples3.ts] ////
2+
3+
=== variadicTuples3.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/58697
5+
6+
function test1<T extends any[], P extends any[]>(): [...T, ...P] {
7+
>test1 : Symbol(test1, Decl(variadicTuples3.ts, 0, 0))
8+
>T : Symbol(T, Decl(variadicTuples3.ts, 2, 15))
9+
>P : Symbol(P, Decl(variadicTuples3.ts, 2, 31))
10+
>T : Symbol(T, Decl(variadicTuples3.ts, 2, 15))
11+
>P : Symbol(P, Decl(variadicTuples3.ts, 2, 31))
12+
13+
let x: any[] = [];
14+
>x : Symbol(x, Decl(variadicTuples3.ts, 3, 5))
15+
16+
return x;
17+
>x : Symbol(x, Decl(variadicTuples3.ts, 3, 5))
18+
}
19+
20+
function test2<T extends any[], P extends any[]>(): [...T, ...P] {
21+
>test2 : Symbol(test2, Decl(variadicTuples3.ts, 5, 1))
22+
>T : Symbol(T, Decl(variadicTuples3.ts, 7, 15))
23+
>P : Symbol(P, Decl(variadicTuples3.ts, 7, 31))
24+
>T : Symbol(T, Decl(variadicTuples3.ts, 7, 15))
25+
>P : Symbol(P, Decl(variadicTuples3.ts, 7, 31))
26+
27+
let x: [any, any] = [null, null];
28+
>x : Symbol(x, Decl(variadicTuples3.ts, 8, 5))
29+
30+
return x;
31+
>x : Symbol(x, Decl(variadicTuples3.ts, 8, 5))
32+
}
33+
34+
function test3<T extends any[], P extends any[]>(): [...T, ...P] {
35+
>test3 : Symbol(test3, Decl(variadicTuples3.ts, 10, 1))
36+
>T : Symbol(T, Decl(variadicTuples3.ts, 12, 15))
37+
>P : Symbol(P, Decl(variadicTuples3.ts, 12, 31))
38+
>T : Symbol(T, Decl(variadicTuples3.ts, 12, 15))
39+
>P : Symbol(P, Decl(variadicTuples3.ts, 12, 31))
40+
41+
let x: [any, any, any] = [null, null, null];
42+
>x : Symbol(x, Decl(variadicTuples3.ts, 13, 5))
43+
44+
return x;
45+
>x : Symbol(x, Decl(variadicTuples3.ts, 13, 5))
46+
}
47+
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//// [tests/cases/conformance/types/tuple/variadicTuples3.ts] ////
2+
3+
=== variadicTuples3.ts ===
4+
// https://github.com/microsoft/TypeScript/issues/58697
5+
6+
function test1<T extends any[], P extends any[]>(): [...T, ...P] {
7+
>test1 : <T extends any[], P extends any[]>() => [...T, ...P]
8+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^^^^^^
9+
10+
let x: any[] = [];
11+
>x : any[]
12+
> : ^^^^^
13+
>[] : never[]
14+
> : ^^^^^^^
15+
16+
return x;
17+
>x : any[]
18+
> : ^^^^^
19+
}
20+
21+
function test2<T extends any[], P extends any[]>(): [...T, ...P] {
22+
>test2 : <T extends any[], P extends any[]>() => [...T, ...P]
23+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^^^^^^
24+
25+
let x: [any, any] = [null, null];
26+
>x : [any, any]
27+
> : ^^^^^^^^^^
28+
>[null, null] : [null, null]
29+
> : ^^^^^^^^^^^^
30+
31+
return x;
32+
>x : [any, any]
33+
> : ^^^^^^^^^^
34+
}
35+
36+
function test3<T extends any[], P extends any[]>(): [...T, ...P] {
37+
>test3 : <T extends any[], P extends any[]>() => [...T, ...P]
38+
> : ^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^^^^^^
39+
40+
let x: [any, any, any] = [null, null, null];
41+
>x : [any, any, any]
42+
> : ^^^^^^^^^^^^^^^
43+
>[null, null, null] : [null, null, null]
44+
> : ^^^^^^^^^^^^^^^^^^
45+
46+
return x;
47+
>x : [any, any, any]
48+
> : ^^^^^^^^^^^^^^^
49+
}
50+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// @strict: true
2+
// @noEmit: true
3+
4+
// https://github.com/microsoft/TypeScript/issues/58697
5+
6+
function test1<T extends any[], P extends any[]>(): [...T, ...P] {
7+
let x: any[] = [];
8+
return x;
9+
}
10+
11+
function test2<T extends any[], P extends any[]>(): [...T, ...P] {
12+
let x: [any, any] = [null, null];
13+
return x;
14+
}
15+
16+
function test3<T extends any[], P extends any[]>(): [...T, ...P] {
17+
let x: [any, any, any] = [null, null, null];
18+
return x;
19+
}

0 commit comments

Comments
 (0)