|
| 1 | +//// [tests/cases/compiler/deeplyNestedMappedTypes.ts] //// |
| 2 | + |
| 3 | +=== deeplyNestedMappedTypes.ts === |
| 4 | +// Simplified repro from #55535 |
| 5 | + |
| 6 | +type Id<T> = { [K in keyof T]: Id<T[K]> }; |
| 7 | +>Id : Symbol(Id, Decl(deeplyNestedMappedTypes.ts, 0, 0)) |
| 8 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 2, 8)) |
| 9 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 2, 16)) |
| 10 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 2, 8)) |
| 11 | +>Id : Symbol(Id, Decl(deeplyNestedMappedTypes.ts, 0, 0)) |
| 12 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 2, 8)) |
| 13 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 2, 16)) |
| 14 | + |
| 15 | +type Foo1 = Id<{ x: { y: { z: { a: { b: { c: number } } } } } }>; |
| 16 | +>Foo1 : Symbol(Foo1, Decl(deeplyNestedMappedTypes.ts, 2, 42)) |
| 17 | +>Id : Symbol(Id, Decl(deeplyNestedMappedTypes.ts, 0, 0)) |
| 18 | +>x : Symbol(x, Decl(deeplyNestedMappedTypes.ts, 4, 16)) |
| 19 | +>y : Symbol(y, Decl(deeplyNestedMappedTypes.ts, 4, 21)) |
| 20 | +>z : Symbol(z, Decl(deeplyNestedMappedTypes.ts, 4, 26)) |
| 21 | +>a : Symbol(a, Decl(deeplyNestedMappedTypes.ts, 4, 31)) |
| 22 | +>b : Symbol(b, Decl(deeplyNestedMappedTypes.ts, 4, 36)) |
| 23 | +>c : Symbol(c, Decl(deeplyNestedMappedTypes.ts, 4, 41)) |
| 24 | + |
| 25 | +type Foo2 = Id<{ x: { y: { z: { a: { b: { c: string } } } } } }>; |
| 26 | +>Foo2 : Symbol(Foo2, Decl(deeplyNestedMappedTypes.ts, 4, 65)) |
| 27 | +>Id : Symbol(Id, Decl(deeplyNestedMappedTypes.ts, 0, 0)) |
| 28 | +>x : Symbol(x, Decl(deeplyNestedMappedTypes.ts, 5, 16)) |
| 29 | +>y : Symbol(y, Decl(deeplyNestedMappedTypes.ts, 5, 21)) |
| 30 | +>z : Symbol(z, Decl(deeplyNestedMappedTypes.ts, 5, 26)) |
| 31 | +>a : Symbol(a, Decl(deeplyNestedMappedTypes.ts, 5, 31)) |
| 32 | +>b : Symbol(b, Decl(deeplyNestedMappedTypes.ts, 5, 36)) |
| 33 | +>c : Symbol(c, Decl(deeplyNestedMappedTypes.ts, 5, 41)) |
| 34 | + |
| 35 | +declare const foo1: Foo1; |
| 36 | +>foo1 : Symbol(foo1, Decl(deeplyNestedMappedTypes.ts, 7, 13)) |
| 37 | +>Foo1 : Symbol(Foo1, Decl(deeplyNestedMappedTypes.ts, 2, 42)) |
| 38 | + |
| 39 | +const foo2: Foo2 = foo1; // Error expected |
| 40 | +>foo2 : Symbol(foo2, Decl(deeplyNestedMappedTypes.ts, 8, 5)) |
| 41 | +>Foo2 : Symbol(Foo2, Decl(deeplyNestedMappedTypes.ts, 4, 65)) |
| 42 | +>foo1 : Symbol(foo1, Decl(deeplyNestedMappedTypes.ts, 7, 13)) |
| 43 | + |
| 44 | +type Id2<T> = { [K in keyof T]: Id2<Id2<T[K]>> }; |
| 45 | +>Id2 : Symbol(Id2, Decl(deeplyNestedMappedTypes.ts, 8, 24)) |
| 46 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 10, 9)) |
| 47 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 10, 17)) |
| 48 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 10, 9)) |
| 49 | +>Id2 : Symbol(Id2, Decl(deeplyNestedMappedTypes.ts, 8, 24)) |
| 50 | +>Id2 : Symbol(Id2, Decl(deeplyNestedMappedTypes.ts, 8, 24)) |
| 51 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 10, 9)) |
| 52 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 10, 17)) |
| 53 | + |
| 54 | +type Foo3 = Id2<{ x: { y: { z: { a: { b: { c: number } } } } } }>; |
| 55 | +>Foo3 : Symbol(Foo3, Decl(deeplyNestedMappedTypes.ts, 10, 49)) |
| 56 | +>Id2 : Symbol(Id2, Decl(deeplyNestedMappedTypes.ts, 8, 24)) |
| 57 | +>x : Symbol(x, Decl(deeplyNestedMappedTypes.ts, 12, 17)) |
| 58 | +>y : Symbol(y, Decl(deeplyNestedMappedTypes.ts, 12, 22)) |
| 59 | +>z : Symbol(z, Decl(deeplyNestedMappedTypes.ts, 12, 27)) |
| 60 | +>a : Symbol(a, Decl(deeplyNestedMappedTypes.ts, 12, 32)) |
| 61 | +>b : Symbol(b, Decl(deeplyNestedMappedTypes.ts, 12, 37)) |
| 62 | +>c : Symbol(c, Decl(deeplyNestedMappedTypes.ts, 12, 42)) |
| 63 | + |
| 64 | +type Foo4 = Id2<{ x: { y: { z: { a: { b: { c: string } } } } } }>; |
| 65 | +>Foo4 : Symbol(Foo4, Decl(deeplyNestedMappedTypes.ts, 12, 66)) |
| 66 | +>Id2 : Symbol(Id2, Decl(deeplyNestedMappedTypes.ts, 8, 24)) |
| 67 | +>x : Symbol(x, Decl(deeplyNestedMappedTypes.ts, 13, 17)) |
| 68 | +>y : Symbol(y, Decl(deeplyNestedMappedTypes.ts, 13, 22)) |
| 69 | +>z : Symbol(z, Decl(deeplyNestedMappedTypes.ts, 13, 27)) |
| 70 | +>a : Symbol(a, Decl(deeplyNestedMappedTypes.ts, 13, 32)) |
| 71 | +>b : Symbol(b, Decl(deeplyNestedMappedTypes.ts, 13, 37)) |
| 72 | +>c : Symbol(c, Decl(deeplyNestedMappedTypes.ts, 13, 42)) |
| 73 | + |
| 74 | +declare const foo3: Foo3; |
| 75 | +>foo3 : Symbol(foo3, Decl(deeplyNestedMappedTypes.ts, 15, 13)) |
| 76 | +>Foo3 : Symbol(Foo3, Decl(deeplyNestedMappedTypes.ts, 10, 49)) |
| 77 | + |
| 78 | +const foo4: Foo4 = foo3; // Error expected |
| 79 | +>foo4 : Symbol(foo4, Decl(deeplyNestedMappedTypes.ts, 16, 5)) |
| 80 | +>Foo4 : Symbol(Foo4, Decl(deeplyNestedMappedTypes.ts, 12, 66)) |
| 81 | +>foo3 : Symbol(foo3, Decl(deeplyNestedMappedTypes.ts, 15, 13)) |
| 82 | + |
| 83 | +// Repro from issue linked in #55535 |
| 84 | + |
| 85 | +type RequiredDeep<T> = { [K in keyof T]-?: RequiredDeep<T[K]> }; |
| 86 | +>RequiredDeep : Symbol(RequiredDeep, Decl(deeplyNestedMappedTypes.ts, 16, 24)) |
| 87 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 20, 18)) |
| 88 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 20, 26)) |
| 89 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 20, 18)) |
| 90 | +>RequiredDeep : Symbol(RequiredDeep, Decl(deeplyNestedMappedTypes.ts, 16, 24)) |
| 91 | +>T : Symbol(T, Decl(deeplyNestedMappedTypes.ts, 20, 18)) |
| 92 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 20, 26)) |
| 93 | + |
| 94 | +type A = { a?: { b: { c: 1 | { d: 2000 } }}} |
| 95 | +>A : Symbol(A, Decl(deeplyNestedMappedTypes.ts, 20, 64)) |
| 96 | +>a : Symbol(a, Decl(deeplyNestedMappedTypes.ts, 22, 10)) |
| 97 | +>b : Symbol(b, Decl(deeplyNestedMappedTypes.ts, 22, 16)) |
| 98 | +>c : Symbol(c, Decl(deeplyNestedMappedTypes.ts, 22, 21)) |
| 99 | +>d : Symbol(d, Decl(deeplyNestedMappedTypes.ts, 22, 30)) |
| 100 | + |
| 101 | +type B = { a?: { b: { c: { d: { e: { f: { g: 2 }}}}, x: 1000 }}} |
| 102 | +>B : Symbol(B, Decl(deeplyNestedMappedTypes.ts, 22, 44)) |
| 103 | +>a : Symbol(a, Decl(deeplyNestedMappedTypes.ts, 23, 10)) |
| 104 | +>b : Symbol(b, Decl(deeplyNestedMappedTypes.ts, 23, 16)) |
| 105 | +>c : Symbol(c, Decl(deeplyNestedMappedTypes.ts, 23, 21)) |
| 106 | +>d : Symbol(d, Decl(deeplyNestedMappedTypes.ts, 23, 26)) |
| 107 | +>e : Symbol(e, Decl(deeplyNestedMappedTypes.ts, 23, 31)) |
| 108 | +>f : Symbol(f, Decl(deeplyNestedMappedTypes.ts, 23, 36)) |
| 109 | +>g : Symbol(g, Decl(deeplyNestedMappedTypes.ts, 23, 41)) |
| 110 | +>x : Symbol(x, Decl(deeplyNestedMappedTypes.ts, 23, 52)) |
| 111 | + |
| 112 | +type C = RequiredDeep<A>; |
| 113 | +>C : Symbol(C, Decl(deeplyNestedMappedTypes.ts, 23, 64)) |
| 114 | +>RequiredDeep : Symbol(RequiredDeep, Decl(deeplyNestedMappedTypes.ts, 16, 24)) |
| 115 | +>A : Symbol(A, Decl(deeplyNestedMappedTypes.ts, 20, 64)) |
| 116 | + |
| 117 | +type D = RequiredDeep<B>; |
| 118 | +>D : Symbol(D, Decl(deeplyNestedMappedTypes.ts, 25, 25)) |
| 119 | +>RequiredDeep : Symbol(RequiredDeep, Decl(deeplyNestedMappedTypes.ts, 16, 24)) |
| 120 | +>B : Symbol(B, Decl(deeplyNestedMappedTypes.ts, 22, 44)) |
| 121 | + |
| 122 | +type Test1 = [C, D] extends [D, C] ? true : false; // false |
| 123 | +>Test1 : Symbol(Test1, Decl(deeplyNestedMappedTypes.ts, 26, 25)) |
| 124 | +>C : Symbol(C, Decl(deeplyNestedMappedTypes.ts, 23, 64)) |
| 125 | +>D : Symbol(D, Decl(deeplyNestedMappedTypes.ts, 25, 25)) |
| 126 | +>D : Symbol(D, Decl(deeplyNestedMappedTypes.ts, 25, 25)) |
| 127 | +>C : Symbol(C, Decl(deeplyNestedMappedTypes.ts, 23, 64)) |
| 128 | + |
| 129 | +type Test2 = C extends D ? true : false; // false |
| 130 | +>Test2 : Symbol(Test2, Decl(deeplyNestedMappedTypes.ts, 28, 50)) |
| 131 | +>C : Symbol(C, Decl(deeplyNestedMappedTypes.ts, 23, 64)) |
| 132 | +>D : Symbol(D, Decl(deeplyNestedMappedTypes.ts, 25, 25)) |
| 133 | + |
| 134 | +type Test3 = D extends C ? true : false; // false |
| 135 | +>Test3 : Symbol(Test3, Decl(deeplyNestedMappedTypes.ts, 29, 40)) |
| 136 | +>D : Symbol(D, Decl(deeplyNestedMappedTypes.ts, 25, 25)) |
| 137 | +>C : Symbol(C, Decl(deeplyNestedMappedTypes.ts, 23, 64)) |
| 138 | + |
| 139 | +// Simplified repro from #54246 |
| 140 | + |
| 141 | +// Except for the final non-recursive Record<K, V>, object types produced by NestedRecord all have the same symbol |
| 142 | +// and thus are considered deeply nested after three levels of nesting. Ideally we'd detect that recursion in this |
| 143 | +// type always terminates, but we're unaware of a general algorithm that accomplishes this. |
| 144 | + |
| 145 | +type NestedRecord<K extends string, V> = K extends `${infer K0}.${infer KR}` ? { [P in K0]: NestedRecord<KR, V> } : Record<K, V>; |
| 146 | +>NestedRecord : Symbol(NestedRecord, Decl(deeplyNestedMappedTypes.ts, 30, 40)) |
| 147 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 38, 18)) |
| 148 | +>V : Symbol(V, Decl(deeplyNestedMappedTypes.ts, 38, 35)) |
| 149 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 38, 18)) |
| 150 | +>K0 : Symbol(K0, Decl(deeplyNestedMappedTypes.ts, 38, 59)) |
| 151 | +>KR : Symbol(KR, Decl(deeplyNestedMappedTypes.ts, 38, 71)) |
| 152 | +>P : Symbol(P, Decl(deeplyNestedMappedTypes.ts, 38, 82)) |
| 153 | +>K0 : Symbol(K0, Decl(deeplyNestedMappedTypes.ts, 38, 59)) |
| 154 | +>NestedRecord : Symbol(NestedRecord, Decl(deeplyNestedMappedTypes.ts, 30, 40)) |
| 155 | +>KR : Symbol(KR, Decl(deeplyNestedMappedTypes.ts, 38, 71)) |
| 156 | +>V : Symbol(V, Decl(deeplyNestedMappedTypes.ts, 38, 35)) |
| 157 | +>Record : Symbol(Record, Decl(lib.es5.d.ts, --, --)) |
| 158 | +>K : Symbol(K, Decl(deeplyNestedMappedTypes.ts, 38, 18)) |
| 159 | +>V : Symbol(V, Decl(deeplyNestedMappedTypes.ts, 38, 35)) |
| 160 | + |
| 161 | +type Bar1 = NestedRecord<"x.y.z.a.b.c", number>; |
| 162 | +>Bar1 : Symbol(Bar1, Decl(deeplyNestedMappedTypes.ts, 38, 129)) |
| 163 | +>NestedRecord : Symbol(NestedRecord, Decl(deeplyNestedMappedTypes.ts, 30, 40)) |
| 164 | + |
| 165 | +type Bar2 = NestedRecord<"x.y.z.a.b.c", string>; |
| 166 | +>Bar2 : Symbol(Bar2, Decl(deeplyNestedMappedTypes.ts, 40, 48)) |
| 167 | +>NestedRecord : Symbol(NestedRecord, Decl(deeplyNestedMappedTypes.ts, 30, 40)) |
| 168 | + |
| 169 | +declare const bar1: Bar1; |
| 170 | +>bar1 : Symbol(bar1, Decl(deeplyNestedMappedTypes.ts, 43, 13)) |
| 171 | +>Bar1 : Symbol(Bar1, Decl(deeplyNestedMappedTypes.ts, 38, 129)) |
| 172 | + |
| 173 | +const bar2: Bar2 = bar1; // Error expected |
| 174 | +>bar2 : Symbol(bar2, Decl(deeplyNestedMappedTypes.ts, 44, 5)) |
| 175 | +>Bar2 : Symbol(Bar2, Decl(deeplyNestedMappedTypes.ts, 40, 48)) |
| 176 | +>bar1 : Symbol(bar1, Decl(deeplyNestedMappedTypes.ts, 43, 13)) |
| 177 | + |
0 commit comments