|
| 1 | +=== tests/cases/compiler/subtypeReductionUnionConstraints.ts === |
| 2 | +// Repro from #53311 |
| 3 | + |
| 4 | +type FooNode = { |
| 5 | +>FooNode : Symbol(FooNode, Decl(subtypeReductionUnionConstraints.ts, 0, 0)) |
| 6 | + |
| 7 | + kind: 'foo'; |
| 8 | +>kind : Symbol(kind, Decl(subtypeReductionUnionConstraints.ts, 2, 16)) |
| 9 | + |
| 10 | + children: Node[]; |
| 11 | +>children : Symbol(children, Decl(subtypeReductionUnionConstraints.ts, 3, 16)) |
| 12 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 13 | + |
| 14 | +}; |
| 15 | + |
| 16 | +type BarNode = { |
| 17 | +>BarNode : Symbol(BarNode, Decl(subtypeReductionUnionConstraints.ts, 5, 2)) |
| 18 | + |
| 19 | + kind: 'bar'; |
| 20 | +>kind : Symbol(kind, Decl(subtypeReductionUnionConstraints.ts, 7, 16)) |
| 21 | +} |
| 22 | + |
| 23 | +type Node = FooNode | BarNode; |
| 24 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 25 | +>FooNode : Symbol(FooNode, Decl(subtypeReductionUnionConstraints.ts, 0, 0)) |
| 26 | +>BarNode : Symbol(BarNode, Decl(subtypeReductionUnionConstraints.ts, 5, 2)) |
| 27 | + |
| 28 | +type Document = { |
| 29 | +>Document : Symbol(Document, Decl(subtypeReductionUnionConstraints.ts, 11, 30)) |
| 30 | + |
| 31 | + kind: 'document'; |
| 32 | +>kind : Symbol(kind, Decl(subtypeReductionUnionConstraints.ts, 13, 17)) |
| 33 | + |
| 34 | + children: Node[]; |
| 35 | +>children : Symbol(children, Decl(subtypeReductionUnionConstraints.ts, 14, 21)) |
| 36 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 37 | + |
| 38 | +}; |
| 39 | + |
| 40 | +declare function isNode(node: unknown): node is Node; |
| 41 | +>isNode : Symbol(isNode, Decl(subtypeReductionUnionConstraints.ts, 16, 2)) |
| 42 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 18, 24)) |
| 43 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 18, 24)) |
| 44 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 45 | + |
| 46 | +declare function isBar(node: Node): node is BarNode; |
| 47 | +>isBar : Symbol(isBar, Decl(subtypeReductionUnionConstraints.ts, 18, 53)) |
| 48 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 19, 23)) |
| 49 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 50 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 19, 23)) |
| 51 | +>BarNode : Symbol(BarNode, Decl(subtypeReductionUnionConstraints.ts, 5, 2)) |
| 52 | + |
| 53 | +export function visitNodes<T extends Node>(node: Document | Node, predicate: (testNode: Node) => testNode is T): void { |
| 54 | +>visitNodes : Symbol(visitNodes, Decl(subtypeReductionUnionConstraints.ts, 19, 52)) |
| 55 | +>T : Symbol(T, Decl(subtypeReductionUnionConstraints.ts, 21, 27)) |
| 56 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 57 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 21, 43)) |
| 58 | +>Document : Symbol(Document, Decl(subtypeReductionUnionConstraints.ts, 11, 30)) |
| 59 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 60 | +>predicate : Symbol(predicate, Decl(subtypeReductionUnionConstraints.ts, 21, 65)) |
| 61 | +>testNode : Symbol(testNode, Decl(subtypeReductionUnionConstraints.ts, 21, 78)) |
| 62 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 63 | +>testNode : Symbol(testNode, Decl(subtypeReductionUnionConstraints.ts, 21, 78)) |
| 64 | +>T : Symbol(T, Decl(subtypeReductionUnionConstraints.ts, 21, 27)) |
| 65 | + |
| 66 | + isNode(node) && predicate(node); |
| 67 | +>isNode : Symbol(isNode, Decl(subtypeReductionUnionConstraints.ts, 16, 2)) |
| 68 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 21, 43)) |
| 69 | +>predicate : Symbol(predicate, Decl(subtypeReductionUnionConstraints.ts, 21, 65)) |
| 70 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 21, 43)) |
| 71 | + |
| 72 | + if (!isNode(node) || !isBar(node)) { |
| 73 | +>isNode : Symbol(isNode, Decl(subtypeReductionUnionConstraints.ts, 16, 2)) |
| 74 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 21, 43)) |
| 75 | +>isBar : Symbol(isBar, Decl(subtypeReductionUnionConstraints.ts, 18, 53)) |
| 76 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 21, 43)) |
| 77 | + |
| 78 | + const nodes: Node[] = node.children; |
| 79 | +>nodes : Symbol(nodes, Decl(subtypeReductionUnionConstraints.ts, 24, 13)) |
| 80 | +>Node : Symbol(Node, Decl(subtypeReductionUnionConstraints.ts, 9, 1)) |
| 81 | +>node.children : Symbol(children, Decl(subtypeReductionUnionConstraints.ts, 3, 16), Decl(subtypeReductionUnionConstraints.ts, 14, 21)) |
| 82 | +>node : Symbol(node, Decl(subtypeReductionUnionConstraints.ts, 21, 43)) |
| 83 | +>children : Symbol(children, Decl(subtypeReductionUnionConstraints.ts, 3, 16), Decl(subtypeReductionUnionConstraints.ts, 14, 21)) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// Repro from #53311 |
| 88 | + |
| 89 | +type A = { a: string }; |
| 90 | +>A : Symbol(A, Decl(subtypeReductionUnionConstraints.ts, 26, 1)) |
| 91 | +>a : Symbol(a, Decl(subtypeReductionUnionConstraints.ts, 30, 10)) |
| 92 | + |
| 93 | +type B = { b: string }; |
| 94 | +>B : Symbol(B, Decl(subtypeReductionUnionConstraints.ts, 30, 23)) |
| 95 | +>b : Symbol(b, Decl(subtypeReductionUnionConstraints.ts, 31, 10)) |
| 96 | + |
| 97 | +function f1<T extends A | B>(t: T, x: A | B) { |
| 98 | +>f1 : Symbol(f1, Decl(subtypeReductionUnionConstraints.ts, 31, 23)) |
| 99 | +>T : Symbol(T, Decl(subtypeReductionUnionConstraints.ts, 33, 12)) |
| 100 | +>A : Symbol(A, Decl(subtypeReductionUnionConstraints.ts, 26, 1)) |
| 101 | +>B : Symbol(B, Decl(subtypeReductionUnionConstraints.ts, 30, 23)) |
| 102 | +>t : Symbol(t, Decl(subtypeReductionUnionConstraints.ts, 33, 29)) |
| 103 | +>T : Symbol(T, Decl(subtypeReductionUnionConstraints.ts, 33, 12)) |
| 104 | +>x : Symbol(x, Decl(subtypeReductionUnionConstraints.ts, 33, 34)) |
| 105 | +>A : Symbol(A, Decl(subtypeReductionUnionConstraints.ts, 26, 1)) |
| 106 | +>B : Symbol(B, Decl(subtypeReductionUnionConstraints.ts, 30, 23)) |
| 107 | + |
| 108 | + const a = [t, x]; // (A | B)[] by subtype reduction |
| 109 | +>a : Symbol(a, Decl(subtypeReductionUnionConstraints.ts, 34, 9)) |
| 110 | +>t : Symbol(t, Decl(subtypeReductionUnionConstraints.ts, 33, 29)) |
| 111 | +>x : Symbol(x, Decl(subtypeReductionUnionConstraints.ts, 33, 34)) |
| 112 | +} |
| 113 | + |
0 commit comments