Skip to content
Closed

#60881 #61143

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
869 changes: 438 additions & 431 deletions src/compiler/checker.ts

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions src/compiler/diagnosticMessages.json
Original file line number Diff line number Diff line change
Expand Up @@ -8447,5 +8447,9 @@
"String literal import and export names are not supported when the '--module' flag is set to 'es2015' or 'es2020'.": {
"category": "Error",
"code": 18057
},
"'{1}' is constrained to be a subtype of '{0}'.": {
"category": "Error",
"code": 18058
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ assignmentCompatWithCallSignatures4.ts(53,9): error TS2322: Type '(x: (arg: Base
'Base' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Base'.
assignmentCompatWithCallSignatures4.ts(58,9): error TS2322: Type '(...x: Base[]) => Base' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
Type 'Base' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
'T' is constrained to be a subtype of 'Base'.
assignmentCompatWithCallSignatures4.ts(62,9): error TS2322: Type '(x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type '<T extends Derived>(x: T, y: T) => T'.
Type 'Base' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
'T' is constrained to be a subtype of 'Base'.
assignmentCompatWithCallSignatures4.ts(66,9): error TS2322: Type '(x: Base[], y: Derived2[]) => Derived[]' is not assignable to type '<T extends Array<Derived2>>(x: Base[], y: Base[]) => T'.
Type 'Derived[]' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
'T' is constrained to be a subtype of 'Derived[]'.
assignmentCompatWithCallSignatures4.ts(69,9): error TS2322: Type '<T>(x: { a: T; b: T; }) => T' is not assignable to type '(x: { a: string; b: number; }) => number'.
Types of parameters 'x' and 'x' are incompatible.
Type '{ a: string; b: number; }' is not assignable to type '{ a: string; b: string; }'.
Expand Down Expand Up @@ -149,23 +149,23 @@ assignmentCompatWithCallSignatures4.ts(96,9): error TS2322: Type '<T>(x: T) => s
~~~
!!! error TS2322: Type '(...x: Base[]) => Base' is not assignable to type '<T extends Derived>(...x: T[]) => T'.
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
!!! error TS2322: 'T' is constrained to be a subtype of 'Base'.

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

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

var b15: <T>(x: { a: T; b: T }) => T;
a15 = b15;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ assignmentCompatWithConstructSignatures4.ts(53,9): error TS2322: Type 'new (x: (
'Base' is assignable to the constraint of type 'T', but 'T' could be instantiated with a different subtype of constraint 'Base'.
assignmentCompatWithConstructSignatures4.ts(58,9): error TS2322: Type 'new (...x: Base[]) => Base' is not assignable to type 'new <T extends Derived>(...x: T[]) => T'.
Type 'Base' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
'T' is constrained to be a subtype of 'Base'.
assignmentCompatWithConstructSignatures4.ts(62,9): error TS2322: Type 'new (x: { foo: string; }, y: { foo: string; bar: string; }) => Base' is not assignable to type 'new <T extends Derived>(x: T, y: T) => T'.
Type 'Base' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
'T' is constrained to be a subtype of 'Base'.
assignmentCompatWithConstructSignatures4.ts(66,9): error TS2322: Type 'new (x: Base[], y: Derived2[]) => Derived[]' is not assignable to type 'new <T extends Array<Derived2>>(x: Base[], y: Base[]) => T'.
Type 'Derived[]' is not assignable to type 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Derived[]'.
'T' is constrained to be a subtype of 'Derived[]'.
assignmentCompatWithConstructSignatures4.ts(69,9): error TS2322: Type 'new <T>(x: { a: T; b: T; }) => T' is not assignable to type 'new (x: { a: string; b: number; }) => number'.
Types of parameters 'x' and 'x' are incompatible.
Type '{ a: string; b: number; }' is not assignable to type '{ a: string; b: string; }'.
Expand Down Expand Up @@ -165,23 +165,23 @@ assignmentCompatWithConstructSignatures4.ts(96,9): error TS2322: Type 'new <T>(x
~~~
!!! error TS2322: Type 'new (...x: Base[]) => Base' is not assignable to type 'new <T extends Derived>(...x: T[]) => T'.
!!! error TS2322: Type 'Base' is not assignable to type 'T'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Base'.
!!! error TS2322: 'T' is constrained to be a subtype of 'Base'.

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

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

var b15: new <T>(x: { a: T; b: T }) => T;
a15 = b15; // ok
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/awaitedTypeNoLib.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ awaitedTypeNoLib.ts(18,27): error TS2345: Argument of type 'NotPromise<TResult>
Type 'TResult | (TResult extends PromiseLike<unknown> ? never : TResult)' is not assignable to type 'Thenable<TResult>'.
Type 'Thenable<unknown> & TResult' is not assignable to type 'Thenable<TResult>'.
Type 'unknown' is not assignable to type 'TResult'.
'TResult' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.
'TResult' is constrained to be a subtype of 'unknown'.


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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
better_subType_assignable_to_superType_error_messsage.ts(13,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 (1 errors) ====
function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) {
// TS 3.3: Type 'SuperType' is not assignable to type 'SubType'.
//
// TS 3.5: Type 'SuperType' is not assignable to type 'SubType'.
// 'SuperType' is assignable to the constraint of type 'SubType', but 'SubType' could be instantiated with a different subtype of constraint '{}'.
//
// TS 3.9: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
//
// TS 4.8: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
// input.tsx(1, 41): This type parameter might need an `extends V` constraint.
subType2 = superType;
~~~~~~~~
!!! 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.
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] ////

//// [better_subType_assignable_to_superType_error_messsage.ts]
function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) {
// TS 3.3: Type 'SuperType' is not assignable to type 'SubType'.
//
// TS 3.5: Type 'SuperType' is not assignable to type 'SubType'.
// 'SuperType' is assignable to the constraint of type 'SubType', but 'SubType' could be instantiated with a different subtype of constraint '{}'.
//
// TS 3.9: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
//
// TS 4.8: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
// input.tsx(1, 41): This type parameter might need an `extends V` constraint.
subType2 = superType;
}


