diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index caab75c99fa8a..499d2dd652638 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21966,6 +21966,13 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { typeToString(constraint), ); } + else if ((source.flags & TypeFlags.TypeParameter) && (isTypeAssignableTo(target, getBaseConstraintOrType(generalizedSource)) || (needsOriginalSource = isTypeAssignableTo(target, getBaseConstraintOrType(source))))) { + reportError( + Diagnostics._1_is_constrained_to_be_a_subtype_of_0, + needsOriginalSource ? sourceType : generalizedSourceType, + targetType, + ); + } else { errorInfo = undefined; reportError( diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index be2fe3957b20a..141127ddf7467 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -3991,6 +3991,10 @@ "category": "Error", "code": 2881 }, + "'{1}' is constrained to be a subtype of '{0}'.": { + "category": "Error", + "code": 2882 + }, "Import declaration '{0}' is using private name '{1}'.": { "category": "Error", diff --git a/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.errors.txt b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.errors.txt new file mode 100644 index 0000000000000..2deec13c0e69c --- /dev/null +++ b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.errors.txt @@ -0,0 +1,25 @@ +better_subType_assignable_to_superType_error_messsage.ts(3,3): error TS2322: Type 'SuperType' is not assignable to type 'SubType'. + 'SubType' is constrained to be a subtype of 'SuperType'. +better_subType_assignable_to_superType_error_messsage.ts(6,3): error TS2322: Type 'SuperType' is not assignable to type 'SubType2'. + 'SubType2' is constrained to be a subtype of 'SuperType'. + + +==== better_subType_assignable_to_superType_error_messsage.ts (2 errors) ==== + function parameterExtendsOtherParameter(superType: SuperType, subtype: SubType, subType2: SubType2) { + //ensures that supertypes are not assignable to subtypes + subtype = superType;//error + ~~~~~~~ +!!! error TS2322: Type 'SuperType' is not assignable to type 'SubType'. +!!! error TS2322: 'SubType' is constrained to be a subtype of 'SuperType'. +!!! related TS2208 better_subType_assignable_to_superType_error_messsage.ts:1:41: This type parameter might need an `extends SubType` constraint. + + //ensures that supertypes are not assignable to 'grandchild' subtypes + subType2 = superType;//error + ~~~~~~~~ +!!! error TS2322: Type 'SuperType' is not assignable to type 'SubType2'. +!!! error TS2322: 'SubType2' is constrained to be a subtype of 'SuperType'. +!!! related TS2208 better_subType_assignable_to_superType_error_messsage.ts:1:41: This type parameter might need an `extends SubType2` constraint. + + superType = subType2;//ok + } + \ No newline at end of file diff --git a/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.js b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.js new file mode 100644 index 0000000000000..6a2cdc1dd3b3f --- /dev/null +++ b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.js @@ -0,0 +1,22 @@ +//// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] //// + +//// [better_subType_assignable_to_superType_error_messsage.ts] +function parameterExtendsOtherParameter(superType: SuperType, subtype: SubType, subType2: SubType2) { + //ensures that supertypes are not assignable to subtypes + subtype = superType;//error + + //ensures that supertypes are not assignable to 'grandchild' subtypes + subType2 = superType;//error + + superType = subType2;//ok +} + + +//// [better_subType_assignable_to_superType_error_messsage.js] +function parameterExtendsOtherParameter(superType, subtype, subType2) { + //ensures that supertypes are not assignable to subtypes + subtype = superType; //error + //ensures that supertypes are not assignable to 'grandchild' subtypes + subType2 = superType; //error + superType = subType2; //ok +} diff --git a/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.symbols b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.symbols new file mode 100644 index 0000000000000..db04cb15bb721 --- /dev/null +++ b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.symbols @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] //// + +=== better_subType_assignable_to_superType_error_messsage.ts === +function parameterExtendsOtherParameter(superType: SuperType, subtype: SubType, subType2: SubType2) { +>parameterExtendsOtherParameter : Symbol(parameterExtendsOtherParameter, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 0)) +>SuperType : Symbol(SuperType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 40)) +>SubType : Symbol(SubType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 50)) +>SuperType : Symbol(SuperType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 40)) +>SubType2 : Symbol(SubType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 77)) +>SubType : Symbol(SubType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 50)) +>superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) +>SuperType : Symbol(SuperType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 40)) +>subtype : Symbol(subtype, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 125)) +>SubType : Symbol(SubType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 50)) +>subType2 : Symbol(subType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 143)) +>SubType2 : Symbol(SubType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 77)) + + //ensures that supertypes are not assignable to subtypes + subtype = superType;//error +>subtype : Symbol(subtype, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 125)) +>superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) + + //ensures that supertypes are not assignable to 'grandchild' subtypes + subType2 = superType;//error +>subType2 : Symbol(subType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 143)) +>superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) + + superType = subType2;//ok +>superType : Symbol(superType, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 104)) +>subType2 : Symbol(subType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 143)) +} + diff --git a/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.types b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.types new file mode 100644 index 0000000000000..81c73b587ac47 --- /dev/null +++ b/tests/baselines/reference/better_subType_assignable_to_superType_error_messsage.types @@ -0,0 +1,40 @@ +//// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] //// + +=== better_subType_assignable_to_superType_error_messsage.ts === +function parameterExtendsOtherParameter(superType: SuperType, subtype: SubType, subType2: SubType2) { +>parameterExtendsOtherParameter : (superType: SuperType, subtype: SubType, subType2: SubType2) => void +> : ^ ^^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^ ^^ ^^^^^^^^^ +>superType : SuperType +> : ^^^^^^^^^ +>subtype : SubType +> : ^^^^^^^ +>subType2 : SubType2 +> : ^^^^^^^^ + + //ensures that supertypes are not assignable to subtypes + subtype = superType;//error +>subtype = superType : SuperType +> : ^^^^^^^^^ +>subtype : SubType +> : ^^^^^^^ +>superType : SuperType +> : ^^^^^^^^^ + + //ensures that supertypes are not assignable to 'grandchild' subtypes + subType2 = superType;//error +>subType2 = superType : SuperType +> : ^^^^^^^^^ +>subType2 : SubType2 +> : ^^^^^^^^ +>superType : SuperType +> : ^^^^^^^^^ + + superType = subType2;//ok +>superType = subType2 : SubType2 +> : ^^^^^^^^ +>superType : SuperType +> : ^^^^^^^^^ +>subType2 : SubType2 +> : ^^^^^^^^ +} + diff --git a/tests/baselines/reference/chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.errors.txt b/tests/baselines/reference/chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.errors.txt index 321b5a897632b..944c3d1f10704 100644 --- a/tests/baselines/reference/chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.errors.txt +++ b/tests/baselines/reference/chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.errors.txt @@ -1,7 +1,7 @@ chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts(7,49): error TS2322: Type 'T' is not assignable to type 'S'. - 'S' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'S' is constrained to be a subtype of 'T'. chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts(10,35): error TS2322: Type 'T' is not assignable to type 'S'. - 'S' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'S' is constrained to be a subtype of 'T'. chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts(32,9): error TS2322: Type 'string' is not assignable to type 'number'. chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts(36,9): error TS2322: Type 'string' is not assignable to type 'number'. chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts(37,9): error TS2322: Type 'string' is not assignable to type 'number'. @@ -17,7 +17,7 @@ chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts(37,9): error TS (new Chain(t)).then(tt => s).then(ss => t); ~ !!! error TS2322: Type 'T' is not assignable to type 'S'. -!!! error TS2322: 'S' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'S' is constrained to be a subtype of 'T'. !!! related TS2208 chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts:1:13: This type parameter might need an `extends S` constraint. !!! related TS6502 chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts:3:27: The expected type comes from the return type of this signature. @@ -25,7 +25,7 @@ chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts(37,9): error TS (new Chain(s)).then(ss => t); ~ !!! error TS2322: Type 'T' is not assignable to type 'S'. -!!! error TS2322: 'S' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'S' is constrained to be a subtype of 'T'. !!! related TS2208 chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts:1:13: This type parameter might need an `extends S` constraint. !!! related TS6502 chainedCallsWithTypeParameterConstrainedToOtherTypeParameter2.ts:3:27: The expected type comes from the return type of this signature. diff --git a/tests/baselines/reference/conditionalTypes2.errors.txt b/tests/baselines/reference/conditionalTypes2.errors.txt index de78fe2d373c3..09508f6109da5 100644 --- a/tests/baselines/reference/conditionalTypes2.errors.txt +++ b/tests/baselines/reference/conditionalTypes2.errors.txt @@ -1,9 +1,9 @@ conditionalTypes2.ts(15,5): error TS2322: Type 'Covariant' is not assignable to type 'Covariant'. Type 'A' is not assignable to type 'B'. - 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. + 'B' is constrained to be a subtype of 'A'. conditionalTypes2.ts(19,5): error TS2322: Type 'Contravariant' is not assignable to type 'Contravariant'. Type 'A' is not assignable to type 'B'. - 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. + 'B' is constrained to be a subtype of 'A'. conditionalTypes2.ts(24,5): error TS2322: Type 'Invariant' is not assignable to type 'Invariant'. Types of property 'foo' are incompatible. Type 'B extends string ? keyof B : B' is not assignable to type 'A extends string ? keyof A : A'. @@ -23,7 +23,7 @@ conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant' is not assignable Type 'A | keyof A' is not assignable to type 'B extends string ? keyof B : B'. Type 'A' is not assignable to type 'B extends string ? keyof B : B'. Type 'A' is not assignable to type 'B'. - 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. + 'B' is constrained to be a subtype of 'A'. conditionalTypes2.ts(73,12): error TS2345: Argument of type 'Extract, Bar>' is not assignable to parameter of type '{ foo: string; bat: string; }'. Type 'Extract' is not assignable to type '{ foo: string; bat: string; }'. Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'. @@ -53,7 +53,7 @@ conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2' is not assignable to type 'Covariant'. !!! error TS2322: Type 'A' is not assignable to type 'B'. -!!! error TS2322: 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. +!!! error TS2322: 'B' is constrained to be a subtype of 'A'. !!! related TS2208 conditionalTypes2.ts:13:13: This type parameter might need an `extends B` constraint. } @@ -62,7 +62,7 @@ conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2' is not assignable to type 'Contravariant'. !!! error TS2322: Type 'A' is not assignable to type 'B'. -!!! error TS2322: 'B' could be instantiated with an arbitrary type which could be unrelated to 'A'. +!!! error TS2322: 'B' is constrained to be a subtype of 'A'. !!! related TS2208 conditionalTypes2.ts:18:13: This type parameter might need an `extends B` constraint. b = a; } @@ -91,7 +91,7 @@ conditionalTypes2.ts(75,12): error TS2345: Argument of type 'Extract2(x: T, y: U = x) { } // error ~~~~~~~~ !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 genericCallWithObjectTypeArgsAndInitializers.ts:6:15: This type parameter might need an `extends U` constraint. function foo5(x: U, y: T = x) { } // ok function foo6(x: T, y: U, z: V = y) { } // error ~~~~~~~~ !!! error TS2322: Type 'U' is not assignable to type 'V'. -!!! error TS2322: 'V' could be instantiated with an arbitrary type which could be unrelated to 'U'. +!!! error TS2322: 'V' is constrained to be a subtype of 'U'. function foo7(x: V, y: U = x) { } // should be ok \ No newline at end of file diff --git a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt index 4377dad060e0e..da16a5193aede 100644 --- a/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt +++ b/tests/baselines/reference/keyofAndIndexedAccessErrors.errors.txt @@ -68,16 +68,16 @@ keyofAndIndexedAccessErrors.ts(105,9): error TS2322: Type 'T[Extract' only permits reading. mappedTypeRelationships.ts(61,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'. Type 'T' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'U' is constrained to be a subtype of 'T'. mappedTypeRelationships.ts(61,5): error TS2542: Index signature in type 'Readonly' only permits reading. mappedTypeRelationships.ts(66,5): error TS2322: Type 'T[K]' is not assignable to type 'U[K]'. Type 'T' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'U' is constrained to be a subtype of 'T'. mappedTypeRelationships.ts(66,5): error TS2542: Index signature in type 'Readonly' only permits reading. mappedTypeRelationships.ts(72,5): error TS2322: Type 'Partial' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial'. @@ -41,7 +41,7 @@ mappedTypeRelationships.ts(127,5): error TS2322: Type 'Partial' is not assign mappedTypeRelationships.ts(143,5): error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type '{ [P in keyof T]: U[P]; }'. Type 'T[P]' is not assignable to type 'U[P]'. Type 'T' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'U' is constrained to be a subtype of 'T'. mappedTypeRelationships.ts(148,5): error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type '{ [P in keyof U]: U[P]; }'. Type 'keyof U' is not assignable to type 'keyof T'. Type 'string | number | symbol' is not assignable to type 'keyof T'. @@ -70,7 +70,7 @@ mappedTypeRelationships.ts(163,5): error TS2322: Type '{ [P in K]: T[P]; }' is n mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type '{ [P in K]: U[P]; }'. Type 'T[P]' is not assignable to type 'U[P]'. Type 'T' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'U' is constrained to be a subtype of 'T'. ==== mappedTypeRelationships.ts (28 errors) ==== @@ -88,7 +88,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n ~~~~ !!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'. !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 mappedTypeRelationships.ts:9:13: This type parameter might need an `extends U` constraint. } @@ -98,7 +98,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n ~~~~ !!! error TS2322: Type 'T[K]' is not assignable to type 'U[K]'. !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 mappedTypeRelationships.ts:14:13: This type parameter might need an `extends U` constraint. } @@ -181,7 +181,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n ~~~~ !!! error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T]'. !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 mappedTypeRelationships.ts:59:14: This type parameter might need an `extends U` constraint. ~~~~ !!! error TS2542: Index signature in type 'Readonly' only permits reading. @@ -193,7 +193,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n ~~~~ !!! error TS2322: Type 'T[K]' is not assignable to type 'U[K]'. !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 mappedTypeRelationships.ts:64:14: This type parameter might need an `extends U` constraint. ~~~~ !!! error TS2542: Index signature in type 'Readonly' only permits reading. @@ -287,7 +287,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n !!! error TS2322: Type '{ [P in keyof T]: T[P]; }' is not assignable to type '{ [P in keyof T]: U[P]; }'. !!! error TS2322: Type 'T[P]' is not assignable to type 'U[P]'. !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 mappedTypeRelationships.ts:141:14: This type parameter might need an `extends U` constraint. } @@ -347,7 +347,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n !!! error TS2322: Type '{ [P in K]: T[P]; }' is not assignable to type '{ [P in K]: U[P]; }'. !!! error TS2322: Type 'T[P]' is not assignable to type 'U[P]'. !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 mappedTypeRelationships.ts:166:14: This type parameter might need an `extends U` constraint. } diff --git a/tests/baselines/reference/recursiveConditionalTypes.errors.txt b/tests/baselines/reference/recursiveConditionalTypes.errors.txt index ed33be5060593..56c443de21637 100644 --- a/tests/baselines/reference/recursiveConditionalTypes.errors.txt +++ b/tests/baselines/reference/recursiveConditionalTypes.errors.txt @@ -1,7 +1,7 @@ recursiveConditionalTypes.ts(16,11): error TS2589: Type instantiation is excessively deep and possibly infinite. recursiveConditionalTypes.ts(20,5): error TS2322: Type '__Awaited' is not assignable to type '__Awaited'. Type 'T' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'U' is constrained to be a subtype of 'T'. recursiveConditionalTypes.ts(21,5): error TS2322: Type 'T' is not assignable to type '__Awaited'. recursiveConditionalTypes.ts(22,5): error TS2322: Type '__Awaited' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to '__Awaited'. @@ -52,7 +52,7 @@ recursiveConditionalTypes.ts(169,5): error TS2322: Type 'number' is not assignab ~~ !!! error TS2322: Type '__Awaited' is not assignable to type '__Awaited'. !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 recursiveConditionalTypes.ts:18:14: This type parameter might need an `extends U` constraint. ta = tx; // Error ~~ diff --git a/tests/baselines/reference/subtypesOfTypeParameterWithConstraints.errors.txt b/tests/baselines/reference/subtypesOfTypeParameterWithConstraints.errors.txt index 5d2c021888cb3..e485fd1d9db88 100644 --- a/tests/baselines/reference/subtypesOfTypeParameterWithConstraints.errors.txt +++ b/tests/baselines/reference/subtypesOfTypeParameterWithConstraints.errors.txt @@ -1,19 +1,19 @@ subtypesOfTypeParameterWithConstraints.ts(19,5): error TS2411: Property 'foo' of type 'U' is not assignable to 'string' index type 'T'. subtypesOfTypeParameterWithConstraints.ts(19,5): error TS2416: Property 'foo' in type 'D3' is not assignable to the same property in base type 'C3'. Type 'U' is not assignable to type 'T'. - 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. + 'T' is constrained to be a subtype of 'U'. subtypesOfTypeParameterWithConstraints.ts(50,5): error TS2411: Property 'foo' of type 'U' is not assignable to 'string' index type 'T'. subtypesOfTypeParameterWithConstraints.ts(50,5): error TS2416: Property 'foo' in type 'D8' is not assignable to the same property in base type 'C3'. Type 'U' is not assignable to type 'T'. - 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. + 'T' is constrained to be a subtype of 'U'. subtypesOfTypeParameterWithConstraints.ts(67,5): error TS2411: Property 'foo' of type 'V' is not assignable to 'string' index type 'T'. subtypesOfTypeParameterWithConstraints.ts(67,5): error TS2416: Property 'foo' in type 'D11' is not assignable to the same property in base type 'C3'. Type 'V' is not assignable to type 'T'. - 'T' could be instantiated with an arbitrary type which could be unrelated to 'V'. + 'T' is constrained to be a subtype of 'V'. subtypesOfTypeParameterWithConstraints.ts(72,5): error TS2411: Property 'foo' of type 'V' is not assignable to 'string' index type 'U'. subtypesOfTypeParameterWithConstraints.ts(72,5): error TS2416: Property 'foo' in type 'D12' is not assignable to the same property in base type 'C3'. Type 'V' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'V'. + 'U' is constrained to be a subtype of 'V'. subtypesOfTypeParameterWithConstraints.ts(112,5): error TS2411: Property 'foo' of type 'U' is not assignable to 'string' index type 'T'. subtypesOfTypeParameterWithConstraints.ts(112,5): error TS2416: Property 'foo' in type 'D19' is not assignable to the same property in base type 'C3'. Type 'U' is not assignable to type 'T'. @@ -65,7 +65,7 @@ subtypesOfTypeParameterWithConstraints.ts(166,5): error TS2416: Property 'foo' i ~~~ !!! error TS2416: Property 'foo' in type 'D3' is not assignable to the same property in base type 'C3'. !!! error TS2416: Type 'U' is not assignable to type 'T'. -!!! error TS2416: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. +!!! error TS2416: 'T' is constrained to be a subtype of 'U'. !!! related TS2208 subtypesOfTypeParameterWithConstraints.ts:17:23: This type parameter might need an `extends T` constraint. } @@ -103,7 +103,7 @@ subtypesOfTypeParameterWithConstraints.ts(166,5): error TS2416: Property 'foo' i ~~~ !!! error TS2416: Property 'foo' in type 'D8' is not assignable to the same property in base type 'C3'. !!! error TS2416: Type 'U' is not assignable to type 'T'. -!!! error TS2416: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. +!!! error TS2416: 'T' is constrained to be a subtype of 'U'. } class D9 extends C3 { @@ -126,7 +126,7 @@ subtypesOfTypeParameterWithConstraints.ts(166,5): error TS2416: Property 'foo' i ~~~ !!! error TS2416: Property 'foo' in type 'D11' is not assignable to the same property in base type 'C3'. !!! error TS2416: Type 'V' is not assignable to type 'T'. -!!! error TS2416: 'T' could be instantiated with an arbitrary type which could be unrelated to 'V'. +!!! error TS2416: 'T' is constrained to be a subtype of 'V'. !!! related TS2208 subtypesOfTypeParameterWithConstraints.ts:65:37: This type parameter might need an `extends T` constraint. } @@ -138,7 +138,7 @@ subtypesOfTypeParameterWithConstraints.ts(166,5): error TS2416: Property 'foo' i ~~~ !!! error TS2416: Property 'foo' in type 'D12' is not assignable to the same property in base type 'C3'. !!! error TS2416: Type 'V' is not assignable to type 'U'. -!!! error TS2416: 'U' could be instantiated with an arbitrary type which could be unrelated to 'V'. +!!! error TS2416: 'U' is constrained to be a subtype of 'V'. !!! related TS2208 subtypesOfTypeParameterWithConstraints.ts:70:37: This type parameter might need an `extends U` constraint. } diff --git a/tests/baselines/reference/typeParameterAssignability2.errors.txt b/tests/baselines/reference/typeParameterAssignability2.errors.txt index c410f851f99dc..217ee5448afea 100644 --- a/tests/baselines/reference/typeParameterAssignability2.errors.txt +++ b/tests/baselines/reference/typeParameterAssignability2.errors.txt @@ -1,13 +1,13 @@ typeParameterAssignability2.ts(5,5): error TS2322: Type 'T' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. + 'U' is constrained to be a subtype of 'T'. typeParameterAssignability2.ts(9,5): error TS2322: Type 'U' is not assignable to type 'T'. - 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. + 'T' is constrained to be a subtype of 'U'. typeParameterAssignability2.ts(14,5): error TS2322: Type 'U' is not assignable to type 'T'. - 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. + 'T' is constrained to be a subtype of 'U'. typeParameterAssignability2.ts(17,5): error TS2322: Type 'V' is not assignable to type 'T'. - 'T' could be instantiated with an arbitrary type which could be unrelated to 'V'. + 'T' is constrained to be a subtype of 'V'. typeParameterAssignability2.ts(20,5): error TS2322: Type 'V' is not assignable to type 'U'. - 'U' could be instantiated with an arbitrary type which could be unrelated to 'V'. + 'U' is constrained to be a subtype of 'V'. typeParameterAssignability2.ts(25,5): error TS2322: Type 'U' is not assignable to type 'T'. 'U' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Date'. typeParameterAssignability2.ts(26,5): error TS2322: Type 'V' is not assignable to type 'T'. @@ -33,7 +33,7 @@ typeParameterAssignability2.ts(51,5): error TS2322: Type 'Date' is not assignabl typeParameterAssignability2.ts(55,5): error TS2322: Type 'Date' is not assignable to type 'V'. 'Date' is assignable to the constraint of type 'V', but 'V' could be instantiated with a different subtype of constraint 'Date'. typeParameterAssignability2.ts(64,5): error TS2322: Type 'U' is not assignable to type 'T'. - 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. + 'T' is constrained to be a subtype of 'U'. typeParameterAssignability2.ts(65,5): error TS2322: Type 'V' is not assignable to type 'T'. 'T' could be instantiated with an arbitrary type which could be unrelated to 'V'. typeParameterAssignability2.ts(68,5): error TS2322: Type 'V' is not assignable to type 'U'. @@ -52,7 +52,7 @@ typeParameterAssignability2.ts(71,5): error TS2322: Type 'U' is not assignable t u = t; // ok ~ !!! error TS2322: Type 'T' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'T'. +!!! error TS2322: 'U' is constrained to be a subtype of 'T'. !!! related TS2208 typeParameterAssignability2.ts:3:14: This type parameter might need an `extends U` constraint. } @@ -60,7 +60,7 @@ typeParameterAssignability2.ts(71,5): error TS2322: Type 'U' is not assignable t t = u; // error ~ !!! error TS2322: Type 'U' is not assignable to type 'T'. -!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. +!!! error TS2322: 'T' is constrained to be a subtype of 'U'. !!! related TS2208 typeParameterAssignability2.ts:8:28: This type parameter might need an `extends T` constraint. u = t; // ok } @@ -69,20 +69,20 @@ typeParameterAssignability2.ts(71,5): error TS2322: Type 'U' is not assignable t t = u; // error ~ !!! error TS2322: Type 'U' is not assignable to type 'T'. -!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. +!!! error TS2322: 'T' is constrained to be a subtype of 'U'. u = t; t = v; // error ~ !!! error TS2322: Type 'V' is not assignable to type 'T'. -!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'V'. +!!! error TS2322: 'T' is constrained to be a subtype of 'V'. !!! related TS2208 typeParameterAssignability2.ts:13:41: This type parameter might need an `extends T` constraint. v = t; // ok u = v; // error ~ !!! error TS2322: Type 'V' is not assignable to type 'U'. -!!! error TS2322: 'U' could be instantiated with an arbitrary type which could be unrelated to 'V'. +!!! error TS2322: 'U' is constrained to be a subtype of 'V'. !!! related TS2208 typeParameterAssignability2.ts:13:41: This type parameter might need an `extends U` constraint. v = u; // ok } @@ -166,7 +166,7 @@ typeParameterAssignability2.ts(71,5): error TS2322: Type 'U' is not assignable t t = u; // error ~ !!! error TS2322: Type 'U' is not assignable to type 'T'. -!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'U'. +!!! error TS2322: 'T' is constrained to be a subtype of 'U'. !!! related TS2208 typeParameterAssignability2.ts:63:28: This type parameter might need an `extends T` constraint. t = v; // error ~ diff --git a/tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts b/tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts new file mode 100644 index 0000000000000..a4ce5d08f6c0e --- /dev/null +++ b/tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts @@ -0,0 +1,9 @@ +function parameterExtendsOtherParameter(superType: SuperType, subtype: SubType, subType2: SubType2) { + //ensures that supertypes are not assignable to subtypes + subtype = superType;//error + + //ensures that supertypes are not assignable to 'grandchild' subtypes + subType2 = superType;//error + + superType = subType2;//ok +}