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.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; +}