Skip to content

Commit 25375a2

Browse files
authored
Unnecessary elaboration about not being assignable to type parameters (#42952)
1 parent 408c804 commit 25375a2

22 files changed

+162
-186
lines changed

src/compiler/checker.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17229,6 +17229,7 @@ namespace ts {
1722917229
);
1723017230
}
1723117231
else {
17232+
errorInfo = undefined;
1723217233
reportError(
1723317234
Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1,
1723417235
targetType,

tests/baselines/reference/callSignatureAssignabilityInInheritance6.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSign
4545
Types of property 'a' are incompatible.
4646
Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
4747
'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.
48-
Type 'Base' is not assignable to type 'T'.
49-
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
5048

5149

5250
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSignatureAssignabilityInInheritance6.ts (7 errors) ====
@@ -152,7 +150,5 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/callSign
152150
!!! error TS2430: Types of property 'a' are incompatible.
153151
!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
154152
!!! error TS2430: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.
155-
!!! error TS2430: Type 'Base' is not assignable to type 'T'.
156-
!!! error TS2430: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
157153
a16: (x: { a: T; b: T }) => T[];
158154
}

tests/baselines/reference/conditionalTypes1.errors.txt

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,6 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(159,5): error TS2
5353
'ZeroOf<T>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
5454
Type '0 | (T extends string ? "" : false)' is not assignable to type 'T'.
5555
'T' could be instantiated with an arbitrary type which could be unrelated to '0 | (T extends string ? "" : false)'.
56-
Type 'number' is not assignable to type 'T'.
57-
'number' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
58-
Type 'string | number' is not assignable to type 'T'.
59-
'string | number' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
60-
Type 'string' is not assignable to type 'T'.
61-
'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
6256
tests/cases/conformance/types/conditional/conditionalTypes1.ts(160,5): error TS2322: Type 'T' is not assignable to type 'ZeroOf<T>'.
6357
Type 'string | number' is not assignable to type 'ZeroOf<T>'.
6458
Type 'string' is not assignable to type 'ZeroOf<T>'.
@@ -303,12 +297,6 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS
303297
!!! error TS2322: 'ZeroOf<T>' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
304298
!!! error TS2322: Type '0 | (T extends string ? "" : false)' is not assignable to type 'T'.
305299
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '0 | (T extends string ? "" : false)'.
306-
!!! error TS2322: Type 'number' is not assignable to type 'T'.
307-
!!! error TS2322: 'number' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
308-
!!! error TS2322: Type 'string | number' is not assignable to type 'T'.
309-
!!! error TS2322: 'string | number' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
310-
!!! error TS2322: Type 'string' is not assignable to type 'T'.
311-
!!! error TS2322: 'string' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'string | number'.
312300
y = x; // Error
313301
~
314302
!!! error TS2322: Type 'T' is not assignable to type 'ZeroOf<T>'.

tests/baselines/reference/constructSignatureAssignabilityInInheritance6.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/construc
4545
Types of property 'a' are incompatible.
4646
Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
4747
'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.
48-
Type 'Base' is not assignable to type 'T'.
49-
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
5048

5149

5250
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/constructSignatureAssignabilityInInheritance6.ts (7 errors) ====
@@ -152,7 +150,5 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/construc
152150
!!! error TS2430: Types of property 'a' are incompatible.
153151
!!! error TS2430: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
154152
!!! error TS2430: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.
155-
!!! error TS2430: Type 'Base' is not assignable to type 'T'.
156-
!!! error TS2430: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
157153
a16: new (x: { a: T; b: T }) => T[];
158154
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts(3,3): error TS2322: Type 'T | Yadda' is not assignable to type 'T'.
2+
'T' could be instantiated with an arbitrary type which could be unrelated to 'T | Yadda'.
3+
4+
5+
==== tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts (1 errors) ====
6+
async function foo<T>(x: T): Promise<T> {
7+
let yaddable = await getXOrYadda(x);
8+
return yaddable;
9+
~~~~~~~~~~~~~~~~
10+
!!! error TS2322: Type 'T | Yadda' is not assignable to type 'T'.
11+
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T | Yadda'.
12+
}
13+
14+
interface Yadda {
15+
stuff: string,
16+
things: string,
17+
}
18+
19+
declare function getXOrYadda<T>(x: T): T | Yadda;
20+
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//// [doNotElaborateAssignabilityToTypeParameters.ts]
2+
async function foo<T>(x: T): Promise<T> {
3+
let yaddable = await getXOrYadda(x);
4+
return yaddable;
5+
}
6+
7+
interface Yadda {
8+
stuff: string,
9+
things: string,
10+
}
11+
12+
declare function getXOrYadda<T>(x: T): T | Yadda;
13+
14+
15+
//// [doNotElaborateAssignabilityToTypeParameters.js]
16+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
17+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
18+
return new (P || (P = Promise))(function (resolve, reject) {
19+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
20+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
21+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
22+
step((generator = generator.apply(thisArg, _arguments || [])).next());
23+
});
24+
};
25+
var __generator = (this && this.__generator) || function (thisArg, body) {
26+
var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
27+
return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
28+
function verb(n) { return function (v) { return step([n, v]); }; }
29+
function step(op) {
30+
if (f) throw new TypeError("Generator is already executing.");
31+
while (_) try {
32+
if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
33+
if (y = 0, t) op = [op[0] & 2, t.value];
34+
switch (op[0]) {
35+
case 0: case 1: t = op; break;
36+
case 4: _.label++; return { value: op[1], done: false };
37+
case 5: _.label++; y = op[1]; op = [0]; continue;
38+
case 7: op = _.ops.pop(); _.trys.pop(); continue;
39+
default:
40+
if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
41+
if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
42+
if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
43+
if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
44+
if (t[2]) _.ops.pop();
45+
_.trys.pop(); continue;
46+
}
47+
op = body.call(thisArg, _);
48+
} catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
49+
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
50+
}
51+
};
52+
function foo(x) {
53+
return __awaiter(this, void 0, void 0, function () {
54+
var yaddable;
55+
return __generator(this, function (_a) {
56+
switch (_a.label) {
57+
case 0: return [4 /*yield*/, getXOrYadda(x)];
58+
case 1:
59+
yaddable = _a.sent();
60+
return [2 /*return*/, yaddable];
61+
}
62+
});
63+
});
64+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
=== tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts ===
2+
async function foo<T>(x: T): Promise<T> {
3+
>foo : Symbol(foo, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 0, 0))
4+
>T : Symbol(T, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 0, 19))
5+
>x : Symbol(x, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 0, 22))
6+
>T : Symbol(T, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 0, 19))
7+
>Promise : Symbol(Promise, Decl(lib.es5.d.ts, --, --), Decl(lib.es2015.iterable.d.ts, --, --), Decl(lib.es2015.promise.d.ts, --, --), Decl(lib.es2015.symbol.wellknown.d.ts, --, --))
8+
>T : Symbol(T, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 0, 19))
9+
10+
let yaddable = await getXOrYadda(x);
11+
>yaddable : Symbol(yaddable, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 1, 5))
12+
>getXOrYadda : Symbol(getXOrYadda, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 8, 1))
13+
>x : Symbol(x, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 0, 22))
14+
15+
return yaddable;
16+
>yaddable : Symbol(yaddable, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 1, 5))
17+
}
18+
19+
interface Yadda {
20+
>Yadda : Symbol(Yadda, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 3, 1))
21+
22+
stuff: string,
23+
>stuff : Symbol(Yadda.stuff, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 5, 17))
24+
25+
things: string,
26+
>things : Symbol(Yadda.things, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 6, 16))
27+
}
28+
29+
declare function getXOrYadda<T>(x: T): T | Yadda;
30+
>getXOrYadda : Symbol(getXOrYadda, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 8, 1))
31+
>T : Symbol(T, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 10, 29))
32+
>x : Symbol(x, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 10, 32))
33+
>T : Symbol(T, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 10, 29))
34+
>T : Symbol(T, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 10, 29))
35+
>Yadda : Symbol(Yadda, Decl(doNotElaborateAssignabilityToTypeParameters.ts, 3, 1))
36+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/doNotElaborateAssignabilityToTypeParameters.ts ===
2+
async function foo<T>(x: T): Promise<T> {
3+
>foo : <T>(x: T) => Promise<T>
4+
>x : T
5+
6+
let yaddable = await getXOrYadda(x);
7+
>yaddable : T | Yadda
8+
>await getXOrYadda(x) : T | Yadda
9+
>getXOrYadda(x) : T | Yadda
10+
>getXOrYadda : <T>(x: T) => T | Yadda
11+
>x : T
12+
13+
return yaddable;
14+
>yaddable : T | Yadda
15+
}
16+
17+
interface Yadda {
18+
stuff: string,
19+
>stuff : string
20+
21+
things: string,
22+
>things : string
23+
}
24+
25+
declare function getXOrYadda<T>(x: T): T | Yadda;
26+
>getXOrYadda : <T>(x: T) => T | Yadda
27+
>x : T
28+

