From e3d23cc40e158ca4f1085a6e4584c78818bac70c Mon Sep 17 00:00:00 2001 From: Anders Hejlsberg Date: Tue, 24 Sep 2019 06:55:38 -0700 Subject: [PATCH] Add tests --- .../recursiveTypeReferences1.ts | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts diff --git a/tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts b/tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts new file mode 100644 index 00000000000..16e23284963 --- /dev/null +++ b/tests/cases/conformance/types/typeRelationships/recursiveTypes/recursiveTypeReferences1.ts @@ -0,0 +1,82 @@ +// @strict: true +// @declaration: true + +type ValueOrArray = T | Array>; + +const a0: ValueOrArray = 1; +const a1: ValueOrArray = [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 { value: T }; + +type T1 = Box; +type T2 = Box>; +type T3 = Box>>; + +function f1(t1: T1, t2: T2, t3: T3) { + t1 = t2; + t1 = t3; + t2 = t1; + t2 = t3; + t3 = t1; + t3 = t2; +} + +type Box1 = Box | number; + +const b10: Box1 = 42; +const b11: Box1 = { value: 42 }; +const b12: Box1 = { value: { value: { value: 42 }}}; + +type Box2 = Box; + +const b20: Box2 = 42; // Error +const b21: Box2 = { value: 42 }; +const b22: Box2 = { value: { value: { value: 42 }}}; + +type RecArray = Array>; + +declare function flat(a: RecArray): Array; +declare function flat1(a: Array>): Array +declare function flat2(a: Array>>): Array; + +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 extends string ? T15[] : never;