From 0715fe5d659f581de923452cc6b3bcbbcb950e94 Mon Sep 17 00:00:00 2001 From: Matias Osorio <65029771+matiasosorio1999@users.noreply.github.com> Date: Wed, 1 Feb 2023 15:11:41 -0300 Subject: [PATCH] Better compatibility error for indexed access type (#51228) --- src/compiler/checker.ts | 43 +++++++++++-------- .../reference/conditionalTypes1.errors.txt | 2 + .../indexedAccessRelation.errors.txt | 6 +-- .../keyofAndIndexedAccess2.errors.txt | 10 +++++ .../keyofAndIndexedAccessErrors.errors.txt | 29 ++++++++++++- .../reference/keyofAndIndexedAccessErrors.js | 27 ++++++++++++ .../keyofAndIndexedAccessErrors.symbols | 41 ++++++++++++++++++ .../keyofAndIndexedAccessErrors.types | 41 ++++++++++++++++++ .../mappedTypeRelationships.errors.txt | 16 +++---- ...tiveConstraintOfIndexAccessType.errors.txt | 22 +++++++++- .../keyof/keyofAndIndexedAccessErrors.ts | 16 +++++++ 11 files changed, 219 insertions(+), 34 deletions(-) diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 2e539ee75a3..4016b4435de 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -20204,26 +20204,31 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { generalizedSourceType = getTypeNameForErrorDisplay(generalizedSource); } - if (target.flags & TypeFlags.TypeParameter && target !== markerSuperTypeForCheck && target !== markerSubTypeForCheck) { - const constraint = getBaseConstraintOfType(target); - let needsOriginalSource; - if (constraint && (isTypeAssignableTo(generalizedSource, constraint) || (needsOriginalSource = isTypeAssignableTo(source, constraint)))) { - reportError( - Diagnostics._0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2, - needsOriginalSource ? sourceType : generalizedSourceType, - targetType, - typeToString(constraint), - ); + // If `target` is of indexed access type (And `source` it is not), we use the object type of `target` for better error reporting + const targetFlags = target.flags & TypeFlags.IndexedAccess && !(source.flags & TypeFlags.IndexedAccess) ? + (target as IndexedAccessType).objectType.flags : + target.flags; + + if (targetFlags & TypeFlags.TypeParameter && target !== markerSuperTypeForCheck && target !== markerSubTypeForCheck) { + const constraint = getBaseConstraintOfType(target); + let needsOriginalSource; + if (constraint && (isTypeAssignableTo(generalizedSource, constraint) || (needsOriginalSource = isTypeAssignableTo(source, constraint)))) { + reportError( + Diagnostics._0_is_assignable_to_the_constraint_of_type_1_but_1_could_be_instantiated_with_a_different_subtype_of_constraint_2, + needsOriginalSource ? sourceType : generalizedSourceType, + targetType, + typeToString(constraint), + ); + } + else { + errorInfo = undefined; + reportError( + Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1, + targetType, + generalizedSourceType + ); + } } - else { - errorInfo = undefined; - reportError( - Diagnostics._0_could_be_instantiated_with_an_arbitrary_type_which_could_be_unrelated_to_1, - targetType, - generalizedSourceType - ); - } - } if (!message) { if (relation === comparableRelation) { diff --git a/tests/baselines/reference/conditionalTypes1.errors.txt b/tests/baselines/reference/conditionalTypes1.errors.txt index e160259a662..eac67704de7 100644 --- a/tests/baselines/reference/conditionalTypes1.errors.txt +++ b/tests/baselines/reference/conditionalTypes1.errors.txt @@ -11,6 +11,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(17,5): error TS23 tests/cases/conformance/types/conditional/conditionalTypes1.ts(24,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'NonNullable[keyof T]>'. Type 'undefined' is not assignable to type 'T[keyof T] & {}'. Type 'undefined' is not assignable to type 'T[keyof T]'. + 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'. tests/cases/conformance/types/conditional/conditionalTypes1.ts(29,5): error TS2322: Type 'T["x"]' is not assignable to type 'NonNullable'. Type 'string | undefined' is not assignable to type 'NonNullable'. Type 'undefined' is not assignable to type 'NonNullable'. @@ -110,6 +111,7 @@ tests/cases/conformance/types/conditional/conditionalTypes1.ts(288,43): error TS !!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'NonNullable[keyof T]>'. !!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T] & {}'. !!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'. +!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'. } function f4(x: T["x"], y: NonNullable) { diff --git a/tests/baselines/reference/indexedAccessRelation.errors.txt b/tests/baselines/reference/indexedAccessRelation.errors.txt index 84e65df856d..22d90aeacad 100644 --- a/tests/baselines/reference/indexedAccessRelation.errors.txt +++ b/tests/baselines/reference/indexedAccessRelation.errors.txt @@ -3,8 +3,7 @@ tests/cases/compiler/indexedAccessRelation.ts(16,23): error TS2345: Argument of Type 'T' is not assignable to type 'S["a"] & T'. Type 'Foo' is not assignable to type 'S["a"] & T'. Type 'Foo' is not assignable to type 'S["a"]'. - Type 'T' is not assignable to type 'S["a"]'. - Type 'Foo' is not assignable to type 'S["a"]'. + 'S["a"]' could be instantiated with an arbitrary type which could be unrelated to 'Foo'. ==== tests/cases/compiler/indexedAccessRelation.ts (1 errors) ==== @@ -30,8 +29,7 @@ tests/cases/compiler/indexedAccessRelation.ts(16,23): error TS2345: Argument of !!! error TS2345: Type 'T' is not assignable to type 'S["a"] & T'. !!! error TS2345: Type 'Foo' is not assignable to type 'S["a"] & T'. !!! error TS2345: Type 'Foo' is not assignable to type 'S["a"]'. -!!! error TS2345: Type 'T' is not assignable to type 'S["a"]'. -!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"]'. +!!! error TS2345: 'S["a"]' could be instantiated with an arbitrary type which could be unrelated to 'Foo'. } } \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt index 510f6fc6359..45b1aef2124 100644 --- a/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccess2.errors.txt @@ -13,17 +13,22 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(19,5): error TS232 '{ [key: string]: number; }' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint '{ [key: string]: number; }'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(26,7): error TS2339: Property 'x' does not exist on type 'T'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(27,5): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. + 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(31,5): error TS2322: Type '{ [key: string]: number; }' is not assignable to type '{ [P in K]: number; }'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(38,5): error TS2322: Type '{ [x: string]: number; }' is not assignable to type '{ [P in K]: number; }'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(50,3): error TS7053: Element implicitly has an 'any' type because expression of type 'string' can't be used to index type 'Item'. No index signature with a parameter of type 'string' was found on type 'Item'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(51,3): error TS2322: Type 'number' is not assignable to type 'never'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(52,3): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. + 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'number'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(53,3): error TS2322: Type 'number' is not assignable to type 'T[K]'. + 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'number'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(65,7): error TS2339: Property 'foo' does not exist on type 'T'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(66,3): error TS2536: Type 'string' cannot be used to index type 'T'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(67,3): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. + 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(68,3): error TS2322: Type 'number' is not assignable to type 'T[K]'. + 'number' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'number'. tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS2322: Type '123' is not assignable to type 'Type[K]'. Type 'number' is not assignable to type 'never'. @@ -83,6 +88,7 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23 c[k] = 1; // Error, cannot write to index signature through constraint ~~~~ !!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. +!!! error TS2322: 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'. } function f3(a: { [P in K]: number }, b: { [key: string]: number }, k: K) { @@ -119,9 +125,11 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23 obj[k3] = 123; // Error ~~~~~~~ !!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. +!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'number'. obj[k4] = 123; // Error ~~~~~~~ !!! error TS2322: Type 'number' is not assignable to type 'T[K]'. +!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'number'. } type Dict = Record; @@ -142,9 +150,11 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccess2.ts(108,5): error TS23 obj[k2] = 123; // Error ~~~~~~~ !!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. +!!! error TS2322: 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'. obj[k3] = 123; // Error ~~~~~~~ !!! error TS2322: Type 'number' is not assignable to type 'T[K]'. +!!! error TS2322: 'number' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'number'. } // Repro from #27895 diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt index b688b579406..5044485d1e1 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt @@ -62,11 +62,16 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(117,5): error tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(122,5): error TS2322: Type 'number' is not assignable to type 'keyof T'. tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(123,5): error TS2322: Type 'string' is not assignable to type 'keyof T'. tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(140,5): error TS2322: Type 'number' is not assignable to type 'T[K]'. + 'number' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'any'. tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(141,5): error TS2322: Type 'string' is not assignable to type 'T[K]'. + 'string' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'any'. tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(142,5): error TS2322: Type 'number[]' is not assignable to type 'T[K]'. + 'number[]' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'any'. +tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(165,5): error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. + 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'. -==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (40 errors) ==== +==== tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts (41 errors) ==== class Shape { name: string; width: number; @@ -312,12 +317,15 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(142,5): error t[k] = 42; // Error ~~~~ !!! error TS2322: Type 'number' is not assignable to type 'T[K]'. +!!! error TS2322: 'number' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'any'. t[k] = "hello"; // Error ~~~~ !!! error TS2322: Type 'string' is not assignable to type 'T[K]'. +!!! error TS2322: 'string' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'any'. t[k] = [10, 20]; // Error ~~~~ !!! error TS2322: Type 'number[]' is not assignable to type 'T[K]'. +!!! error TS2322: 'number[]' is assignable to the constraint of type 'T[K]', but 'T[K]' could be instantiated with a different subtype of constraint 'any'. } // Repro from #28839 @@ -329,4 +337,23 @@ tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts(142,5): error function f31() { let x: Partial>>>>>>>[K] = "hello"; } + + // Repro from #51069 + + class Test> { + testy: T; + + constructor(t: T) { + this.testy = t; + } + + public t(key: keyof T): number { + this.testy[key] += 1; // Error + ~~~~~~~~~~~~~~~ +!!! error TS2322: Type 'number' is not assignable to type 'T[keyof T]'. +!!! error TS2322: 'number' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'number'. + + return this.testy[key]; + } + } \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.js b/tests/baselines/reference/keyofAndIndexedAccessErrors.js index 864cd0731ac..cc15d9916c4 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.js +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.js @@ -152,6 +152,22 @@ function f30() { function f31() { let x: Partial>>>>>>>[K] = "hello"; } + +// Repro from #51069 + +class Test> { + testy: T; + + constructor(t: T) { + this.testy = t; + } + + public t(key: keyof T): number { + this.testy[key] += 1; // Error + + return this.testy[key]; + } +} //// [keyofAndIndexedAccessErrors.js] @@ -232,3 +248,14 @@ function f30() { function f31() { var x = "hello"; } +// Repro from #51069 +var Test = /** @class */ (function () { + function Test(t) { + this.testy = t; + } + Test.prototype.t = function (key) { + this.testy[key] += 1; // Error + return this.testy[key]; + }; + return Test; +}()); diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols b/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols index 973e68dccd2..e7e3091010d 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.symbols @@ -523,3 +523,44 @@ function f31() { >K : Symbol(K, Decl(keyofAndIndexedAccessErrors.ts, 150, 15)) } +// Repro from #51069 + +class Test> { +>Test : Symbol(Test, Decl(keyofAndIndexedAccessErrors.ts, 152, 1)) +>T : Symbol(T, Decl(keyofAndIndexedAccessErrors.ts, 156, 11)) +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) + + testy: T; +>testy : Symbol(Test.testy, Decl(keyofAndIndexedAccessErrors.ts, 156, 46)) +>T : Symbol(T, Decl(keyofAndIndexedAccessErrors.ts, 156, 11)) + + constructor(t: T) { +>t : Symbol(t, Decl(keyofAndIndexedAccessErrors.ts, 159, 14)) +>T : Symbol(T, Decl(keyofAndIndexedAccessErrors.ts, 156, 11)) + + this.testy = t; +>this.testy : Symbol(Test.testy, Decl(keyofAndIndexedAccessErrors.ts, 156, 46)) +>this : Symbol(Test, Decl(keyofAndIndexedAccessErrors.ts, 152, 1)) +>testy : Symbol(Test.testy, Decl(keyofAndIndexedAccessErrors.ts, 156, 46)) +>t : Symbol(t, Decl(keyofAndIndexedAccessErrors.ts, 159, 14)) + } + + public t(key: keyof T): number { +>t : Symbol(Test.t, Decl(keyofAndIndexedAccessErrors.ts, 161, 3)) +>key : Symbol(key, Decl(keyofAndIndexedAccessErrors.ts, 163, 11)) +>T : Symbol(T, Decl(keyofAndIndexedAccessErrors.ts, 156, 11)) + + this.testy[key] += 1; // Error +>this.testy : Symbol(Test.testy, Decl(keyofAndIndexedAccessErrors.ts, 156, 46)) +>this : Symbol(Test, Decl(keyofAndIndexedAccessErrors.ts, 152, 1)) +>testy : Symbol(Test.testy, Decl(keyofAndIndexedAccessErrors.ts, 156, 46)) +>key : Symbol(key, Decl(keyofAndIndexedAccessErrors.ts, 163, 11)) + + return this.testy[key]; +>this.testy : Symbol(Test.testy, Decl(keyofAndIndexedAccessErrors.ts, 156, 46)) +>this : Symbol(Test, Decl(keyofAndIndexedAccessErrors.ts, 152, 1)) +>testy : Symbol(Test.testy, Decl(keyofAndIndexedAccessErrors.ts, 156, 46)) +>key : Symbol(key, Decl(keyofAndIndexedAccessErrors.ts, 163, 11)) + } +} + diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.types b/tests/baselines/reference/keyofAndIndexedAccessErrors.types index ed449ab1e3e..dae96b4ee38 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.types +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.types @@ -483,3 +483,44 @@ function f31() { >"hello" : "hello" } +// Repro from #51069 + +class Test> { +>Test : Test + + testy: T; +>testy : T + + constructor(t: T) { +>t : T + + this.testy = t; +>this.testy = t : T +>this.testy : T +>this : this +>testy : T +>t : T + } + + public t(key: keyof T): number { +>t : (key: keyof T) => number +>key : keyof T + + this.testy[key] += 1; // Error +>this.testy[key] += 1 : number +>this.testy[key] : T[keyof T] +>this.testy : T +>this : this +>testy : T +>key : keyof T +>1 : 1 + + return this.testy[key]; +>this.testy[key] : T[keyof T] +>this.testy : T +>this : this +>testy : T +>key : keyof T + } +} + diff --git a/tests/baselines/reference/mappedTypeRelationships.errors.txt b/tests/baselines/reference/mappedTypeRelationships.errors.txt index 33c20572d12..1abc27cd245 100644 --- a/tests/baselines/reference/mappedTypeRelationships.errors.txt +++ b/tests/baselines/reference/mappedTypeRelationships.errors.txt @@ -9,16 +9,16 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(21,12): error TS tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(25,5): error TS2536: Type 'K' cannot be used to index type 'T'. tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(26,12): error TS2536: Type 'K' cannot be used to index type 'T'. tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(30,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'. - Type 'undefined' is not assignable to type 'T[keyof T]'. + 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'. tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(35,5): error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'. - Type 'undefined' is not assignable to type 'T[K]'. + 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'. tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(40,5): error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'. - Type 'undefined' is not assignable to type 'T[keyof T]'. + 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'U[keyof T] | undefined'. tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(41,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'. Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[keyof T] | undefined'. Type 'T[string]' is not assignable to type 'U[keyof T] | undefined'. tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(45,5): error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'. - Type 'undefined' is not assignable to type 'T[K]'. + 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'U[K] | undefined'. tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(46,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'. Type 'T[keyof T]' is not assignable to type 'U[K] | undefined'. Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'U[K] | undefined'. @@ -124,7 +124,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS x[k] = y[k]; // Error ~~~~ !!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'. -!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'. +!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'. y[k] = x[k]; } @@ -132,7 +132,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS x[k] = y[k]; // Error ~~~~ !!! error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'. -!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'. +!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'. y[k] = x[k]; } @@ -140,7 +140,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS x[k] = y[k]; // Error ~~~~ !!! error TS2322: Type 'U[keyof T] | undefined' is not assignable to type 'T[keyof T]'. -!!! error TS2322: Type 'undefined' is not assignable to type 'T[keyof T]'. +!!! error TS2322: 'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'U[keyof T] | undefined'. y[k] = x[k]; // Error ~~~~ !!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'. @@ -152,7 +152,7 @@ tests/cases/conformance/types/mapped/mappedTypeRelationships.ts(168,5): error TS x[k] = y[k]; // Error ~~~~ !!! error TS2322: Type 'U[K] | undefined' is not assignable to type 'T[K]'. -!!! error TS2322: Type 'undefined' is not assignable to type 'T[K]'. +!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'U[K] | undefined'. y[k] = x[k]; // Error ~~~~ !!! error TS2322: Type 'T[K]' is not assignable to type 'U[K] | undefined'. diff --git a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt index 14c97cb148c..e698b55d897 100644 --- a/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt +++ b/tests/baselines/reference/nonPrimitiveConstraintOfIndexAccessType.errors.txt @@ -1,14 +1,23 @@ tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(3,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(6,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(9,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(12,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(15,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. - Type 'string' is not assignable to type 'never'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(18,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(21,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(24,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(27,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts(30,5): error TS2322: Type 'string' is not assignable to type 'T[P]'. + 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. ==== tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessType.ts (10 errors) ==== @@ -17,51 +26,60 @@ tests/cases/conformance/types/nonPrimitive/nonPrimitiveConstraintOfIndexAccessTy tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function g(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function h(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function i(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function j(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. -!!! error TS2322: Type 'string' is not assignable to type 'never'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function k(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function o(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function l(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function m(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } function n(s: string, tp: T[P]): void { tp = s; ~~ !!! error TS2322: Type 'string' is not assignable to type 'T[P]'. +!!! error TS2322: 'T[P]' could be instantiated with an arbitrary type which could be unrelated to 'string'. } \ No newline at end of file diff --git a/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts b/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts index a8666841c85..2e77b82ecdc 100644 --- a/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts +++ b/tests/cases/conformance/types/keyof/keyofAndIndexedAccessErrors.ts @@ -151,3 +151,19 @@ function f30() { function f31() { let x: Partial>>>>>>>[K] = "hello"; } + +// Repro from #51069 + +class Test> { + testy: T; + + constructor(t: T) { + this.testy = t; + } + + public t(key: keyof T): number { + this.testy[key] += 1; // Error + + return this.testy[key]; + } +}