tests/baselines/reference/genericCallWithObjectTypeArgsAndInitializers.errors.txt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/genericC
44
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
55
tests/cases/conformance/types/typeRelationships/assignmentCompatibility/genericCallWithObjectTypeArgsAndInitializers.ts(8,56): error TS2322: Type 'U' is not assignable to type 'V'.
66
'V' could be instantiated with an arbitrary type which could be unrelated to 'U'.
7-
Type 'T' is not assignable to type 'V'.
8-
'V' could be instantiated with an arbitrary type which could be unrelated to 'T'.
97

108

119
==== tests/cases/conformance/types/typeRelationships/assignmentCompatibility/genericCallWithObjectTypeArgsAndInitializers.ts (3 errors) ====
@@ -26,6 +24,4 @@ tests/cases/conformance/types/typeRelationships/assignmentCompatibility/genericC
2624
~~~~~~~~
2725
!!! error TS2322: Type 'U' is not assignable to type 'V'.
2826
!!! error TS2322: 'V' could be instantiated with an arbitrary type which could be unrelated to 'U'.
29-
!!! error TS2322: Type 'T' is not assignable to type 'V'.
30-
!!! error TS2322: 'V' could be instantiated with an arbitrary type which could be unrelated to 'T'.
3127
function foo7<T, U extends T, V extends U>(x: V, y: U = x) { } // should be ok

