Skip to content

Commit

Permalink
new approach and all changed tests reviewed
Browse files Browse the repository at this point in the history
  • Loading branch information
NamHaiBui committed Feb 14, 2025
1 parent 65fdfc1 commit 4db251b
Show file tree
Hide file tree
Showing 22 changed files with 62 additions and 108 deletions.
4 changes: 2 additions & 2 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21965,8 +21965,8 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
typeToString(constraint),
);
}
// check if target is a descendant of the source
else if ((isGenericType(target) && isGenericType(source)) &&( isTypeAssignableTo(target, getBaseConstraintOrType(generalizedSource)) || (needsOriginalSource = isTypeAssignableTo(target, getBaseConstraintOrType(source))))) {

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,
Expand Down
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' is constrained to be a subtype of 'Base'.
'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Base'.
'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Derived[]'.
'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Base'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Base'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Derived[]'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Base'.
'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Base'.
'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Derived[]'.
'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Base'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Base'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'Derived[]'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'unknown'.
'TResult' could be instantiated with an arbitrary type which could be unrelated to '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' is constrained to be a subtype of 'unknown'.
!!! error TS2345: 'TResult' could be instantiated with an arbitrary type which could be unrelated to 'unknown'.
}
}

Expand Down
8 changes: 4 additions & 4 deletions tests/baselines/reference/conditionalTypes1.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ conditionalTypes1.ts(29,5): error TS2322: Type 'T["x"]' is not assignable to typ
Type 'string | undefined' is not assignable to type '{}'.
Type 'undefined' is not assignable to type '{}'.
conditionalTypes1.ts(103,5): error TS2322: Type 'FunctionProperties<T>' is not assignable to type 'T'.
'T' is constrained to be a subtype of 'FunctionProperties<T>'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'FunctionProperties<T>'.
conditionalTypes1.ts(104,5): error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'T'.
'T' is constrained to be a subtype of 'NonFunctionProperties<T>'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'NonFunctionProperties<T>'.
conditionalTypes1.ts(106,5): error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'FunctionProperties<T>'.
Type 'FunctionPropertyNames<T>' is not assignable to type 'NonFunctionPropertyNames<T>'.
Type 'keyof T' is not assignable to type 'T[keyof T] extends Function ? never : keyof T'.
Expand Down Expand Up @@ -212,11 +212,11 @@ conditionalTypes1.ts(288,43): error TS2322: Type 'T95<U>' is not assignable to t
x = y; // Error
~
!!! error TS2322: Type 'FunctionProperties<T>' is not assignable to type 'T'.
!!! error TS2322: 'T' is constrained to be a subtype of 'FunctionProperties<T>'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'FunctionProperties<T>'.
x = z; // Error
~
!!! error TS2322: Type 'NonFunctionProperties<T>' is not assignable to type 'T'.
!!! error TS2322: 'T' is constrained to be a subtype of 'NonFunctionProperties<T>'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'NonFunctionProperties<T>'.
y = x;
y = z; // Error
~
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/deeplyNestedMappedTypes.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ deeplyNestedMappedTypes.ts(69,5): error TS2322: Type '{ level1: { level2: { foo:
The types of 'level1.level2' are incompatible between these types.
Property 'bar' is missing in type '{ foo: string; }' but required in type '{ foo: string; bar: string; }'.
deeplyNestedMappedTypes.ts(73,5): error TS2322: Type '{ level1: { level2: { foo: string; }; }; }[]' is not assignable to type 'T'.
'T' is constrained to be a subtype of '{ level1: { level2: { foo: string; }; }; }[]'.
'T' could be instantiated with an arbitrary type which could be unrelated to '{ level1: { level2: { foo: string; }; }; }[]'.
deeplyNestedMappedTypes.ts(77,5): error TS2322: Type '{ level1: { level2: { foo: string; }; }; }[]' is not assignable to type '{ level1: { level2: { foo: string; bar: string; }; }; }[]'.
Type '{ level1: { level2: { foo: string; }; }; }' is not assignable to type '{ level1: { level2: { foo: string; bar: string; }; }; }'.
The types of 'level1.level2' are incompatible between these types.
Expand Down Expand Up @@ -106,7 +106,7 @@ deeplyNestedMappedTypes.ts(77,5): error TS2322: Type '{ level1: { level2: { foo:
return ors; // Error
~~~~~~
!!! error TS2322: Type '{ level1: { level2: { foo: string; }; }; }[]' is not assignable to type 'T'.
!!! error TS2322: 'T' is constrained to be a subtype of '{ level1: { level2: { foo: string; }; }; }[]'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to '{ level1: { level2: { foo: string; }; }; }[]'.
}

function problematicFunction3(ors: (typeof Input.static)[]): Output[] {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
incompatibleAssignmentOfIdenticallyNamedTypes.ts(6,9): error TS2719: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
'T' is constrained to be a subtype of 'T'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.


==== incompatibleAssignmentOfIdenticallyNamedTypes.ts (1 errors) ====
Expand All @@ -11,7 +11,7 @@ incompatibleAssignmentOfIdenticallyNamedTypes.ts(6,9): error TS2719: Type 'T' is
this.x = a;
~~~~~~
!!! error TS2719: Type 'T' is not assignable to type 'T'. Two different types with this name exist, but they are unrelated.
!!! error TS2719: 'T' is constrained to be a subtype of 'T'.
!!! error TS2719: 'T' could be instantiated with an arbitrary type which could be unrelated to 'T'.
}
}

