|
| 1 | +//// [tests/cases/compiler/keyRemappingKeyofResult.ts] //// |
| 2 | + |
| 3 | +//// [keyRemappingKeyofResult.ts] |
| 4 | +const sym = Symbol("") |
| 5 | +type Orig = { [k: string]: any, str: any, [sym]: any } |
| 6 | + |
| 7 | +type Okay = Exclude<keyof Orig, never> |
| 8 | +// type Okay = string | number | typeof sym |
| 9 | + |
| 10 | +type Remapped = { [K in keyof Orig as {} extends Record<K, any> ? never : K]: any } |
| 11 | +/* type Remapped = { |
| 12 | + str: any; |
| 13 | + [sym]: any; |
| 14 | +} */ |
| 15 | +// no string index signature, right? |
| 16 | + |
| 17 | +type Oops = Exclude<keyof Remapped, never> |
| 18 | +declare let x: Oops; |
| 19 | +x = sym; |
| 20 | +x = "str"; |
| 21 | +// type Oops = typeof sym <-- what happened to "str"? |
| 22 | + |
| 23 | +// equivalently, with an unresolved generic (no `exclude` shenanigans, since conditions won't execute): |
| 24 | +function f<T>() { |
| 25 | + type Orig = { [k: string]: any, str: any, [sym]: any } & T; |
| 26 | + |
| 27 | + type Okay = keyof Orig; |
| 28 | + let a: Okay; |
| 29 | + a = "str"; |
| 30 | + a = sym; |
| 31 | + a = "whatever"; |
| 32 | + // type Okay = string | number | typeof sym |
| 33 | + |
| 34 | + type Remapped = { [K in keyof Orig as {} extends Record<K, any> ? never : K]: any } |
| 35 | + /* type Remapped = { |
| 36 | + str: any; |
| 37 | + [sym]: any; |
| 38 | + } */ |
| 39 | + // no string index signature, right? |
| 40 | + |
| 41 | + type Oops = keyof Remapped; |
| 42 | + let x: Oops; |
| 43 | + x = sym; |
| 44 | + x = "str"; |
| 45 | +} |
| 46 | + |
| 47 | +// and another generic case with a _distributive_ mapping, to trigger a different branch in `getIndexType` |
| 48 | +function g<T>() { |
| 49 | + type Orig = { [k: string]: any, str: any, [sym]: any } & T; |
| 50 | + |
| 51 | + type Okay = keyof Orig; |
| 52 | + let a: Okay; |
| 53 | + a = "str"; |
| 54 | + a = sym; |
| 55 | + a = "whatever"; |
| 56 | + // type Okay = string | number | typeof sym |
| 57 | + |
| 58 | + type NonIndex<T extends PropertyKey> = {} extends Record<T, any> ? never : T; |
| 59 | + type DistributiveNonIndex<T extends PropertyKey> = T extends unknown ? NonIndex<T> : never; |
| 60 | + |
| 61 | + type Remapped = { [K in keyof Orig as DistributiveNonIndex<K>]: any } |
| 62 | + /* type Remapped = { |
| 63 | + str: any; |
| 64 | + [sym]: any; |
| 65 | + } */ |
| 66 | + // no string index signature, right? |
| 67 | + |
| 68 | + type Oops = keyof Remapped; |
| 69 | + let x: Oops; |
| 70 | + x = sym; |
| 71 | + x = "str"; |
| 72 | +} |
| 73 | + |
| 74 | +// https://github.com/microsoft/TypeScript/issues/57827 |
| 75 | + |
| 76 | +type StringKeys<T> = Extract< |
| 77 | + keyof { |
| 78 | + [P in keyof T as T[P] extends string ? P : never]: any; |
| 79 | + }, |
| 80 | + string |
| 81 | +>; |
| 82 | + |
| 83 | +function test_57827<T>(z: StringKeys<T>) { |
| 84 | + const f: string = z; |
| 85 | +} |
| 86 | + |
| 87 | +export {}; |
| 88 | + |
| 89 | +//// [keyRemappingKeyofResult.js] |
| 90 | +const sym = Symbol(""); |
| 91 | +x = sym; |
| 92 | +x = "str"; |
| 93 | +// type Oops = typeof sym <-- what happened to "str"? |
| 94 | +// equivalently, with an unresolved generic (no `exclude` shenanigans, since conditions won't execute): |
| 95 | +function f() { |
| 96 | + let a; |
| 97 | + a = "str"; |
| 98 | + a = sym; |
| 99 | + a = "whatever"; |
| 100 | + let x; |
| 101 | + x = sym; |
| 102 | + x = "str"; |
| 103 | +} |
| 104 | +// and another generic case with a _distributive_ mapping, to trigger a different branch in `getIndexType` |
| 105 | +function g() { |
| 106 | + let a; |
| 107 | + a = "str"; |
| 108 | + a = sym; |
| 109 | + a = "whatever"; |
| 110 | + let x; |
| 111 | + x = sym; |
| 112 | + x = "str"; |
| 113 | +} |
| 114 | +function test_57827(z) { |
| 115 | + const f = z; |
| 116 | +} |
| 117 | +export {}; |
0 commit comments