From 8d9438214d564978016c63bf4153286e9a339033 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Mon, 26 Jun 2023 17:13:51 +0000 Subject: [PATCH] Do not mark indexed access object type comparisons as unreliable. --- src/compiler/checker.ts | 5 --- ...VarianceComparisonResultCorrect.errors.txt | 35 +++++++++++++++++++ ...ccessVarianceComparisonResultCorrect.types | 2 +- 3 files changed, 36 insertions(+), 6 deletions(-) create mode 100644 tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.errors.txt diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index ee86ae50306c7..5c8ba6115267e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -21538,11 +21538,6 @@ export function createTypeChecker(host: TypeCheckerHost): TypeChecker { // Relate components directly before falling back to constraint relationships // A type S[K] is related to a type T[J] if S is related to T and K is related to J. if (result = isRelatedTo((source as IndexedAccessType).objectType, (target as IndexedAccessType).objectType, RecursionFlags.Both, reportErrors)) { - // This does _not_ generalize - specific instantiations of `S[K]` and `T[J]` may be related, even if the indexed accesses generally are not. - // 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 - // `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 - // be used in the affirmative case, it failing is not an indicator that the structural result will not succeed. - instantiateType((source as IndexedAccessType).objectType, reportUnreliableMapper); result &= isRelatedTo((source as IndexedAccessType).indexType, (target as IndexedAccessType).indexType, RecursionFlags.Both, reportErrors); } if (result) { diff --git a/tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.errors.txt b/tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.errors.txt new file mode 100644 index 0000000000000..5e4776ac6db3c --- /dev/null +++ b/tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.errors.txt @@ -0,0 +1,35 @@ +genericIndexedAccessVarianceComparisonResultCorrect.ts(25,1): error TS2322: Type 'T' is not assignable to type 'T'. + Property 'z' is missing in type 'A' but required in type 'B'. + + +==== genericIndexedAccessVarianceComparisonResultCorrect.ts (1 errors) ==== + class A { + x: string = 'A'; + y: number = 0; + } + + class B { + x: string = 'B'; + z: boolean = true; + } + + type T = Pick; + + type C = T; + type D = T; + + type C_extends_D = C extends D ? true : false; // true + type PickA_extends_PickB = Pick extends Pick ? true : false; // true + type TA_extends_TB = T extends T ? true : false; // should be true + + declare let a: T; + declare let b: T; + declare let c: C; + declare let d: D; + + b = a; // should be no error + ~ +!!! error TS2322: Type 'T' is not assignable to type 'T'. +!!! error TS2322: Property 'z' is missing in type 'A' but required in type 'B'. +!!! related TS2728 genericIndexedAccessVarianceComparisonResultCorrect.ts:8:5: 'z' is declared here. + c = d; \ No newline at end of file diff --git a/tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.types b/tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.types index 0e146f4db4cbd..9f980ca5c651f 100644 --- a/tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.types +++ b/tests/baselines/reference/genericIndexedAccessVarianceComparisonResultCorrect.types @@ -46,7 +46,7 @@ type PickA_extends_PickB = Pick extends Pick ? true : false; >false : false type TA_extends_TB = T extends T ? true : false; // should be true ->TA_extends_TB : true +>TA_extends_TB : false >true : true >false : false