Skip to content

Commit 4db251b

Browse files
committed
new approach and all changed tests reviewed
1 parent 65fdfc1 commit 4db251b

22 files changed

+62
-108
lines changed

src/compiler/checker.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21965,8 +21965,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2196521965
typeToString(constraint),
2196621966
);
2196721967
}
21968-
// check if target is a descendant of the source
21969-
else if ((isGenericType(target) && isGenericType(source)) &&( isTypeAssignableTo(target, getBaseConstraintOrType(generalizedSource)) || (needsOriginalSource = isTypeAssignableTo(target, getBaseConstraintOrType(source))))) {
21968+
21969+
else if ((source.flags & TypeFlags.TypeParameter) && (isTypeAssignableTo(target, getBaseConstraintOrType(generalizedSource)) || (needsOriginalSource = isTypeAssignableTo(target, getBaseConstraintOrType(source))))) {
2197021970
reportError(
2197121971
Diagnostics._1_is_constrained_to_be_a_subtype_of_0,
2197221972
needsOriginalSource ? sourceType : generalizedSourceType,

tests/baselines/reference/assignmentCompatWithCallSignatures4.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ assignmentCompatWithCallSignatures4.ts(53,9): error TS2322: Type '(x: (arg: Base
1919
'Base' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Base'.
2020
assignmentCompatWithCallSignatures4.ts(58,9): error TS2322: Type '(...x: Base[]) => Base' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
2121
Type 'Base' is not assignable to type 'T'.
22-
'T' is constrained to be a subtype of 'Base'.
22+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
2323
assignmentCompatWithCallSignatures4.ts(62,9): error TS2322: Type '(x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type '<T extends Derived>(x: T, y: T) => T'.
2424
Type 'Base' is not assignable to type 'T'.
25-
'T' is constrained to be a subtype of 'Base'.
25+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
2626
assignmentCompatWithCallSignatures4.ts(66,9): error TS2322: Type '(x: Base[], y: Derived2[]) => Derived[]' is not assignable to type '<T extends Array<Derived2>>(x: Base[], y: Base[]) => T'.
2727
Type 'Derived[]' is not assignable to type 'T'.
28-
'T' is constrained to be a subtype of 'Derived[]'.
28+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
2929
assignmentCompatWithCallSignatures4.ts(69,9): error TS2322: Type '<T>(x: { a: T; b: T; }) => T' is not assignable to type '(x: { a: string; b: number; }) => number'.
3030
Types of parameters 'x' and 'x' are incompatible.
3131
Type '{ a: string; b: number; }' is not assignable to type '{ a: string; b: string; }'.
@@ -149,23 +149,23 @@ assignmentCompatWithCallSignatures4.ts(96,9): error TS2322: Type '<T>(x: T) => s
149149
~~~
150150
!!! error TS2322: Type '(...x: Base[]) => Base' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
151151
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
152-
!!! error TS2322: 'T' is constrained to be a subtype of 'Base'.
152+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
153153

154154
var b11: <T extends Derived>(x: T, y: T) => T;
155155
a11 = b11;
156156
b11 = a11;
157157
~~~
158158
!!! error TS2322: Type '(x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type '<T extends Derived>(x: T, y: T) => T'.
159159
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
160-
!!! error TS2322: 'T' is constrained to be a subtype of 'Base'.
160+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
161161

162162
var b12: <T extends Array<Derived2>>(x: Array<Base>, y: Array<Base>) => T;
163163
a12 = b12;
164164
b12 = a12;
165165
~~~
166166
!!! error TS2322: Type '(x: Base[], y: Derived2[]) => Derived[]' is not assignable to type '<T extends Array<Derived2>>(x: Base[], y: Base[]) => T'.
167167
!!! error TS2322: Type 'Derived[]' is not assignable to type 'T'.
168-
!!! error TS2322: 'T' is constrained to be a subtype of 'Derived[]'.
168+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
169169

170170
var b15: <T>(x: { a: T; b: T }) => T;
171171
a15 = b15;

tests/baselines/reference/assignmentCompatWithConstructSignatures4.errors.txt

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ assignmentCompatWithConstructSignatures4.ts(53,9): error TS2322: Type 'new (x: (
1919
'Base' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Base'.
2020
assignmentCompatWithConstructSignatures4.ts(58,9): error TS2322: Type 'new (...x: Base[]) => Base' is not assignable to type 'new <T extends Derived>(...x: T[]) => T'.
2121
Type 'Base' is not assignable to type 'T'.
22-
'T' is constrained to be a subtype of 'Base'.
22+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
2323
assignmentCompatWithConstructSignatures4.ts(62,9): error TS2322: Type 'new (x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type 'new <T extends Derived>(x: T, y: T) => T'.
2424
Type 'Base' is not assignable to type 'T'.
25-
'T' is constrained to be a subtype of 'Base'.
25+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
2626
assignmentCompatWithConstructSignatures4.ts(66,9): error TS2322: Type 'new (x: Base[], y: Derived2[]) => Derived[]' is not assignable to type 'new <T extends Array<Derived2>>(x: Base[], y: Base[]) => T'.
2727
Type 'Derived[]' is not assignable to type 'T'.
28-
'T' is constrained to be a subtype of 'Derived[]'.
28+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
2929
assignmentCompatWithConstructSignatures4.ts(69,9): error TS2322: Type 'new <T>(x: { a: T; b: T; }) => T' is not assignable to type 'new (x: { a: string; b: number; }) => number'.
3030
Types of parameters 'x' and 'x' are incompatible.
3131
Type '{ a: string; b: number; }' is not assignable to type '{ a: string; b: string; }'.
@@ -165,23 +165,23 @@ assignmentCompatWithConstructSignatures4.ts(96,9): error TS2322: Type 'new <T>(x
165165
~~~
166166
!!! error TS2322: Type 'new (...x: Base[]) => Base' is not assignable to type 'new <T extends Derived>(...x: T[]) => T'.
167167
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
168-
!!! error TS2322: 'T' is constrained to be a subtype of 'Base'.
168+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
169169

170170
var b11: new <T extends Derived>(x: T, y: T) => T;
171171
a11 = b11; // ok
172172
b11 = a11; // ok
173173
~~~
174174
!!! error TS2322: Type 'new (x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type 'new <T extends Derived>(x: T, y: T) => T'.
175175
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
176-
!!! error TS2322: 'T' is constrained to be a subtype of 'Base'.
176+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
177177

178178
var b12: new <T extends Array<Derived2>>(x: Array<Base>, y: Array<Base>) => T;
179179
a12 = b12; // ok
180180
b12 = a12; // ok
181181
~~~
182182
!!! error TS2322: Type 'new (x: Base[], y: Derived2[]) => Derived[]' is not assignable to type 'new <T extends Array<Derived2>>(x: Base[], y: Base[]) => T'.
183183
!!! error TS2322: Type 'Derived[]' is not assignable to type 'T'.
184-
!!! error TS2322: 'T' is constrained to be a subtype of 'Derived[]'.
184+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
185185

186186
var b15: new <T>(x: { a: T; b: T }) => T;
187187
a15 = b15; // ok

tests/baselines/reference/awaitedTypeNoLib.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ awaitedTypeNoLib.ts(18,27): error TS2345: Argument of type 'NotPromise<TResult>
1313
Type 'TResult | (TResult extends PromiseLike<unknown> ? never : TResult)' is not assignable to type 'Thenable<TResult>'.
1414
Type 'Thenable<unknown> & TResult' is not assignable to type 'Thenable<TResult>'.
1515
Type 'unknown' is not assignable to type 'TResult'.
16-
'TResult' is constrained to be a subtype of 'unknown'.
16+
'TResult' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.
1717

1818

1919
!!! error TS2318: Cannot find global type 'Array'.
@@ -52,7 +52,7 @@ awaitedTypeNoLib.ts(18,27): error TS2345: Argument of type 'NotPromise<TResult>
5252
!!! error TS2345: Type 'TResult | (TResult extends PromiseLike<unknown> ? never : TResult)' is not assignable to type 'Thenable<TResult>'.
5353
!!! error TS2345: Type 'Thenable<unknown> & TResult' is not assignable to type 'Thenable<TResult>'.
5454
!!! error TS2345: Type 'unknown' is not assignable to type 'TResult'.
55-
!!! error TS2345: 'TResult' is constrained to be a subtype of 'unknown'.
55+
!!! error TS2345: 'TResult' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.
5656
}
5757
}
5858

tests/baselines/reference/conditionalTypes1.errors.txt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ conditionalTypes1.ts(29,5): error TS2322: Type 'T["x"]' is not assignable to typ
2020
Type 'string | undefined' is not assignable to type '{}'.
2121
Type 'undefined' is not assignable to type '{}'.
2222
conditionalTypes1.ts(103,5): error TS2322: Type 'FunctionProperties<T>' is not assignable to type 'T'.
23-
'T' is constrained to be a subtype of 'FunctionProperties<T>'.
23+
'T' could be instantiated with an arbitrary type which could be unrelated to 'FunctionProperties<T>'.
2424
conditionalTypes1.ts(104,5): error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'T'.
25-
'T' is constrained to be a subtype of 'NonFunctionProperties<T>'.
25+
'T' could be instantiated with an arbitrary type which could be unrelated to 'NonFunctionProperties<T>'.
2626
conditionalTypes1.ts(106,5): error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'FunctionProperties<T>'.
2727
Type 'FunctionPropertyNames<T>' is not assignable to type 'NonFunctionPropertyNames<T>'.
2828
Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
@@ -212,11 +212,11 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
212212
x = y; // Error
213213
~
214214
!!! error TS2322: Type 'FunctionProperties<T>' is not assignable to type 'T'.
215-
!!! error TS2322: 'T' is constrained to be a subtype of 'FunctionProperties<T>'.
215+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'FunctionProperties<T>'.
216216
x = z; // Error
217217
~
218218
!!! error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'T'.
219-
!!! error TS2322: 'T' is constrained to be a subtype of 'NonFunctionProperties<T>'.
219+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'NonFunctionProperties<T>'.
220220
y = x;
221221
y = z; // Error
222222
~

tests/baselines/reference/deeplyNestedMappedTypes.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ deeplyNestedMappedTypes.ts(69,5): error TS2322: Type '{ level1: { level2: { foo:
99
The types of 'level1.level2' are incompatible between these types.
1010
Property 'bar' is missing in type '{ foo: string; }' but required in type '{ foo: string; bar: string; }'.
1111
deeplyNestedMappedTypes.ts(73,5): error TS2322: Type '{ level1: { level2: { foo: string; }; }; }[]' is not assignable to type 'T'.
12-
'T' is constrained to be a subtype of '{ level1: { level2: { foo: string; }; }; }[]'.
12+
'T' could be instantiated with an arbitrary type which could be unrelated to '{ level1: { level2: { foo: string; }; }; }[]'.
1313
deeplyNestedMappedTypes.ts(77,5): error TS2322: Type '{ level1: { level2: { foo: string; }; }; }[]' is not assignable to type '{ level1: { level2: { foo: string; bar: string; }; }; }[]'.
1414
Type '{ level1: { level2: { foo: string; }; }; }' is not assignable to type '{ level1: { level2: { foo: string; bar: string; }; }; }'.
1515
The types of 'level1.level2' are incompatible between these types.
@@ -106,7 +106,7 @@ deeplyNestedMappedTypes.ts(77,5): error TS2322: Type '{ level1: { level2: { foo:
106106
return ors; // Error
107107
~~~~~~
108108
!!! error TS2322: Type '{ level1: { level2: { foo: string; }; }; }[]' is not assignable to type 'T'.
109-
!!! error TS2322: 'T' is constrained to be a subtype of '{ level1: { level2: { foo: string; }; }; }[]'.
109+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '{ level1: { level2: { foo: string; }; }; }[]'.
110110
}
111111