12 changes: 2 additions & 10 deletions tests/baselines/reference/indexedAccessRelation.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,7 @@ indexedAccessRelation.ts(16,23): error TS2345: Argument of type '{ a: T; }' is n
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"]'.
'S["a"]' is constrained to be a subtype of 'Foo'.
Type 'T' is not assignable to type 'S["a"]'.
'S["a"]' is constrained to be a subtype of 'T'.
Type 'Foo' is not assignable to type 'S["a"]'.
'S["a"]' is constrained to be a subtype of 'Foo'.
'S["a"]' could be instantiated with an arbitrary type which could be unrelated to 'Foo'.


==== indexedAccessRelation.ts (1 errors) ====
Expand All @@ -33,11 +29,7 @@ indexedAccessRelation.ts(16,23): error TS2345: Argument of type '{ a: T; }' is n
!!! 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: 'S["a"]' is constrained to be a subtype of 'Foo'.
!!! error TS2345: Type 'T' is not assignable to type 'S["a"]'.
!!! error TS2345: 'S["a"]' is constrained to be a subtype of 'T'.
!!! error TS2345: Type 'Foo' is not assignable to type 'S["a"]'.
!!! error TS2345: 'S["a"]' is constrained to be a subtype of 'Foo'.
!!! error TS2345: 'S["a"]' could be instantiated with an arbitrary type which could be unrelated to 'Foo'.
}
}

20 changes: 6 additions & 14 deletions tests/baselines/reference/mappedTypeRelationships.errors.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,9 @@ mappedTypeRelationships.ts(21,12): error TS2536: Type 'keyof U' cannot be used t
mappedTypeRelationships.ts(25,5): error TS2536: Type 'K' cannot be used to index type 'T'.
mappedTypeRelationships.ts(26,12): error TS2536: Type 'K' cannot be used to index type 'T'.
mappedTypeRelationships.ts(30,5): error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
'T[keyof T]' is constrained to be a subtype of 'T[keyof T] | undefined'.
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'.
'T[keyof T]' could be instantiated with an arbitrary type which could be unrelated to 'T[keyof T] | undefined'.
mappedTypeRelationships.ts(35,5): error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'.
'T[K]' is constrained to be a subtype of 'T[K] | undefined'.
Type 'undefined' is not assignable to type 'T[K]'.
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'undefined'.
'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'.
mappedTypeRelationships.ts(40,5): error TS2322: Type 'U[keyof T] | 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'.
mappedTypeRelationships.ts(41,5): error TS2322: Type 'T[keyof T]' is not assignable to type 'U[keyof T] | undefined'.
Expand All @@ -38,7 +34,7 @@ mappedTypeRelationships.ts(66,5): error TS2322: Type 'T[K]' is not assignable to
'U' is constrained to be a subtype of 'T'.
mappedTypeRelationships.ts(66,5): error TS2542: Index signature in type 'Readonly<U>' only permits reading.
mappedTypeRelationships.ts(72,5): error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
'T' is constrained to be a subtype of 'Partial<T>'.
'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial<T>'.
mappedTypeRelationships.ts(78,5): error TS2322: Type 'Partial<Thing>' is not assignable to type 'Partial<T>'.
mappedTypeRelationships.ts(88,5): error TS2322: Type 'Readonly<Thing>' is not assignable to type 'Readonly<T>'.
mappedTypeRelationships.ts(127,5): error TS2322: Type 'Partial<U>' is not assignable to type 'Identity<U>'.
Expand Down Expand Up @@ -128,19 +124,15 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
x[k] = y[k]; // Error
~~~~
!!! error TS2322: Type 'T[keyof T] | undefined' is not assignable to type 'T[keyof T]'.
!!! error TS2322: 'T[keyof T]' is constrained to be a subtype of 'T[keyof T] | undefined'.
!!! 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'.
!!! 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];
}

function f11<T, K extends keyof T>(x: T, y: Partial<T>, k: K) {
x[k] = y[k]; // Error
~~~~
!!! error TS2322: Type 'T[K] | undefined' is not assignable to type 'T[K]'.
!!! error TS2322: 'T[K]' is constrained to be a subtype of 'T[K] | undefined'.
!!! 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 'undefined'.
!!! error TS2322: 'T[K]' could be instantiated with an arbitrary type which could be unrelated to 'T[K] | undefined'.
y[k] = x[k];
}

Expand Down Expand Up @@ -213,7 +205,7 @@ mappedTypeRelationships.ts(168,5): error TS2322: Type '{ [P in K]: T[P]; }' is n
x = y; // Error
~
!!! error TS2322: Type 'Partial<T>' is not assignable to type 'T'.
!!! error TS2322: 'T' is constrained to be a subtype of 'Partial<T>'.
!!! error TS2322: 'T' could be instantiated with an arbitrary type which could be unrelated to 'Partial<T>'.
y = x;
}

Expand Down
Loading

0 comments on commit 4db251b

Please sign in to comment.