tests/baselines/reference/mappedTypeRelationships.errors.txt

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,6 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(41,5): error TS2
2020
Type 'T[string]' is not assignable to type 'U[keyof T]'.
2121
Type 'T' is not assignable to type 'U'.
2222
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
23-
Type 'T[keyof T]' is not assignable to type 'U[keyof T]'.
24-
Type 'T' is not assignable to type 'U'.
25-
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
2623
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(45,5): error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'.
2724
Type 'undefined' is not assignable to type 'T[K]'.
2825
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(46,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'.
@@ -32,12 +29,6 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(46,5): error TS2
3229
Type 'T[string]' is not assignable to type 'U[K]'.
3330
Type 'T' is not assignable to type 'U'.
3431
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
35-
Type 'T[keyof T]' is not assignable to type 'U[K]'.
36-
Type 'T' is not assignable to type 'U'.
37-
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
38-
Type 'T[K]' is not assignable to type 'U[K]'.
39-
Type 'T' is not assignable to type 'U'.
40-
'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
4132
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(51,5): error TS2542: Index signature in type 'Readonly<T>' only permits reading.
4233
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(56,5): error TS2542: Index signature in type 'Readonly<T>' only permits reading.
4334
tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(61,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'.
@@ -162,9 +153,6 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
162153
!!! error TS2322: Type 'T[string]' is not assignable to type 'U[keyof T]'.
163154
!!! error TS2322: Type 'T' is not assignable to type 'U'.
164155
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
165-
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'.
166-
!!! error TS2322: Type 'T' is not assignable to type 'U'.
167-
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
168156
}
169157

170158
function f13<T, U extends T, K extends keyof T>(x: T, y: Partial<U>, k: K) {
@@ -181,12 +169,6 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS
181169
!!! error TS2322: Type 'T[string]' is not assignable to type 'U[K]'.
182170
!!! error TS2322: Type 'T' is not assignable to type 'U'.
183171
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
184-
!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[K]'.
185-
!!! error TS2322: Type 'T' is not assignable to type 'U'.
186-
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
187-
!!! error TS2322: Type 'T[K]' is not assignable to type 'U[K]'.
188-
!!! error TS2322: Type 'T' is not assignable to type 'U'.
189-
!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'.
190172
}
191173

192174
function f20<T>(x: T, y: Readonly<T>, k: keyof T) {

0 commit comments

Comments
 (0)