112112
function problematicFunction3(ors: (typeof Input.static)[]): Output[] {

tests/baselines/reference/incompatibleAssignmentOfIdenticallyNamedTypes.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
incompatibleAssignmentOfIdenticallyNamedTypes.ts(6,9): error TS2719: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
2-
'T' is constrained to be a subtype of 'T'.
2+
'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.
33

44

55
==== incompatibleAssignmentOfIdenticallyNamedTypes.ts (1 errors) ====
@@ -11,7 +11,7 @@ incompatibleAssignmentOfIdenticallyNamedTypes.ts(6,9): error TS2719: Type 'T' is
1111
this.x = a;
1212
~~~~~~
1313
!!! error TS2719: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
14-
!!! error TS2719: 'T' is constrained to be a subtype of 'T'.
14+
!!! error TS2719: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.
1515
}
1616
}
1717

tests/baselines/reference/indexedAccessRelation.errors.txt

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ indexedAccessRelation.ts(16,23): error TS2345: Argument of type '{ a: T; }' is n
33
Type 'T' is not assignable to type 'S["a"] & T'.
44
Type 'Foo' is not assignable to type 'S["a"] & T'.
55
Type 'Foo' is not assignable to type 'S["a"]'.
6-
'S["a"]' is constrained to be a subtype of 'Foo'.
7-
Type 'T' is not assignable to type 'S["a"]'.
8-
'S["a"]' is constrained to be a subtype of 'T'.
9-
Type 'Foo' is not assignable to type 'S["a"]'.
10-
'S["a"]' is constrained to be a subtype of 'Foo'.
6+
'S["a"]' could be instantiated with an arbitrary type which could be unrelated to 'Foo'.
117

