|
| 1 | +//// [recursiveTypeReferences1.ts] |
| 2 | +type ValueOrArray<T> = T | Array<ValueOrArray<T>>; |
| 3 | + |
| 4 | +const a0: ValueOrArray<number> = 1; |
| 5 | +const a1: ValueOrArray<number> = [1, [2, 3], [4, [5, [6, 7]]]]; |
| 6 | + |
| 7 | +type HypertextNode = string | [string, { [key: string]: unknown }, ...HypertextNode[]]; |
| 8 | + |
| 9 | +const hypertextNode: HypertextNode = |
| 10 | + ["div", { id: "parent" }, |
| 11 | + ["div", { id: "first-child" }, "I'm the first child"], |
| 12 | + ["div", { id: "second-child" }, "I'm the second child"] |
| 13 | + ]; |
| 14 | + |
| 15 | +type Json = string | number | boolean | null | Json[] | { [key: string]: Json }; |
| 16 | + |
| 17 | +let data: Json = { |
| 18 | + caption: "Test", |
| 19 | + location: { x: 10, y: 20 }, |
| 20 | + values: [true, [10, 20], null] |
| 21 | +}; |
| 22 | + |
| 23 | +interface Box<T> { value: T }; |
| 24 | + |
| 25 | +type T1 = Box<T1>; |
| 26 | +type T2 = Box<Box<T2>>; |
| 27 | +type T3 = Box<Box<Box<T3>>>; |
| 28 | + |
| 29 | +function f1(t1: T1, t2: T2, t3: T3) { |
| 30 | + t1 = t2; |
| 31 | + t1 = t3; |
| 32 | + t2 = t1; |
| 33 | + t2 = t3; |
| 34 | + t3 = t1; |
| 35 | + t3 = t2; |
| 36 | +} |
| 37 | + |
| 38 | +type Box1 = Box<Box1> | number; |
| 39 | + |
| 40 | +const b10: Box1 = 42; |
| 41 | +const b11: Box1 = { value: 42 }; |
| 42 | +const b12: Box1 = { value: { value: { value: 42 }}}; |
| 43 | + |
| 44 | +type Box2 = Box<Box2 | number>; |
| 45 | + |
| 46 | +const b20: Box2 = 42; // Error |
| 47 | +const b21: Box2 = { value: 42 }; |
| 48 | +const b22: Box2 = { value: { value: { value: 42 }}}; |
| 49 | + |
| 50 | +type RecArray<T> = Array<T | RecArray<T>>; |
| 51 | + |
| 52 | +declare function flat<T>(a: RecArray<T>): Array<T>; |
| 53 | +declare function flat1<T>(a: Array<T | Array<T>>): Array<T> |
| 54 | +declare function flat2<T>(a: Array<T | Array<T | Array<T>>>): Array<T>; |
| 55 | + |
| 56 | +flat([1, [2, [3]]]); // number[] |
| 57 | +flat([[[0]]]); // number[] |
| 58 | +flat([[[[[[[[[[[4]]]]]]]]]]]); // number[] |
| 59 | +flat([1, 'a', [2]]); // (string | number)[] |
| 60 | +flat([1, [2, 'a']]); // (string | number)[] |
| 61 | +flat([1, ['a']]); // Error |
| 62 | + |
| 63 | +flat1([1, [2, [3]]]); // (number | number[])[] |
| 64 | +flat1([[[0]]]); // number[][] |
| 65 | +flat1([1, 'a', [2]]); // (string | number)[] |
| 66 | +flat1([1, [2, 'a']]); // (string | number)[] |
| 67 | +flat1([1, ['a']]); // Error |
| 68 | + |
| 69 | +flat2([1, [2, [3]]]); // number[] |
| 70 | +flat2([[[0]]]); // number[] |
| 71 | +flat2([1, 'a', [2]]); // (string | number)[] |
| 72 | +flat2([1, [2, 'a']]); // (string | number)[] |
| 73 | +flat2([1, ['a']]); // Error |
| 74 | + |
| 75 | +type T10 = T10[]; |
| 76 | +type T11 = readonly T11[]; |
| 77 | +type T12 = (T12)[]; |
| 78 | +type T13 = T13[] | string; |
| 79 | +type T14 = T14[] & { x: string }; |
| 80 | +type T15<X> = X extends string ? T15<X>[] : never; |
| 81 | + |
| 82 | + |
| 83 | +//// [recursiveTypeReferences1.js] |
| 84 | +"use strict"; |
| 85 | +var a0 = 1; |
| 86 | +var a1 = [1, [2, 3], [4, [5, [6, 7]]]]; |
| 87 | +var hypertextNode = ["div", { id: "parent" }, |
| 88 | + ["div", { id: "first-child" }, "I'm the first child"], |
| 89 | + ["div", { id: "second-child" }, "I'm the second child"] |
| 90 | +]; |
| 91 | +var data = { |
| 92 | + caption: "Test", |
| 93 | + location: { x: 10, y: 20 }, |
| 94 | + values: [true, [10, 20], null] |
| 95 | +}; |
| 96 | +; |
| 97 | +function f1(t1, t2, t3) { |
| 98 | + t1 = t2; |
| 99 | + t1 = t3; |
| 100 | + t2 = t1; |
| 101 | + t2 = t3; |
| 102 | + t3 = t1; |
| 103 | + t3 = t2; |
| 104 | +} |
| 105 | +var b10 = 42; |
| 106 | +var b11 = { value: 42 }; |
| 107 | +var b12 = { value: { value: { value: 42 } } }; |
| 108 | +var b20 = 42; // Error |
| 109 | +var b21 = { value: 42 }; |
| 110 | +var b22 = { value: { value: { value: 42 } } }; |
| 111 | +flat([1, [2, [3]]]); // number[] |
| 112 | +flat([[[0]]]); // number[] |
| 113 | +flat([[[[[[[[[[[4]]]]]]]]]]]); // number[] |
| 114 | +flat([1, 'a', [2]]); // (string | number)[] |
| 115 | +flat([1, [2, 'a']]); // (string | number)[] |
| 116 | +flat([1, ['a']]); // Error |
| 117 | +flat1([1, [2, [3]]]); // (number | number[])[] |
| 118 | +flat1([[[0]]]); // number[][] |
| 119 | +flat1([1, 'a', [2]]); // (string | number)[] |
| 120 | +flat1([1, [2, 'a']]); // (string | number)[] |
| 121 | +flat1([1, ['a']]); // Error |
| 122 | +flat2([1, [2, [3]]]); // number[] |
| 123 | +flat2([[[0]]]); // number[] |
| 124 | +flat2([1, 'a', [2]]); // (string | number)[] |
| 125 | +flat2([1, [2, 'a']]); // (string | number)[] |
| 126 | +flat2([1, ['a']]); // Error |
| 127 | + |
| 128 | + |
| 129 | +//// [recursiveTypeReferences1.d.ts] |
| 130 | +declare type ValueOrArray<T> = T | Array<ValueOrArray<T>>; |
| 131 | +declare const a0: ValueOrArray<number>; |
| 132 | +declare const a1: ValueOrArray<number>; |
| 133 | +declare type HypertextNode = string | [string, { |
| 134 | + [key: string]: unknown; |
| 135 | +}, ...HypertextNode[]]; |
| 136 | +declare const hypertextNode: HypertextNode; |
| 137 | +declare type Json = string | number | boolean | null | Json[] | { |
| 138 | + [key: string]: Json; |
| 139 | +}; |
| 140 | +declare let data: Json; |
| 141 | +interface Box<T> { |
| 142 | + value: T; |
| 143 | +} |
| 144 | +declare type T1 = Box<T1>; |
| 145 | +declare type T2 = Box<Box<T2>>; |
| 146 | +declare type T3 = Box<Box<Box<T3>>>; |
| 147 | +declare function f1(t1: T1, t2: T2, t3: T3): void; |
| 148 | +declare type Box1 = Box<Box1> | number; |
| 149 | +declare const b10: Box1; |
| 150 | +declare const b11: Box1; |
| 151 | +declare const b12: Box1; |
| 152 | +declare type Box2 = Box<Box2 | number>; |
| 153 | +declare const b20: Box2; |
| 154 | +declare const b21: Box2; |
| 155 | +declare const b22: Box2; |
| 156 | +declare type RecArray<T> = Array<T | RecArray<T>>; |
| 157 | +declare function flat<T>(a: RecArray<T>): Array<T>; |
| 158 | +declare function flat1<T>(a: Array<T | Array<T>>): Array<T>; |
| 159 | +declare function flat2<T>(a: Array<T | Array<T | Array<T>>>): Array<T>; |
| 160 | +declare type T10 = T10[]; |
| 161 | +declare type T11 = readonly T11[]; |
| 162 | +declare type T12 = (T12)[]; |
| 163 | +declare type T13 = T13[] | string; |
| 164 | +declare type T14 = T14[] & { |
| 165 | + x: string; |
| 166 | +}; |
| 167 | +declare type T15<X> = X extends string ? T15<X>[] : never; |
0 commit comments