1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
|
//// [recursiveTypeReferences1.ts]
type ValueOrArray<T> = T | Array<ValueOrArray<T>>;
const a0: ValueOrArray<number> = 1;
const a1: ValueOrArray<number> = [1, [2, 3], [4, [5, [6, 7]]]];
type HypertextNode = string | [string, { [key: string]: unknown }, ...HypertextNode[]];
const hypertextNode: HypertextNode =
["div", { id: "parent" },
["div", { id: "first-child" }, "I'm the first child"],
["div", { id: "second-child" }, "I'm the second child"]
];
type Json = string | number | boolean | null | Json[] | { [key: string]: Json };
let data: Json = {
caption: "Test",
location: { x: 10, y: 20 },
values: [true, [10, 20], null]
};
interface Box<T> { value: T };
type T1 = Box<T1>;
type T2 = Box<Box<T2>>;
type T3 = Box<Box<Box<T3>>>;
function f1(t1: T1, t2: T2, t3: T3) {
t1 = t2;
t1 = t3;
t2 = t1;
t2 = t3;
t3 = t1;
t3 = t2;
}
type Box1 = Box<Box1> | number;
const b10: Box1 = 42;
const b11: Box1 = { value: 42 };
const b12: Box1 = { value: { value: { value: 42 }}};
type Box2 = Box<Box2 | number>;
const b20: Box2 = 42; // Error
const b21: Box2 = { value: 42 };
const b22: Box2 = { value: { value: { value: 42 }}};
type RecArray<T> = Array<T | RecArray<T>>;
declare function flat<T>(a: RecArray<T>): Array<T>;
declare function flat1<T>(a: Array<T | Array<T>>): Array<T>
declare function flat2<T>(a: Array<T | Array<T | Array<T>>>): Array<T>;
flat([1, [2, [3]]]); // number[]
flat([[[0]]]); // number[]
flat([[[[[[[[[[[4]]]]]]]]]]]); // number[]
flat([1, 'a', [2]]); // (string | number)[]
flat([1, [2, 'a']]); // (string | number)[]
flat([1, ['a']]); // Error
flat1([1, [2, [3]]]); // (number | number[])[]
flat1([[[0]]]); // number[][]
flat1([1, 'a', [2]]); // (string | number)[]
flat1([1, [2, 'a']]); // (string | number)[]
flat1([1, ['a']]); // Error
flat2([1, [2, [3]]]); // number[]
flat2([[[0]]]); // number[]
flat2([1, 'a', [2]]); // (string | number)[]
flat2([1, [2, 'a']]); // (string | number)[]
flat2([1, ['a']]); // Error
type T10 = T10[];
type T11 = readonly T11[];
type T12 = (T12)[];
type T13 = T13[] | string;
type T14 = T14[] & { x: string };
type T15<X> = X extends string ? T15<X>[] : never;
type ValueOrArray1<T> = T | ValueOrArray1<T>[];
type ValueOrArray2<T> = T | ValueOrArray2<T>[];
declare function foo1<T>(a: ValueOrArray1<T>): T;
declare let ra1: ValueOrArray2<string>;
let x1 = foo1(ra1); // Boom!
type NumberOrArray1<T> = T | ValueOrArray1<T>[];
type NumberOrArray2<T> = T | ValueOrArray2<T>[];
declare function foo2<T>(a: ValueOrArray1<T>): T;
declare let ra2: ValueOrArray2<string>;
let x2 = foo2(ra2); // Boom!
// Repro from #33617 (errors are expected)
type Tree = [HTMLHeadingElement, Tree][];
function parse(node: Tree, index: number[] = []): HTMLUListElement {
return html('ul', node.map(([el, children], i) => {
const idx = [...index, i + 1];
return html('li', [
html('a', { href: `#${el.id}`, rel: 'noopener', 'data-index': idx.join('.') }, el.textContent!),
children.length > 0 ? parse(children, idx) : frag()
]);
}));
}
function cons(hs: HTMLHeadingElement[]): Tree {
return hs
.reduce<HTMLHeadingElement[][]>((hss, h) => {
const hs = hss.pop()!;
return hs.length === 0 || level(h) > level(hs[0])
? concat(hss, [concat(hs, [h])])
: concat(hss, [hs, [h]]);
}, [[]])
.reduce<Tree>((node, hs) =>
hs.length === 0
? node
: concat<Tree[number]>(node, [[hs.shift()!, cons(hs)]])
, []);
}
function level(h: HTMLHeadingElement): number {
assert(isFinite(+h.tagName[1]));
return +h.tagName[1];
}
//// [recursiveTypeReferences1.js]
"use strict";
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
var a0 = 1;
var a1 = [1, [2, 3], [4, [5, [6, 7]]]];
var hypertextNode = ["div", { id: "parent" },
["div", { id: "first-child" }, "I'm the first child"],
["div", { id: "second-child" }, "I'm the second child"]
];
var data = {
caption: "Test",
location: { x: 10, y: 20 },
values: [true, [10, 20], null]
};
;
function f1(t1, t2, t3) {
t1 = t2;
t1 = t3;
t2 = t1;
t2 = t3;
t3 = t1;
t3 = t2;
}
var b10 = 42;
var b11 = { value: 42 };
var b12 = { value: { value: { value: 42 } } };
var b20 = 42; // Error
var b21 = { value: 42 };
var b22 = { value: { value: { value: 42 } } };
flat([1, [2, [3]]]); // number[]
flat([[[0]]]); // number[]
flat([[[[[[[[[[[4]]]]]]]]]]]); // number[]
flat([1, 'a', [2]]); // (string | number)[]
flat([1, [2, 'a']]); // (string | number)[]
flat([1, ['a']]); // Error
flat1([1, [2, [3]]]); // (number | number[])[]
flat1([[[0]]]); // number[][]
flat1([1, 'a', [2]]); // (string | number)[]
flat1([1, [2, 'a']]); // (string | number)[]
flat1([1, ['a']]); // Error
flat2([1, [2, [3]]]); // number[]
flat2([[[0]]]); // number[]
flat2([1, 'a', [2]]); // (string | number)[]
flat2([1, [2, 'a']]); // (string | number)[]
flat2([1, ['a']]); // Error
var x1 = foo1(ra1); // Boom!
var x2 = foo2(ra2); // Boom!
function parse(node, index) {
if (index === void 0) { index = []; }
return html('ul', node.map(function (_a, i) {
var el = _a[0], children = _a[1];
var idx = __spreadArray(__spreadArray([], index, true), [i + 1], false);
return html('li', [
html('a', { href: "#".concat(el.id), rel: 'noopener', 'data-index': idx.join('.') }, el.textContent),
children.length > 0 ? parse(children, idx) : frag()
]);
}));
}
function cons(hs) {
return hs
.reduce(function (hss, h) {
var hs = hss.pop();
return hs.length === 0 || level(h) > level(hs[0])
? concat(hss, [concat(hs, [h])])
: concat(hss, [hs, [h]]);
}, [[]])
.reduce(function (node, hs) {
return hs.length === 0
? node
: concat(node, [[hs.shift(), cons(hs)]]);
}, []);
}
function level(h) {
assert(isFinite(+h.tagName[1]));
return +h.tagName[1];
}
//// [recursiveTypeReferences1.d.ts]
type ValueOrArray<T> = T | Array<ValueOrArray<T>>;
declare const a0: ValueOrArray<number>;
declare const a1: ValueOrArray<number>;
type HypertextNode = string | [string, {
[key: string]: unknown;
}, ...HypertextNode[]];
declare const hypertextNode: HypertextNode;
type Json = string | number | boolean | null | Json[] | {
[key: string]: Json;
};
declare let data: Json;
interface Box<T> {
value: T;
}
type T1 = Box<T1>;
type T2 = Box<Box<T2>>;
type T3 = Box<Box<Box<T3>>>;
declare function f1(t1: T1, t2: T2, t3: T3): void;
type Box1 = Box<Box1> | number;
declare const b10: Box1;
declare const b11: Box1;
declare const b12: Box1;
type Box2 = Box<Box2 | number>;
declare const b20: Box2;
declare const b21: Box2;
declare const b22: Box2;
type RecArray<T> = Array<T | RecArray<T>>;
declare function flat<T>(a: RecArray<T>): Array<T>;
declare function flat1<T>(a: Array<T | Array<T>>): Array<T>;
declare function flat2<T>(a: Array<T | Array<T | Array<T>>>): Array<T>;
type T10 = T10[];
type T11 = readonly T11[];
type T12 = (T12)[];
type T13 = T13[] | string;
type T14 = T14[] & {
x: string;
};
type T15<X> = X extends string ? T15<X>[] : never;
type ValueOrArray1<T> = T | ValueOrArray1<T>[];
type ValueOrArray2<T> = T | ValueOrArray2<T>[];
declare function foo1<T>(a: ValueOrArray1<T>): T;
declare let ra1: ValueOrArray2<string>;
declare let x1: string;
type NumberOrArray1<T> = T | ValueOrArray1<T>[];
type NumberOrArray2<T> = T | ValueOrArray2<T>[];
declare function foo2<T>(a: ValueOrArray1<T>): T;
declare let ra2: ValueOrArray2<string>;
declare let x2: string;
type Tree = [HTMLHeadingElement, Tree][];
declare function parse(node: Tree, index?: number[]): HTMLUListElement;
declare function cons(hs: HTMLHeadingElement[]): Tree;
declare function level(h: HTMLHeadingElement): number;
|