//// [better_subType_assignable_to_superType_error_messsage.js]
function parameterExtendsOtherParameter(superType, subType2) {
// TS 3.3: Type 'SuperType' is not assignable to type 'SubType'.
//
// TS 3.5: Type 'SuperType' is not assignable to type 'SubType'.
// 'SuperType' is assignable to the constraint of type 'SubType', but 'SubType' could be instantiated with a different subtype of constraint '{}'.
//
// TS 3.9: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
//
// TS 4.8: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
// input.tsx(1, 41): This type parameter might need an `extends V` constraint.
subType2 = superType;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] ////

=== better_subType_assignable_to_superType_error_messsage.ts ===
function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, 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))
>subType2 : Symbol(subType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 125))
>SubType2 : Symbol(SubType2, Decl(better_subType_assignable_to_superType_error_messsage.ts, 0, 77))

// TS 3.3: Type 'SuperType' is not assignable to type 'SubType'.
//
// TS 3.5: Type 'SuperType' is not assignable to type 'SubType'.
// 'SuperType' is assignable to the constraint of type 'SubType', but 'SubType' could be instantiated with a different subtype of constraint '{}'.
//
// TS 3.9: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
//
// TS 4.8: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
// input.tsx(1, 41): This type parameter might need an `extends V` constraint.
subType2 = superType;
>subType2 : Symbol(subType2, 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))
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//// [tests/cases/compiler/better_subType_assignable_to_superType_error_messsage.ts] ////

=== better_subType_assignable_to_superType_error_messsage.ts ===
function parameterExtendsOtherParameter<SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) {
>parameterExtendsOtherParameter : <SuperType, SubType extends SuperType, SubType2 extends SubType>(superType: SuperType, subType2: SubType2) => void
> : ^ ^^ ^^^^^^^^^ ^^ ^^^^^^^^^ ^^ ^^ ^^ ^^ ^^^^^^^^^
>superType : SuperType
> : ^^^^^^^^^
>subType2 : SubType2
> : ^^^^^^^^

// TS 3.3: Type 'SuperType' is not assignable to type 'SubType'.
//
// TS 3.5: Type 'SuperType' is not assignable to type 'SubType'.
// 'SuperType' is assignable to the constraint of type 'SubType', but 'SubType' could be instantiated with a different subtype of constraint '{}'.
//
// TS 3.9: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
//
// TS 4.8: Type 'SuperType' is not assignable to type 'SubType'.
// 'SubType' could be instantiated with an arbitrary type which could be unrelated to 'SuperType'.
// input.tsx(1, 41): This type parameter might need an `extends V` constraint.
subType2 = superType;
>subType2 = superType : SuperType
> : ^^^^^^^^^
>subType2 : SubType2
> : ^^^^^^^^
>superType : SuperType
> : ^^^^^^^^^
}

Original file line number Diff line number Diff line change
@@ -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'.
Expand All @@ -17,15 +17,15 @@ 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.

// But error to try to climb up the chain
(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.

Expand Down
Loading
Loading