From 46bddedb4b622d731658d0c6a52f34acd876b2de Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Mon, 30 Sep 2019 09:28:19 -0700 Subject: [PATCH] Add regression tests --- .../recursiveTypeReferences1.ts | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts b/tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts index 16e23284963..c4d7cbc1694 100644 --- a/tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts +++ b/tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts @@ -80,3 +80,53 @@ type T12 = (T12)[]; type T13 = T13[] | string; type T14 = T14[] & { x: string }; type T15 = X extends string ? T15[] : never; + +type ValueOrArray1 = T | ValueOrArray1[]; +type ValueOrArray2 = T | ValueOrArray2[]; + +declare function foo1(a: ValueOrArray1): T; +declare let ra1: ValueOrArray2; + +let x1 = foo1(ra1); // Boom! + +type NumberOrArray1 = T | ValueOrArray1[]; +type NumberOrArray2 = T | ValueOrArray2[]; + +declare function foo2(a: ValueOrArray1): T; +declare let ra2: ValueOrArray2; + +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((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((node, hs) => + hs.length === 0 + ? node + : concat(node, [[hs.shift()!, cons(hs)]]) + , []); +} + +function level(h: HTMLHeadingElement): number { + assert(isFinite(+h.tagName[1])); + return +h.tagName[1]; +}