mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-05 08:11:30 -06:00
Better compatibility error for indexed access type (#51228)
This commit is contained in:
parent
1da47255fa
commit
0715fe5d65
@ -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) {
|
||||
|
||||
@ -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<Partial<T>[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<T["x"]>'.
|
||||
Type 'string | undefined' is not assignable to type 'NonNullable<T["x"]>'.
|
||||
Type 'undefined' is not assignable to type 'NonNullable<T["x"]>'.
|
||||
@ -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<Partial<T>[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<T extends { x: string | undefined }>(x: T["x"], y: NonNullable<T["x"]>) {
|
||||
|
||||
@ -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'.
|
||||
}
|
||||
}
|
||||
|
||||
@ -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<K extends string>(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<string, number>;
|
||||
@ -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
|
||||
|
||||
@ -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<T, K extends keyof T>() {
|
||||
let x: Partial<Partial<Partial<Partial<Partial<Partial<Partial<Record<keyof T, string>>>>>>>>[K] = "hello";
|
||||
}
|
||||
|
||||
// Repro from #51069
|
||||
|
||||
class Test<T extends Record<string, number>> {
|
||||
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];
|
||||
}
|
||||
}
|
||||
|
||||
@ -152,6 +152,22 @@ function f30<T, K extends keyof T>() {
|
||||
function f31<T, K extends keyof T>() {
|
||||
let x: Partial<Partial<Partial<Partial<Partial<Partial<Partial<Record<keyof T, string>>>>>>>>[K] = "hello";
|
||||
}
|
||||
|
||||
// Repro from #51069
|
||||
|
||||
class Test<T extends Record<string, number>> {
|
||||
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;
|
||||
}());
|
||||
|
||||
@ -523,3 +523,44 @@ function f31<T, K extends keyof T>() {
|
||||
>K : Symbol(K, Decl(keyofAndIndexedAccessErrors.ts, 150, 15))
|
||||
}
|
||||
|
||||
// Repro from #51069
|
||||
|
||||
class Test<T extends Record<string, number>> {
|
||||
>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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -483,3 +483,44 @@ function f31<T, K extends keyof T>() {
|
||||
>"hello" : "hello"
|
||||
}
|
||||
|
||||
// Repro from #51069
|
||||
|
||||
class Test<T extends Record<string, number>> {
|
||||
>Test : Test<T>
|
||||
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -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'.
|
||||
|
||||
@ -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<T extends null, P extends keyof T>(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<T extends undefined, P extends keyof T>(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<T extends void, P extends keyof T>(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<T extends never, P extends keyof T>(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<T extends number, P extends keyof T>(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<T extends string, P extends keyof T>(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<T extends {}, P extends keyof T>(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<T extends { a: number }, P extends keyof T>(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<T extends { [s: string]: number }, P extends keyof T>(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'.
|
||||
}
|
||||
|
||||
@ -151,3 +151,19 @@ function f30<T, K extends keyof T>() {
|
||||
function f31<T, K extends keyof T>() {
|
||||
let x: Partial<Partial<Partial<Partial<Partial<Partial<Partial<Record<keyof T, string>>>>>>>>[K] = "hello";
|
||||
}
|
||||
|
||||
// Repro from #51069
|
||||
|
||||
class Test<T extends Record<string, number>> {
|
||||
testy: T;
|
||||
|
||||
constructor(t: T) {
|
||||
this.testy = t;
|
||||
}
|
||||
|
||||
public t(key: keyof T): number {
|
||||
this.testy[key] += 1; // Error
|
||||
|
||||
return this.testy[key];
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user