128

139
==== indexedAccessRelation.ts (1 errors) ====
@@ -33,11 +29,7 @@ indexedAccessRelation.ts(16,23): error TS2345: Argument of type '{ a: T; }' is n
3329
!!! error TS2345: Type 'T' is not assignable to type 'S["a"] & T'.
3430
!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"] & T'.
3531
!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"]'.
36-
!!! error TS2345: 'S["a"]' is constrained to be a subtype of 'Foo'.
37-
!!! error TS2345: Type 'T' is not assignable to type 'S["a"]'.
38-
!!! error TS2345: 'S["a"]' is constrained to be a subtype of 'T'.
39-
!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"]'.
40-
!!! error TS2345: 'S["a"]' is constrained to be a subtype of 'Foo'.
32+
!!! error TS2345: 'S["a"]' could be instantiated with an arbitrary type which could be unrelated to 'Foo'.
4133
}
4234
}
4335

tests/baselines/reference/mappedTypeRelationships.errors.txt

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,9 @@ mappedTypeRelationships.ts(21,12): error TS2536: Type 'keyof U' cannot be used t
99
mappedTypeRelationships.ts(25,5): error TS2536: Type 'K' cannot be used to index type 'T'.
1010
mappedTypeRelationships.ts(26,12): error TS2536: Type 'K' cannot be used to index type 'T'.
1111
mappedTypeRelationships.ts(30,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
12-
'T[keyof T]' is constrained to be a subtype of 'T[keyof T] | undefined'.
13-
Type 'undefined' is not assignable to type 'T[keyof T]'.
14-
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
12+
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'.
1513
mappedTypeRelationships.ts(35,5): error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'.
16-
'T[K]' is constrained to be a subtype of 'T[K] | undefined'.
17-
Type 'undefined' is not assignable to type 'T[K]'.
18-
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
14+
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'.
1915
mappedTypeRelationships.ts(40,5): error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
2016
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'U[keyof T] | undefined'.
2117
mappedTypeRelationships.ts(41,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
@@ -38,7 +34,7 @@ mappedTypeRelationships.ts(66,5): error TS2322: Type 'T[K]' is not assignable to
3834
'U' is constrained to be a subtype of 'T'.
3935
mappedTypeRelationships.ts(66,5): error TS2542: Index signature in type 'Readonly<U>' only permits reading.
4036
mappedTypeRelationships.ts(72,5): error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
41-
'T' is constrained to be a subtype of 'Partial<T>'.
37+
'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial<T>'.
4238
mappedTypeRelationships.ts(78,5): error TS2322: Type 'Partial<Thing>' is not assignable to type 'Partial<T>'.
4339
mappedTypeRelationships.ts(88,5): error TS2322: Type 'Readonly<Thing>' is not assignable to type 'Readonly<T>'.
4440
mappedTypeRelationships.ts(127,5): error TS2322: Type 'Partial<U>' is not assignable to type 'Identity<U>'.
@@ -128,19 +124,15 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
128124
x[k] = y[k]; // Error
129125
~~~~
130126
!!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
131-
!!! error TS2322: 'T[keyof T]' is constrained to be a subtype of 'T[keyof T] | undefined'.
132-
!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'.
133-
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
127+
!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'.
134128
y[k] = x[k];
135129
}
136130

137131
function f11<T, K extends keyof T>(x: T, y: Partial<T>, k: K) {
138132
x[k] = y[k]; // Error
139133
~~~~
140134
!!! error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'.
141-
!!! error TS2322: 'T[K]' is constrained to be a subtype of 'T[K] | undefined'.
142-
!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'.
143-
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
135+
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'.
144136
y[k] = x[k];
145137
}
146138

@@ -213,7 +205,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
213205
x = y; // Error
214206
~
215207
!!! error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
216-
!!! error TS2322: 'T' is constrained to be a subtype of 'Partial<T>'.
208+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial<T>'.
217209
y = x;
218210
}
219211

0 commit comments

Comments
 (0)