Skip to content

Commit 4499503

Browse files
committed
Fixed issues in relater related to tuples with variadic elements
1 parent f5ccf43 commit 4499503

5 files changed

Lines changed: 543 additions & 9 deletions

File tree

src/compiler/checker.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24383,7 +24383,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2438324383
const sourceArity = getTypeReferenceArity(source);
2438424384
const targetArity = getTypeReferenceArity(target);
2438524385
const sourceRestFlag = isTupleType(source) ? source.target.combinedFlags & ElementFlags.Rest : ElementFlags.Rest;
24386-
const targetHasRestElement = !!(target.target.combinedFlags & ElementFlags.Variable);
24386+
const targetHasVariableElement = !!(target.target.combinedFlags & ElementFlags.Variable);
2438724387
const sourceMinLength = isTupleType(source) ? source.target.minLength : 0;
2438824388
const targetMinLength = target.target.minLength;
2438924389
if (!sourceRestFlag && sourceArity < targetMinLength) {
@@ -24392,13 +24392,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2439224392
}
2439324393
return Ternary.False;
2439424394
}
24395-
if (!targetHasRestElement && targetArity < sourceMinLength) {
24395+
if (!targetHasVariableElement && targetArity < sourceMinLength) {
2439624396
if (reportErrors) {
2439724397
reportError(Diagnostics.Source_has_0_element_s_but_target_allows_only_1, sourceMinLength, targetArity);
2439824398
}
2439924399
return Ternary.False;
2440024400
}
24401-
if (!targetHasRestElement && (sourceRestFlag || targetArity < sourceArity)) {
24401+
if (!targetHasVariableElement && (sourceRestFlag || targetArity < sourceArity)) {
2440224402
if (reportErrors) {
2440324403
if (sourceMinLength < targetMinLength) {
2440424404
reportError(Diagnostics.Target_requires_0_element_s_but_source_may_have_fewer, targetMinLength);
@@ -24411,22 +24411,29 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2441124411
}
2441224412
const sourceTypeArguments = getTypeArguments(source);
2441324413
const targetTypeArguments = getTypeArguments(target);
24414-
const targetStartCount = getStartElementCount(target.target, ElementFlags.NonRest);
24415-
const targetEndCount = getEndElementCount(target.target, ElementFlags.NonRest);
24414+
const targetStartCount = getStartElementCount(target.target, ElementFlags.Fixed);
24415+
const targetEndCount = getEndElementCount(target.target, ElementFlags.Fixed);
2441624416
let canExcludeDiscriminants = !!excludedProperties;
2441724417
for (let sourcePosition = 0; sourcePosition < sourceArity; sourcePosition++) {
2441824418
const sourceFlags = isTupleType(source) ? source.target.elementFlags[sourcePosition] : ElementFlags.Rest;
2441924419
const sourcePositionFromEnd = sourceArity - 1 - sourcePosition;
2442024420

24421-
const targetPosition = targetHasRestElement && sourcePosition >= targetStartCount
24422-
? targetArity - 1 - Math.min(sourcePositionFromEnd, targetEndCount)
24421+
const targetPosition = targetHasVariableElement && sourcePosition >= targetStartCount
24422+
? sourcePositionFromEnd < targetEndCount
24423+
? targetArity - 1 - sourcePositionFromEnd
24424+
: Math.min(sourcePosition, targetArity - 1 - targetEndCount)
2442324425
: sourcePosition;
2442424426

2442524427
const targetFlags = target.target.elementFlags[targetPosition];
2442624428

2442724429
if (targetFlags & ElementFlags.Variadic && !(sourceFlags & ElementFlags.Variadic)) {
2442824430
if (reportErrors) {
24429-
reportError(Diagnostics.Source_provides_no_match_for_variadic_element_at_position_0_in_target, targetPosition);
24431+
if (sourcePosition > targetStartCount) {
24432+
reportError(Diagnostics.Target_allows_only_0_element_s_but_source_may_have_more, targetArity);
24433+
}
24434+
else {
24435+
reportError(Diagnostics.Source_provides_no_match_for_variadic_element_at_position_0_in_target, targetPosition);
24436+
}
2443024437
}
2443124438
return Ternary.False;
2443224439
}
@@ -24460,7 +24467,7 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2446024467
const related = isRelatedTo(sourceType, targetCheckType, RecursionFlags.Both, reportErrors, /*headMessage*/ undefined, intersectionState);
2446124468
if (!related) {
2446224469
if (reportErrors && (targetArity > 1 || sourceArity > 1)) {
24463-
if (targetHasRestElement && sourcePosition >= targetStartCount && sourcePositionFromEnd >= targetEndCount && targetStartCount !== sourceArity - targetEndCount - 1) {
24470+
if (targetHasVariableElement && sourcePosition >= targetStartCount && sourcePositionFromEnd >= targetEndCount && targetStartCount !== sourceArity - targetEndCount - 1) {
2446424471
reportIncompatibleError(Diagnostics.Type_at_positions_0_through_1_in_source_is_not_compatible_with_type_at_position_2_in_target, targetStartCount, sourceArity - targetEndCount - 1, targetPosition);
2446524472
}
2446624473
else {
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
variadicTuples4.ts(5,5): error TS2344: Type '[...A, boolean, boolean, boolean, boolean]' does not satisfy the constraint '[...A, boolean]'.
2+
Target allows only 2 element(s) but source may have more.
3+
variadicTuples4.ts(12,44): error TS2344: Type '[...A, boolean, boolean, boolean]' does not satisfy the constraint '[...A, boolean]'.
4+
Target allows only 2 element(s) but source may have more.
5+
variadicTuples4.ts(20,5): error TS2344: Type '[...A, boolean, ...B, boolean, boolean, boolean]' does not satisfy the constraint '[...A, boolean]'.
6+
Target allows only 2 element(s) but source may have more.
7+
variadicTuples4.ts(34,5): error TS2344: Type '[...A, boolean, ...B, boolean, boolean]' does not satisfy the constraint '[...A, boolean, ...B, boolean]'.
8+
Target allows only 4 element(s) but source may have more.
9+
variadicTuples4.ts(62,5): error TS2344: Type '[...A, boolean, boolean, ...B, boolean]' does not satisfy the constraint '[...A, boolean, ...B, boolean]'.
10+
Target allows only 4 element(s) but source may have more.
11+
variadicTuples4.ts(76,5): error TS2344: Type '[...A, boolean, ...B, boolean]' does not satisfy the constraint '[...A, boolean, boolean, ...B, boolean]'.
12+
Source has 4 element(s) but target requires 5.
13+
14+
15+
==== variadicTuples4.ts (6 errors) ====
16+
{
17+
type Inner<A extends any[], A2 extends [...A, boolean]> = A2;
18+
type Wrapper<A extends any[]> = Inner<
19+
A,
20+
[...A, boolean, boolean, boolean, boolean]
21+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
22+
!!! error TS2344: Type '[...A, boolean, boolean, boolean, boolean]' does not satisfy the constraint '[...A, boolean]'.
23+
!!! error TS2344: Target allows only 2 element(s) but source may have more.
24+
>;
25+
}
26+
27+
{
28+
type Inner<A extends any[], A2 extends [...A, boolean]> = A2;
29+
30+
type Wrapper<A extends any[]> = Inner<A, [...A, boolean, boolean, boolean]>;
31+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
32+
!!! error TS2344: Type '[...A, boolean, boolean, boolean]' does not satisfy the constraint '[...A, boolean]'.
33+
!!! error TS2344: Target allows only 2 element(s) but source may have more.
34+
}
35+
36+
{
37+
type Inner<A extends any[], A2 extends [...A, boolean]> = A2;
38+
39+
type Wrapper<A extends any[], B extends any[]> = Inner<
40+
A,
41+
[...A, boolean, ...B, boolean, boolean, boolean]
42+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
43+
!!! error TS2344: Type '[...A, boolean, ...B, boolean, boolean, boolean]' does not satisfy the constraint '[...A, boolean]'.
44+
!!! error TS2344: Target allows only 2 element(s) but source may have more.
45+
>;
46+
}
47+
48+
{
49+
type Inner<
50+
A extends unknown[],
51+
B extends unknown[],
52+
C extends [...A, boolean, ...B, boolean],
53+
> = C;
54+
55+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
56+
A,
57+
B,
58+
[...A, boolean, ...B, boolean, boolean]
59+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
60+
!!! error TS2344: Type '[...A, boolean, ...B, boolean, boolean]' does not satisfy the constraint '[...A, boolean, ...B, boolean]'.
61+
!!! error TS2344: Target allows only 4 element(s) but source may have more.
62+
>;
63+
}
64+
65+
{
66+
type Inner<
67+
A extends unknown[],
68+
B extends unknown[],
69+
C extends [...A, boolean, ...B, boolean],
70+
> = C;
71+
72+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
73+
A,
74+
B,
75+
[...A, boolean, ...B, boolean] // ok
76+
>;
77+
}
78+
79+
{
80+
type Inner<
81+
A extends unknown[],
82+
B extends unknown[],
83+
C extends [...A, boolean, ...B, boolean],
84+
> = C;
85+
86+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
87+
A,
88+
B,
89+
[...A, boolean, boolean, ...B, boolean]
90+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
91+
!!! error TS2344: Type '[...A, boolean, boolean, ...B, boolean]' does not satisfy the constraint '[...A, boolean, ...B, boolean]'.
92+
!!! error TS2344: Target allows only 4 element(s) but source may have more.
93+
>;
94+
}
95+
96+
{
97+
type Inner<
98+
A extends unknown[],
99+
B extends unknown[],
100+
C extends [...A, boolean, boolean, ...B, boolean],
101+
> = C;
102+
103+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
104+
A,
105+
B,
106+
[...A, boolean, ...B, boolean]
107+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
108+
!!! error TS2344: Type '[...A, boolean, ...B, boolean]' does not satisfy the constraint '[...A, boolean, boolean, ...B, boolean]'.
109+
!!! error TS2344: Source has 4 element(s) but target requires 5.
110+
>;
111+
}
112+
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
//// [tests/cases/conformance/types/tuple/variadicTuples4.ts] ////
2+
3+
=== variadicTuples4.ts ===
4+
{
5+
type Inner<A extends any[], A2 extends [...A, boolean]> = A2;
6+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 0, 1))
7+
>A : Symbol(A, Decl(variadicTuples4.ts, 1, 13))
8+
>A2 : Symbol(A2, Decl(variadicTuples4.ts, 1, 29))
9+
>A : Symbol(A, Decl(variadicTuples4.ts, 1, 13))
10+
>A2 : Symbol(A2, Decl(variadicTuples4.ts, 1, 29))
11+
12+
type Wrapper<A extends any[]> = Inner<
13+
>Wrapper : Symbol(Wrapper, Decl(variadicTuples4.ts, 1, 63))
14+
>A : Symbol(A, Decl(variadicTuples4.ts, 2, 15))
15+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 0, 1))
16+
17+
A,
18+
>A : Symbol(A, Decl(variadicTuples4.ts, 2, 15))
19+
20+
[...A, boolean, boolean, boolean, boolean]
21+
>A : Symbol(A, Decl(variadicTuples4.ts, 2, 15))
22+
23+
>;
24+
}
25+
26+
{
27+
type Inner<A extends any[], A2 extends [...A, boolean]> = A2;
28+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 8, 1))
29+
>A : Symbol(A, Decl(variadicTuples4.ts, 9, 13))
30+
>A2 : Symbol(A2, Decl(variadicTuples4.ts, 9, 29))
31+
>A : Symbol(A, Decl(variadicTuples4.ts, 9, 13))
32+
>A2 : Symbol(A2, Decl(variadicTuples4.ts, 9, 29))
33+
34+
type Wrapper<A extends any[]> = Inner<A, [...A, boolean, boolean, boolean]>;
35+
>Wrapper : Symbol(Wrapper, Decl(variadicTuples4.ts, 9, 63))
36+
>A : Symbol(A, Decl(variadicTuples4.ts, 11, 15))
37+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 8, 1))
38+
>A : Symbol(A, Decl(variadicTuples4.ts, 11, 15))
39+
>A : Symbol(A, Decl(variadicTuples4.ts, 11, 15))
40+
}
41+
42+
{
43+
type Inner<A extends any[], A2 extends [...A, boolean]> = A2;
44+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 14, 1))
45+
>A : Symbol(A, Decl(variadicTuples4.ts, 15, 13))
46+
>A2 : Symbol(A2, Decl(variadicTuples4.ts, 15, 29))
47+
>A : Symbol(A, Decl(variadicTuples4.ts, 15, 13))
48+
>A2 : Symbol(A2, Decl(variadicTuples4.ts, 15, 29))
49+
50+
type Wrapper<A extends any[], B extends any[]> = Inner<
51+
>Wrapper : Symbol(Wrapper, Decl(variadicTuples4.ts, 15, 63))
52+
>A : Symbol(A, Decl(variadicTuples4.ts, 17, 15))
53+
>B : Symbol(B, Decl(variadicTuples4.ts, 17, 31))
54+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 14, 1))
55+
56+
A,
57+
>A : Symbol(A, Decl(variadicTuples4.ts, 17, 15))
58+
59+
[...A, boolean, ...B, boolean, boolean, boolean]
60+
>A : Symbol(A, Decl(variadicTuples4.ts, 17, 15))
61+
>B : Symbol(B, Decl(variadicTuples4.ts, 17, 31))
62+
63+
>;
64+
}
65+
66+
{
67+
type Inner<
68+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 23, 1))
69+
70+
A extends unknown[],
71+
>A : Symbol(A, Decl(variadicTuples4.ts, 24, 13))
72+
73+
B extends unknown[],
74+
>B : Symbol(B, Decl(variadicTuples4.ts, 25, 24))
75+
76+
C extends [...A, boolean, ...B, boolean],
77+
>C : Symbol(C, Decl(variadicTuples4.ts, 26, 24))
78+
>A : Symbol(A, Decl(variadicTuples4.ts, 24, 13))
79+
>B : Symbol(B, Decl(variadicTuples4.ts, 25, 24))
80+
81+
> = C;
82+
>C : Symbol(C, Decl(variadicTuples4.ts, 26, 24))
83+
84+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
85+
>Wrapper : Symbol(Wrapper, Decl(variadicTuples4.ts, 28, 8))
86+
>A : Symbol(A, Decl(variadicTuples4.ts, 30, 15))
87+
>B : Symbol(B, Decl(variadicTuples4.ts, 30, 35))
88+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 23, 1))
89+
90+
A,
91+
>A : Symbol(A, Decl(variadicTuples4.ts, 30, 15))
92+
93+
B,
94+
>B : Symbol(B, Decl(variadicTuples4.ts, 30, 35))
95+
96+
[...A, boolean, ...B, boolean, boolean]
97+
>A : Symbol(A, Decl(variadicTuples4.ts, 30, 15))
98+
>B : Symbol(B, Decl(variadicTuples4.ts, 30, 35))
99+
100+
>;
101+
}
102+
103+
{
104+
type Inner<
105+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 37, 1))
106+
107+
A extends unknown[],
108+
>A : Symbol(A, Decl(variadicTuples4.ts, 38, 13))
109+
110+
B extends unknown[],
111+
>B : Symbol(B, Decl(variadicTuples4.ts, 39, 24))
112+
113+
C extends [...A, boolean, ...B, boolean],
114+
>C : Symbol(C, Decl(variadicTuples4.ts, 40, 24))
115+
>A : Symbol(A, Decl(variadicTuples4.ts, 38, 13))
116+
>B : Symbol(B, Decl(variadicTuples4.ts, 39, 24))
117+
118+
> = C;
119+
>C : Symbol(C, Decl(variadicTuples4.ts, 40, 24))
120+
121+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
122+
>Wrapper : Symbol(Wrapper, Decl(variadicTuples4.ts, 42, 8))
123+
>A : Symbol(A, Decl(variadicTuples4.ts, 44, 15))
124+
>B : Symbol(B, Decl(variadicTuples4.ts, 44, 35))
125+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 37, 1))
126+
127+
A,
128+
>A : Symbol(A, Decl(variadicTuples4.ts, 44, 15))
129+
130+
B,
131+
>B : Symbol(B, Decl(variadicTuples4.ts, 44, 35))
132+
133+
[...A, boolean, ...B, boolean] // ok
134+
>A : Symbol(A, Decl(variadicTuples4.ts, 44, 15))
135+
>B : Symbol(B, Decl(variadicTuples4.ts, 44, 35))
136+
137+
>;
138+
}
139+
140+
{
141+
type Inner<
142+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 51, 1))
143+
144+
A extends unknown[],
145+
>A : Symbol(A, Decl(variadicTuples4.ts, 52, 13))
146+
147+
B extends unknown[],
148+
>B : Symbol(B, Decl(variadicTuples4.ts, 53, 24))
149+
150+
C extends [...A, boolean, ...B, boolean],
151+
>C : Symbol(C, Decl(variadicTuples4.ts, 54, 24))
152+
>A : Symbol(A, Decl(variadicTuples4.ts, 52, 13))
153+
>B : Symbol(B, Decl(variadicTuples4.ts, 53, 24))
154+
155+
> = C;
156+
>C : Symbol(C, Decl(variadicTuples4.ts, 54, 24))
157+
158+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
159+
>Wrapper : Symbol(Wrapper, Decl(variadicTuples4.ts, 56, 8))
160+
>A : Symbol(A, Decl(variadicTuples4.ts, 58, 15))
161+
>B : Symbol(B, Decl(variadicTuples4.ts, 58, 35))
162+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 51, 1))
163+
164+
A,
165+
>A : Symbol(A, Decl(variadicTuples4.ts, 58, 15))
166+
167+
B,
168+
>B : Symbol(B, Decl(variadicTuples4.ts, 58, 35))
169+
170+
[...A, boolean, boolean, ...B, boolean]
171+
>A : Symbol(A, Decl(variadicTuples4.ts, 58, 15))
172+
>B : Symbol(B, Decl(variadicTuples4.ts, 58, 35))
173+
174+
>;
175+
}
176+
177+
{
178+
type Inner<
179+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 65, 1))
180+
181+
A extends unknown[],
182+
>A : Symbol(A, Decl(variadicTuples4.ts, 66, 13))
183+
184+
B extends unknown[],
185+
>B : Symbol(B, Decl(variadicTuples4.ts, 67, 24))
186+
187+
C extends [...A, boolean, boolean, ...B, boolean],
188+
>C : Symbol(C, Decl(variadicTuples4.ts, 68, 24))
189+
>A : Symbol(A, Decl(variadicTuples4.ts, 66, 13))
190+
>B : Symbol(B, Decl(variadicTuples4.ts, 67, 24))
191+
192+
> = C;
193+
>C : Symbol(C, Decl(variadicTuples4.ts, 68, 24))
194+
195+
type Wrapper<A extends unknown[], B extends unknown[]> = Inner<
196+
>Wrapper : Symbol(Wrapper, Decl(variadicTuples4.ts, 70, 8))
197+
>A : Symbol(A, Decl(variadicTuples4.ts, 72, 15))
198+
>B : Symbol(B, Decl(variadicTuples4.ts, 72, 35))
199+
>Inner : Symbol(Inner, Decl(variadicTuples4.ts, 65, 1))
200+
201+
A,
202+
>A : Symbol(A, Decl(variadicTuples4.ts, 72, 15))
203+
204+
B,
205+
>B : Symbol(B, Decl(variadicTuples4.ts, 72, 35))
206+
207+
[...A, boolean, ...B, boolean]
208+
>A : Symbol(A, Decl(variadicTuples4.ts, 72, 15))
209+
>B : Symbol(B, Decl(variadicTuples4.ts, 72, 35))
210+
211+
>;
212+
}
213+

0 commit comments

Comments
 (0)