Skip to content

Commit 63c92d8

Browse files
committed
Type comparison when conditional type is target
1 parent 7726464 commit 63c92d8

8 files changed

+662
-325
lines changed

src/compiler/checker.ts

+72-57
Large diffs are not rendered by default.

src/compiler/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -4720,7 +4720,9 @@ namespace ts {
47204720
/* @internal */
47214721
resolvedInferredTrueType?: Type; // The `trueType` instantiated with the `combinedMapper`, if present
47224722
/* @internal */
4723-
resolvedDefaultConstraint?: Type;
4723+
resolvedDefaultConstraintForReading?: Type;
4724+
/* @internal */
4725+
resolvedDefaultConstraintForWriting?: Type;
47244726
/* @internal */
47254727
mapper?: TypeMapper;
47264728
/* @internal */

tests/baselines/reference/circularlyConstrainedMappedTypeContainingConditionalNoInfiniteInstantiationDepth.errors.txt

+192-86
Large diffs are not rendered by default.

tests/baselines/reference/conditionalTypes1.errors.txt

+120-18
Large diffs are not rendered by default.

tests/baselines/reference/conditionalTypes2.errors.txt

+1-53
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,3 @@
1-
tests/cases/conformance/types/conditional/conditionalTypes2.ts(15,5): error TS2322: Type 'Covariant<A>' is not assignable to type 'Covariant<B>'.
2-
Type 'A' is not assignable to type 'B'.
3-
'A' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint '{}'.
4-
tests/cases/conformance/types/conditional/conditionalTypes2.ts(19,5): error TS2322: Type 'Contravariant<B>' is not assignable to type 'Contravariant<A>'.
5-
Type 'A' is not assignable to type 'B'.
6-
'A' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint '{}'.
7-
tests/cases/conformance/types/conditional/conditionalTypes2.ts(24,5): error TS2322: Type 'Invariant<B>' is not assignable to type 'Invariant<A>'.
8-
Types of property 'foo' are incompatible.
9-
Type 'B extends string ? keyof B : B' is not assignable to type 'A extends string ? keyof A : A'.
10-
Type 'keyof B' is not assignable to type 'keyof A'.
11-
Type 'string | number | symbol' is not assignable to type 'keyof A'.
12-
Type 'string' is not assignable to type 'keyof A'.
13-
Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
14-
Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
15-
Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
16-
Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
17-
Type 'keyof B' is not assignable to type '"valueOf"'.
18-
Type 'string | number | symbol' is not assignable to type '"valueOf"'.
19-
Type 'string' is not assignable to type '"valueOf"'.
20-
tests/cases/conformance/types/conditional/conditionalTypes2.ts(25,5): error TS2322: Type 'Invariant<A>' is not assignable to type 'Invariant<B>'.
21-
Types of property 'foo' are incompatible.
22-
Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'.
23-
Type 'A' is not assignable to type 'B'.
24-
'A' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint '{}'.
251
tests/cases/conformance/types/conditional/conditionalTypes2.ts(73,12): error TS2345: Argument of type 'Extract<Extract<T, Foo>, Bar>' is not assignable to parameter of type '{ foo: string; bat: string; }'.
262
Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'.
273
Type 'Extract<T, Bar>' is not assignable to type '{ foo: string; bat: string; }'.
@@ -33,7 +9,7 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2
339
Property 'bat' is missing in type 'Bar & Foo' but required in type '{ foo: string; bat: string; }'.
3410

3511

36-
==== tests/cases/conformance/types/conditional/conditionalTypes2.ts (7 errors) ====
12+
==== tests/cases/conformance/types/conditional/conditionalTypes2.ts (3 errors) ====
3713
interface Covariant<T> {
3814
foo: T extends string ? T : number;
3915
}
@@ -49,44 +25,16 @@ tests/cases/conformance/types/conditional/conditionalTypes2.ts(75,12): error TS2
4925
function f1<A, B extends A>(a: Covariant<A>, b: Covariant<B>) {
5026
a = b;
5127
b = a; // Error
52-
~
53-
!!! error TS2322: Type 'Covariant<A>' is not assignable to type 'Covariant<B>'.
54-
!!! error TS2322: Type 'A' is not assignable to type 'B'.
55-
!!! error TS2322: 'A' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint '{}'.
5628
}
5729

5830
function f2<A, B extends A>(a: Contravariant<A>, b: Contravariant<B>) {
5931
a = b; // Error
60-
~
61-
!!! error TS2322: Type 'Contravariant<B>' is not assignable to type 'Contravariant<A>'.
62-
!!! error TS2322: Type 'A' is not assignable to type 'B'.
63-
!!! error TS2322: 'A' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint '{}'.
6432
b = a;
6533
}
6634

6735
function f3<A, B extends A>(a: Invariant<A>, b: Invariant<B>) {
6836
a = b; // Error
69-
~
70-
!!! error TS2322: Type 'Invariant<B>' is not assignable to type 'Invariant<A>'.
71-
!!! error TS2322: Types of property 'foo' are incompatible.
72-
!!! error TS2322: Type 'B extends string ? keyof B : B' is not assignable to type 'A extends string ? keyof A : A'.
73-
!!! error TS2322: Type 'keyof B' is not assignable to type 'keyof A'.
74-
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'keyof A'.
75-
!!! error TS2322: Type 'string' is not assignable to type 'keyof A'.
76-
!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
77-
!!! error TS2322: Type 'keyof B' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
78-
!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
79-
!!! error TS2322: Type 'string' is not assignable to type 'number | "toString" | "charAt" | "charCodeAt" | "concat" | "indexOf" | "lastIndexOf" | "localeCompare" | "match" | "replace" | "search" | "slice" | "split" | "substring" | "toLowerCase" | "toLocaleLowerCase" | "toUpperCase" | "toLocaleUpperCase" | "trim" | "length" | "substr" | "valueOf"'.
80-
!!! error TS2322: Type 'keyof B' is not assignable to type '"valueOf"'.
81-
!!! error TS2322: Type 'string | number | symbol' is not assignable to type '"valueOf"'.
82-
!!! error TS2322: Type 'string' is not assignable to type '"valueOf"'.
8337
b = a; // Error
84-
~
85-
!!! error TS2322: Type 'Invariant<A>' is not assignable to type 'Invariant<B>'.
86-
!!! error TS2322: Types of property 'foo' are incompatible.
87-
!!! error TS2322: Type 'A extends string ? keyof A : A' is not assignable to type 'B extends string ? keyof B : B'.
88-
!!! error TS2322: Type 'A' is not assignable to type 'B'.
89-
!!! error TS2322: 'A' is assignable to the constraint of type 'B', but 'B' could be instantiated with a different subtype of constraint '{}'.
9038
}
9139

9240
// Extract<T, Function> is a T that is known to be a Function

tests/baselines/reference/conditionalTypesExcessProperties.errors.txt

-24
This file was deleted.

0 commit comments

Comments
 (0)