From 3f1e86aec45d29467d6755c23b2c349d495397b9 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:39:09 -0800 Subject: [PATCH 1/2] Add a test for indexed access types with a union index Reading obj[key] with a key of a union of type parameters gives a type that should be identical to T[K1 | K2] written in a type annotation. It is reported as not assignable to it, which TypeScript 6.0 accepts. --- .../indexedAccessUnionKeyIdentity.errors.txt | 79 ++++++++++++++++ .../indexedAccessUnionKeyIdentity.symbols | 92 +++++++++++++++++++ .../indexedAccessUnionKeyIdentity.types | 65 +++++++++++++ .../compiler/indexedAccessUnionKeyIdentity.ts | 22 +++++ 4 files changed, 258 insertions(+) create mode 100644 tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt create mode 100644 tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.symbols create mode 100644 tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.types create mode 100644 tsc/testdata/tests/cases/compiler/indexedAccessUnionKeyIdentity.ts diff --git a/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt b/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt new file mode 100644 index 0000000000000..20c2604349a4e --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt @@ -0,0 +1,79 @@ +indexedAccessUnionKeyIdentity.ts(6,11): error TS2322: Type 'T[K1] | T[K2]' is not assignable to type 'T[K1] & T[K2]'. + Type 'T[K1]' is not assignable to type 'T[K1] & T[K2]'. + Type 'T[keyof T]' is not assignable to type 'T[K1] & T[K2]'. + Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'T[K1] & T[K2]'. + Type 'T[string]' is not assignable to type 'T[K1] & T[K2]'. + Type 'T[string]' is not assignable to type 'T[K1]'. + Type 'string' is not assignable to type 'K1'. + 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. + Type 'T[keyof T]' is not assignable to type 'T[K1]'. + Type 'keyof T' is not assignable to type 'K1'. + 'keyof T' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. + Type 'string | number | symbol' is not assignable to type 'K1'. + 'string | number | symbol' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. + Type 'string' is not assignable to type 'K1'. + 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. + Type 'T[K1]' is not assignable to type 'T[K2]'. + Type 'K1' is not assignable to type 'K2'. + 'K1' is assignable to the constraint of type 'K2', but 'K2' could be instantiated with a different subtype of constraint 'string | number | symbol'. +indexedAccessUnionKeyIdentity.ts(12,26): error TS2345: Argument of type '(A & B)[keyof A] | (A & B)[keyof B]' is not assignable to parameter of type '(A & B)[keyof A] & (A & B)[keyof B]'. + Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. + Type '(A[string] & B[string]) | (A[number] & B[number]) | (A[symbol] & B[symbol])' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. + Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. + Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A]'. + Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof B]'. + Type 'keyof A' is not assignable to type 'keyof B'. + Type 'string | number | symbol' is not assignable to type 'keyof B'. + Type 'string' is not assignable to type 'keyof B'. + + +==== indexedAccessUnionKeyIdentity.ts (2 errors) ==== + // An indexed access type with a union index must be the same type wherever + // it is written, whatever flags the access was made with, as reading and + // writing the same type are related by identity. + + function f(obj: T, key: K1 | K2) { + const value: T[K1 | K2] = obj[key]; + ~~~~~ +!!! error TS2322: Type 'T[K1] | T[K2]' is not assignable to type 'T[K1] & T[K2]'. +!!! error TS2322: Type 'T[K1]' is not assignable to type 'T[K1] & T[K2]'. +!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'T[K1] & T[K2]'. +!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'T[K1] & T[K2]'. +!!! error TS2322: Type 'T[string]' is not assignable to type 'T[K1] & T[K2]'. +!!! error TS2322: Type 'T[string]' is not assignable to type 'T[K1]'. +!!! error TS2322: Type 'string' is not assignable to type 'K1'. +!!! error TS2322: 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. +!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'T[K1]'. +!!! error TS2322: Type 'keyof T' is not assignable to type 'K1'. +!!! error TS2322: 'keyof T' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. +!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'K1'. +!!! error TS2322: 'string | number | symbol' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. +!!! error TS2322: Type 'string' is not assignable to type 'K1'. +!!! error TS2322: 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. +!!! error TS2322: Type 'T[K1]' is not assignable to type 'T[K2]'. +!!! error TS2322: Type 'K1' is not assignable to type 'K2'. +!!! error TS2322: 'K1' is assignable to the constraint of type 'K2', but 'K2' could be instantiated with a different subtype of constraint 'string | number | symbol'. + return value; + } + + declare function set(obj: T, key: keyof T, value: T[keyof T]): void; + function g(obj: A & B, key: keyof (A & B)) { + set(obj, key, obj[key]); + ~~~~~~~~ +!!! error TS2345: Argument of type '(A & B)[keyof A] | (A & B)[keyof B]' is not assignable to parameter of type '(A & B)[keyof A] & (A & B)[keyof B]'. +!!! error TS2345: Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. +!!! error TS2345: Type '(A[string] & B[string]) | (A[number] & B[number]) | (A[symbol] & B[symbol])' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. +!!! error TS2345: Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. +!!! error TS2345: Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A]'. +!!! error TS2345: Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof B]'. +!!! error TS2345: Type 'keyof A' is not assignable to type 'keyof B'. +!!! error TS2345: Type 'string | number | symbol' is not assignable to type 'keyof B'. +!!! error TS2345: Type 'string' is not assignable to type 'keyof B'. + } + + function h(obj: T, key: K1 | K2) { + const a = obj[key]; + const value: typeof a = obj[key]; + return value; + } + \ No newline at end of file diff --git a/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.symbols b/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.symbols new file mode 100644 index 0000000000000..a183d53f0c263 --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.symbols @@ -0,0 +1,92 @@ +//// [tests/cases/compiler/indexedAccessUnionKeyIdentity.ts] //// + +=== indexedAccessUnionKeyIdentity.ts === +// An indexed access type with a union index must be the same type wherever +// it is written, whatever flags the access was made with, as reading and +// writing the same type are related by identity. + +function f(obj: T, key: K1 | K2) { +>f : Symbol(f, Decl(indexedAccessUnionKeyIdentity.ts, 0, 0)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 4, 11)) +>K1 : Symbol(K1, Decl(indexedAccessUnionKeyIdentity.ts, 4, 13)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 4, 11)) +>K2 : Symbol(K2, Decl(indexedAccessUnionKeyIdentity.ts, 4, 33)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 4, 11)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 4, 54)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 4, 11)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 4, 61)) +>K1 : Symbol(K1, Decl(indexedAccessUnionKeyIdentity.ts, 4, 13)) +>K2 : Symbol(K2, Decl(indexedAccessUnionKeyIdentity.ts, 4, 33)) + + const value: T[K1 | K2] = obj[key]; +>value : Symbol(value, Decl(indexedAccessUnionKeyIdentity.ts, 5, 9)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 4, 11)) +>K1 : Symbol(K1, Decl(indexedAccessUnionKeyIdentity.ts, 4, 13)) +>K2 : Symbol(K2, Decl(indexedAccessUnionKeyIdentity.ts, 4, 33)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 4, 54)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 4, 61)) + + return value; +>value : Symbol(value, Decl(indexedAccessUnionKeyIdentity.ts, 5, 9)) +} + +declare function set(obj: T, key: keyof T, value: T[keyof T]): void; +>set : Symbol(set, Decl(indexedAccessUnionKeyIdentity.ts, 7, 1)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 9, 21)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 9, 24)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 9, 21)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 9, 31)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 9, 21)) +>value : Symbol(value, Decl(indexedAccessUnionKeyIdentity.ts, 9, 45)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 9, 21)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 9, 21)) + +function g(obj: A & B, key: keyof (A & B)) { +>g : Symbol(g, Decl(indexedAccessUnionKeyIdentity.ts, 9, 71)) +>A : Symbol(A, Decl(indexedAccessUnionKeyIdentity.ts, 10, 11)) +>B : Symbol(B, Decl(indexedAccessUnionKeyIdentity.ts, 10, 13)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 10, 17)) +>A : Symbol(A, Decl(indexedAccessUnionKeyIdentity.ts, 10, 11)) +>B : Symbol(B, Decl(indexedAccessUnionKeyIdentity.ts, 10, 13)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 10, 28)) +>A : Symbol(A, Decl(indexedAccessUnionKeyIdentity.ts, 10, 11)) +>B : Symbol(B, Decl(indexedAccessUnionKeyIdentity.ts, 10, 13)) + + set(obj, key, obj[key]); +>set : Symbol(set, Decl(indexedAccessUnionKeyIdentity.ts, 7, 1)) +>A : Symbol(A, Decl(indexedAccessUnionKeyIdentity.ts, 10, 11)) +>B : Symbol(B, Decl(indexedAccessUnionKeyIdentity.ts, 10, 13)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 10, 17)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 10, 28)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 10, 17)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 10, 28)) +} + +function h(obj: T, key: K1 | K2) { +>h : Symbol(h, Decl(indexedAccessUnionKeyIdentity.ts, 12, 1)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 14, 11)) +>K1 : Symbol(K1, Decl(indexedAccessUnionKeyIdentity.ts, 14, 13)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 14, 11)) +>K2 : Symbol(K2, Decl(indexedAccessUnionKeyIdentity.ts, 14, 33)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 14, 11)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 14, 54)) +>T : Symbol(T, Decl(indexedAccessUnionKeyIdentity.ts, 14, 11)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 14, 61)) +>K1 : Symbol(K1, Decl(indexedAccessUnionKeyIdentity.ts, 14, 13)) +>K2 : Symbol(K2, Decl(indexedAccessUnionKeyIdentity.ts, 14, 33)) + + const a = obj[key]; +>a : Symbol(a, Decl(indexedAccessUnionKeyIdentity.ts, 15, 9)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 14, 54)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 14, 61)) + + const value: typeof a = obj[key]; +>value : Symbol(value, Decl(indexedAccessUnionKeyIdentity.ts, 16, 9)) +>a : Symbol(a, Decl(indexedAccessUnionKeyIdentity.ts, 15, 9)) +>obj : Symbol(obj, Decl(indexedAccessUnionKeyIdentity.ts, 14, 54)) +>key : Symbol(key, Decl(indexedAccessUnionKeyIdentity.ts, 14, 61)) + + return value; +>value : Symbol(value, Decl(indexedAccessUnionKeyIdentity.ts, 16, 9)) +} + diff --git a/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.types b/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.types new file mode 100644 index 0000000000000..cf69b2c64d35b --- /dev/null +++ b/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.types @@ -0,0 +1,65 @@ +//// [tests/cases/compiler/indexedAccessUnionKeyIdentity.ts] //// + +=== indexedAccessUnionKeyIdentity.ts === +// An indexed access type with a union index must be the same type wherever +// it is written, whatever flags the access was made with, as reading and +// writing the same type are related by identity. + +function f(obj: T, key: K1 | K2) { +>f : (obj: T, key: K1 | K2) => T[K1 | K2] +>obj : T +>key : K1 | K2 + + const value: T[K1 | K2] = obj[key]; +>value : T[K1 | K2] +>obj[key] : T[K1 | K2] +>obj : T +>key : K1 | K2 + + return value; +>value : T[K1 | K2] +} + +declare function set(obj: T, key: keyof T, value: T[keyof T]): void; +>set : (obj: T, key: keyof T, value: T[keyof T]) => void +>obj : T +>key : keyof T +>value : T[keyof T] + +function g(obj: A & B, key: keyof (A & B)) { +>g : (obj: A & B, key: keyof (A & B)) => void +>obj : A & B +>key : keyof A | keyof B + + set(obj, key, obj[key]); +>set(obj, key, obj[key]) : void +>set : (obj: T, key: keyof T, value: T[keyof T]) => void +>obj : A & B +>key : keyof A | keyof B +>obj[key] : (A & B)[keyof A | keyof B] +>obj : A & B +>key : keyof A | keyof B +} + +function h(obj: T, key: K1 | K2) { +>h : (obj: T, key: K1 | K2) => T[K1 | K2] +>obj : T +>key : K1 | K2 + + const a = obj[key]; +>a : T[K1 | K2] +>obj[key] : T[K1 | K2] +>obj : T +>key : K1 | K2 + + const value: typeof a = obj[key]; +>value : T[K1 | K2] +>a : T[K1 | K2] +>obj[key] : T[K1 | K2] +>obj : T +>key : K1 | K2 + + return value; +>value : T[K1 | K2] +} + diff --git a/tsc/testdata/tests/cases/compiler/indexedAccessUnionKeyIdentity.ts b/tsc/testdata/tests/cases/compiler/indexedAccessUnionKeyIdentity.ts new file mode 100644 index 0000000000000..e816f2748d0f4 --- /dev/null +++ b/tsc/testdata/tests/cases/compiler/indexedAccessUnionKeyIdentity.ts @@ -0,0 +1,22 @@ +// @strict: true +// @noEmit: true + +// An indexed access type with a union index must be the same type wherever +// it is written, whatever flags the access was made with, as reading and +// writing the same type are related by identity. + +function f(obj: T, key: K1 | K2) { + const value: T[K1 | K2] = obj[key]; + return value; +} + +declare function set(obj: T, key: keyof T, value: T[keyof T]): void; +function g(obj: A & B, key: keyof (A & B)) { + set(obj, key, obj[key]); +} + +function h(obj: T, key: K1 | K2) { + const a = obj[key]; + const value: typeof a = obj[key]; + return value; +} From 4254b94545c7435d3f7788e4957bca108c7f7bf8 Mon Sep 17 00:00:00 2001 From: Jake Bailey <5341706+jakebailey@users.noreply.github.com> Date: Wed, 11 Nov 2026 16:40:40 -0800 Subject: [PATCH 2/2] Key deferred indexed access types by their persistent access flags An indexed access type keeps only the persistent access flags, but the cache of deferred indexed access types was keyed by all of them, so an access made in an expression created a second type identical to the one written in a type annotation. Relating the two then could not use their identity, and simplified the source for reading and the target for writing, which are not related when the index is a union. TypeScript 6.0 keys the cache by the persistent access flags. --- tsc/internal/checker/checker.go | 2 +- .../indexedAccessUnionKeyIdentity.errors.txt | 79 ------------------- 2 files changed, 1 insertion(+), 80 deletions(-) delete mode 100644 tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt diff --git a/tsc/internal/checker/checker.go b/tsc/internal/checker/checker.go index b3b4da9db293f..f1e5564580f3c 100644 --- a/tsc/internal/checker/checker.go +++ b/tsc/internal/checker/checker.go @@ -27784,7 +27784,7 @@ func (c *Checker) getIndexedAccessTypeOrUndefined(objectType *Type, indexType *T } // Defer the operation by creating an indexed access type. persistentAccessFlags := accessFlags & AccessFlagsPersistent - key := getIndexedAccessKey(objectType, indexType, accessFlags, alias) + key := getIndexedAccessKey(objectType, indexType, persistentAccessFlags, alias) t := c.indexedAccessTypes[key] if t == nil { t = c.newIndexedAccessType(objectType, indexType, persistentAccessFlags) diff --git a/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt b/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt deleted file mode 100644 index 20c2604349a4e..0000000000000 --- a/tsc/testdata/baselines/reference/compiler/indexedAccessUnionKeyIdentity.errors.txt +++ /dev/null @@ -1,79 +0,0 @@ -indexedAccessUnionKeyIdentity.ts(6,11): error TS2322: Type 'T[K1] | T[K2]' is not assignable to type 'T[K1] & T[K2]'. - Type 'T[K1]' is not assignable to type 'T[K1] & T[K2]'. - Type 'T[keyof T]' is not assignable to type 'T[K1] & T[K2]'. - Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'T[K1] & T[K2]'. - Type 'T[string]' is not assignable to type 'T[K1] & T[K2]'. - Type 'T[string]' is not assignable to type 'T[K1]'. - Type 'string' is not assignable to type 'K1'. - 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. - Type 'T[keyof T]' is not assignable to type 'T[K1]'. - Type 'keyof T' is not assignable to type 'K1'. - 'keyof T' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. - Type 'string | number | symbol' is not assignable to type 'K1'. - 'string | number | symbol' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. - Type 'string' is not assignable to type 'K1'. - 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. - Type 'T[K1]' is not assignable to type 'T[K2]'. - Type 'K1' is not assignable to type 'K2'. - 'K1' is assignable to the constraint of type 'K2', but 'K2' could be instantiated with a different subtype of constraint 'string | number | symbol'. -indexedAccessUnionKeyIdentity.ts(12,26): error TS2345: Argument of type '(A & B)[keyof A] | (A & B)[keyof B]' is not assignable to parameter of type '(A & B)[keyof A] & (A & B)[keyof B]'. - Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. - Type '(A[string] & B[string]) | (A[number] & B[number]) | (A[symbol] & B[symbol])' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. - Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. - Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A]'. - Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof B]'. - Type 'keyof A' is not assignable to type 'keyof B'. - Type 'string | number | symbol' is not assignable to type 'keyof B'. - Type 'string' is not assignable to type 'keyof B'. - - -==== indexedAccessUnionKeyIdentity.ts (2 errors) ==== - // An indexed access type with a union index must be the same type wherever - // it is written, whatever flags the access was made with, as reading and - // writing the same type are related by identity. - - function f(obj: T, key: K1 | K2) { - const value: T[K1 | K2] = obj[key]; - ~~~~~ -!!! error TS2322: Type 'T[K1] | T[K2]' is not assignable to type 'T[K1] & T[K2]'. -!!! error TS2322: Type 'T[K1]' is not assignable to type 'T[K1] & T[K2]'. -!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'T[K1] & T[K2]'. -!!! error TS2322: Type 'T[string] | T[number] | T[symbol]' is not assignable to type 'T[K1] & T[K2]'. -!!! error TS2322: Type 'T[string]' is not assignable to type 'T[K1] & T[K2]'. -!!! error TS2322: Type 'T[string]' is not assignable to type 'T[K1]'. -!!! error TS2322: Type 'string' is not assignable to type 'K1'. -!!! error TS2322: 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. -!!! error TS2322: Type 'T[keyof T]' is not assignable to type 'T[K1]'. -!!! error TS2322: Type 'keyof T' is not assignable to type 'K1'. -!!! error TS2322: 'keyof T' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. -!!! error TS2322: Type 'string | number | symbol' is not assignable to type 'K1'. -!!! error TS2322: 'string | number | symbol' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. -!!! error TS2322: Type 'string' is not assignable to type 'K1'. -!!! error TS2322: 'string' is assignable to the constraint of type 'K1', but 'K1' could be instantiated with a different subtype of constraint 'string | number | symbol'. -!!! error TS2322: Type 'T[K1]' is not assignable to type 'T[K2]'. -!!! error TS2322: Type 'K1' is not assignable to type 'K2'. -!!! error TS2322: 'K1' is assignable to the constraint of type 'K2', but 'K2' could be instantiated with a different subtype of constraint 'string | number | symbol'. - return value; - } - - declare function set(obj: T, key: keyof T, value: T[keyof T]): void; - function g(obj: A & B, key: keyof (A & B)) { - set(obj, key, obj[key]); - ~~~~~~~~ -!!! error TS2345: Argument of type '(A & B)[keyof A] | (A & B)[keyof B]' is not assignable to parameter of type '(A & B)[keyof A] & (A & B)[keyof B]'. -!!! error TS2345: Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. -!!! error TS2345: Type '(A[string] & B[string]) | (A[number] & B[number]) | (A[symbol] & B[symbol])' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. -!!! error TS2345: Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A] & (A & B)[keyof B]'. -!!! error TS2345: Type 'A[string] & B[string]' is not assignable to type '(A & B)[keyof A]'. -!!! error TS2345: Type '(A & B)[keyof A]' is not assignable to type '(A & B)[keyof B]'. -!!! error TS2345: Type 'keyof A' is not assignable to type 'keyof B'. -!!! error TS2345: Type 'string | number | symbol' is not assignable to type 'keyof B'. -!!! error TS2345: Type 'string' is not assignable to type 'keyof B'. - } - - function h(obj: T, key: K1 | K2) { - const a = obj[key]; - const value: typeof a = obj[key]; - return value; - } - \ No newline at end of file