|
| 1 | +// @strict: true |
| 2 | +// @declaration: true |
| 3 | + |
| 4 | +type ValueOrArray<T> = T | Array<ValueOrArray<T>>; |
| 5 | + |
| 6 | +const a0: ValueOrArray<number> = 1; |
| 7 | +const a1: ValueOrArray<number> = [1, [2, 3], [4, [5, [6, 7]]]]; |
| 8 | + |
| 9 | +type HypertextNode = string | [string, { [key: string]: unknown }, ...HypertextNode[]]; |
| 10 | + |
| 11 | +const hypertextNode: HypertextNode = |
| 12 | + ["div", { id: "parent" }, |
| 13 | + ["div", { id: "first-child" }, "I'm the first child"], |
| 14 | + ["div", { id: "second-child" }, "I'm the second child"] |
| 15 | + ]; |
| 16 | + |
| 17 | +type Json = string | number | boolean | null | Json[] | { [key: string]: Json }; |
| 18 | + |
| 19 | +let data: Json = { |
| 20 | + caption: "Test", |
| 21 | + location: { x: 10, y: 20 }, |
| 22 | + values: [true, [10, 20], null] |
| 23 | +}; |
| 24 | + |
| 25 | +interface Box<T> { value: T }; |
| 26 | + |
| 27 | +type T1 = Box<T1>; |
| 28 | +type T2 = Box<Box<T2>>; |
| 29 | +type T3 = Box<Box<Box<T3>>>; |
| 30 | + |
| 31 | +function f1(t1: T1, t2: T2, t3: T3) { |
| 32 | + t1 = t2; |
| 33 | + t1 = t3; |
| 34 | + t2 = t1; |
| 35 | + t2 = t3; |
| 36 | + t3 = t1; |
| 37 | + t3 = t2; |
| 38 | +} |
| 39 | + |
| 40 | +type Box1 = Box<Box1> | number; |
| 41 | + |
| 42 | +const b10: Box1 = 42; |
| 43 | +const b11: Box1 = { value: 42 }; |
| 44 | +const b12: Box1 = { value: { value: { value: 42 }}}; |
| 45 | + |
| 46 | +type Box2 = Box<Box2 | number>; |
| 47 | + |
| 48 | +const b20: Box2 = 42; // Error |
| 49 | +const b21: Box2 = { value: 42 }; |
| 50 | +const b22: Box2 = { value: { value: { value: 42 }}}; |
| 51 | + |
| 52 | +type RecArray<T> = Array<T | RecArray<T>>; |
| 53 | + |
| 54 | +declare function flat<T>(a: RecArray<T>): Array<T>; |
| 55 | +declare function flat1<T>(a: Array<T | Array<T>>): Array<T> |
| 56 | +declare function flat2<T>(a: Array<T | Array<T | Array<T>>>): Array<T>; |
| 57 | + |
| 58 | +flat([1, [2, [3]]]); // number[] |
| 59 | +flat([[[0]]]); // number[] |
| 60 | +flat([[[[[[[[[[[4]]]]]]]]]]]); // number[] |
| 61 | +flat([1, 'a', [2]]); // (string | number)[] |
| 62 | +flat([1, [2, 'a']]); // (string | number)[] |
| 63 | +flat([1, ['a']]); // Error |
| 64 | + |
| 65 | +flat1([1, [2, [3]]]); // (number | number[])[] |
| 66 | +flat1([[[0]]]); // number[][] |
| 67 | +flat1([1, 'a', [2]]); // (string | number)[] |
| 68 | +flat1([1, [2, 'a']]); // (string | number)[] |
| 69 | +flat1([1, ['a']]); // Error |
| 70 | + |
| 71 | +flat2([1, [2, [3]]]); // number[] |
| 72 | +flat2([[[0]]]); // number[] |
| 73 | +flat2([1, 'a', [2]]); // (string | number)[] |
| 74 | +flat2([1, [2, 'a']]); // (string | number)[] |
| 75 | +flat2([1, ['a']]); // Error |
| 76 | + |
| 77 | +type T10 = T10[]; |
| 78 | +type T11 = readonly T11[]; |
| 79 | +type T12 = (T12)[]; |
| 80 | +type T13 = T13[] | string; |
| 81 | +type T14 = T14[] & { x: string }; |
| 82 | +type T15<X> = X extends string ? T15<X>[] : never; |
0 commit comments