Skip to content

Commit f244192

Browse files
Pick 54781 to release 5.1 (#54792)
1 parent db3575a commit f244192

File tree

3 files changed

+36
-6
lines changed

3 files changed

+36
-6
lines changed

src/compiler/checker.ts

-5
Original file line numberDiff line numberDiff line change
@@ -21459,11 +21459,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker {
2145921459
// Relate components directly before falling back to constraint relationships
2146021460
// A type S[K] is related to a type T[J] if S is related to T and K is related to J.
2146121461
if (result = isRelatedTo((source as IndexedAccessType).objectType, (target as IndexedAccessType).objectType, RecursionFlags.Both, reportErrors)) {
21462-
// This does _not_ generalize - specific instantiations of `S[K]` and `T[J]` may be related, even if the indexed accesses generally are not.
21463-
// For example, `S = {x: string, a: string}`, `T = {x: string, b: string}`, `K = J = "x"`. `S` and `T` are unrelated, but the result of executing
21464-
// `S["x"]` and `T["x"]` _are_. Given that, we have to flag the object type comparison here as "unreliable", since while the generic result can reliably
21465-
// be used in the affirmative case, it failing is not an indicator that the structural result will not succeed.
21466-
instantiateType((source as IndexedAccessType).objectType, reportUnreliableMapper);
2146721462
result &= isRelatedTo((source as IndexedAccessType).indexType, (target as IndexedAccessType).indexType, RecursionFlags.Both, reportErrors);
2146821463
}
2146921464
if (result) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
tests/cases/compiler/genericIndexedAccessVarianceComparisonResultCorrect.ts(25,1): error TS2322: Type 'T<A>' is not assignable to type 'T<B>'.
2+
Property 'z' is missing in type 'A' but required in type 'B'.
3+
4+
5+
==== tests/cases/compiler/genericIndexedAccessVarianceComparisonResultCorrect.ts (1 errors) ====
6+
class A {
7+
x: string = 'A';
8+
y: number = 0;
9+
}
10+
11+
class B {
12+
x: string = 'B';
13+
z: boolean = true;
14+
}
15+
16+
type T<X extends { x: any }> = Pick<X, 'x'>;
17+
18+
type C = T<A>;
19+
type D = T<B>;
20+
21+
type C_extends_D = C extends D ? true : false; // true
22+
type PickA_extends_PickB = Pick<A, 'x'> extends Pick<B, 'x'> ? true : false; // true
23+
type TA_extends_TB = T<A> extends T<B> ? true : false; // should be true
24+
25+
declare let a: T<A>;
26+
declare let b: T<B>;
27+
declare let c: C;
28+
declare let d: D;
29+
30+
b = a; // should be no error
31+
~
32+
!!! error TS2322: Type 'T<A>' is not assignable to type 'T<B>'.
33+
!!! error TS2322: Property 'z' is missing in type 'A' but required in type 'B'.
34+
!!! related TS2728 tests/cases/compiler/genericIndexedAccessVarianceComparisonResultCorrect.ts:8:5: 'z' is declared here.
35+
c = d;

tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.types

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ type PickA_extends_PickB = Pick<A, 'x'> extends Pick<B, 'x'> ? true : false;
4444
>false : false
4545

4646
type TA_extends_TB = T<A> extends T<B> ? true : false; // should be true
47-
>TA_extends_TB : true
47+
>TA_extends_TB : false
4848
>true : true
4949
>false : false
5050

0 commit comments

Comments
 (0)