|
| 1 | +=== tests/cases/compiler/typePredicatesInUnion3.ts === |
| 2 | +// A union of function types is considered a type predicate if at least one constituent is a type |
| 3 | +// predicate and the other constituents are matching type predicates or functions returning `false`. |
| 4 | + |
| 5 | +type P1 = (x: unknown) => x is string; |
| 6 | +>P1 : Symbol(P1, Decl(typePredicatesInUnion3.ts, 0, 0)) |
| 7 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 3, 11)) |
| 8 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 3, 11)) |
| 9 | + |
| 10 | +type P2 = (x: unknown) => x is number; |
| 11 | +>P2 : Symbol(P2, Decl(typePredicatesInUnion3.ts, 3, 38)) |
| 12 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 4, 11)) |
| 13 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 4, 11)) |
| 14 | + |
| 15 | +type F1 = (x: unknown) => false; |
| 16 | +>F1 : Symbol(F1, Decl(typePredicatesInUnion3.ts, 4, 38)) |
| 17 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 6, 11)) |
| 18 | + |
| 19 | +type F2 = (x: unknown) => boolean; |
| 20 | +>F2 : Symbol(F2, Decl(typePredicatesInUnion3.ts, 6, 32)) |
| 21 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 7, 11)) |
| 22 | + |
| 23 | +type F3 = (x: unknown) => string; |
| 24 | +>F3 : Symbol(F3, Decl(typePredicatesInUnion3.ts, 7, 34)) |
| 25 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 8, 11)) |
| 26 | + |
| 27 | +function f1(x: unknown, p: P1 | P2) { |
| 28 | +>f1 : Symbol(f1, Decl(typePredicatesInUnion3.ts, 8, 33)) |
| 29 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 10, 12)) |
| 30 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 10, 23)) |
| 31 | +>P1 : Symbol(P1, Decl(typePredicatesInUnion3.ts, 0, 0)) |
| 32 | +>P2 : Symbol(P2, Decl(typePredicatesInUnion3.ts, 3, 38)) |
| 33 | + |
| 34 | + if (p(x)) { |
| 35 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 10, 23)) |
| 36 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 10, 12)) |
| 37 | + |
| 38 | + x; // string | number |
| 39 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 10, 12)) |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function f2(x: unknown, p: P1 | P2 | F1) { |
| 44 | +>f2 : Symbol(f2, Decl(typePredicatesInUnion3.ts, 14, 1)) |
| 45 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 16, 12)) |
| 46 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 16, 23)) |
| 47 | +>P1 : Symbol(P1, Decl(typePredicatesInUnion3.ts, 0, 0)) |
| 48 | +>P2 : Symbol(P2, Decl(typePredicatesInUnion3.ts, 3, 38)) |
| 49 | +>F1 : Symbol(F1, Decl(typePredicatesInUnion3.ts, 4, 38)) |
| 50 | + |
| 51 | + if (p(x)) { |
| 52 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 16, 23)) |
| 53 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 16, 12)) |
| 54 | + |
| 55 | + x; // string | number |
| 56 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 16, 12)) |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +function f3(x: unknown, p: P1 | P2 | F2) { |
| 61 | +>f3 : Symbol(f3, Decl(typePredicatesInUnion3.ts, 20, 1)) |
| 62 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 22, 12)) |
| 63 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 22, 23)) |
| 64 | +>P1 : Symbol(P1, Decl(typePredicatesInUnion3.ts, 0, 0)) |
| 65 | +>P2 : Symbol(P2, Decl(typePredicatesInUnion3.ts, 3, 38)) |
| 66 | +>F2 : Symbol(F2, Decl(typePredicatesInUnion3.ts, 6, 32)) |
| 67 | + |
| 68 | + if (p(x)) { |
| 69 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 22, 23)) |
| 70 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 22, 12)) |
| 71 | + |
| 72 | + x; // unknown |
| 73 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 22, 12)) |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +function f4(x: unknown, p: P1 | P2 | F3) { |
| 78 | +>f4 : Symbol(f4, Decl(typePredicatesInUnion3.ts, 26, 1)) |
| 79 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 28, 12)) |
| 80 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 28, 23)) |
| 81 | +>P1 : Symbol(P1, Decl(typePredicatesInUnion3.ts, 0, 0)) |
| 82 | +>P2 : Symbol(P2, Decl(typePredicatesInUnion3.ts, 3, 38)) |
| 83 | +>F3 : Symbol(F3, Decl(typePredicatesInUnion3.ts, 7, 34)) |
| 84 | + |
| 85 | + if (p(x)) { |
| 86 | +>p : Symbol(p, Decl(typePredicatesInUnion3.ts, 28, 23)) |
| 87 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 28, 12)) |
| 88 | + |
| 89 | + x; // unknown |
| 90 | +>x : Symbol(x, Decl(typePredicatesInUnion3.ts, 28, 12)) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// Repro from #54143 |
| 95 | + |
| 96 | +type HasAttribute<T> = T & { attribute: number }; |
| 97 | +>HasAttribute : Symbol(HasAttribute, Decl(typePredicatesInUnion3.ts, 32, 1)) |
| 98 | +>T : Symbol(T, Decl(typePredicatesInUnion3.ts, 36, 18)) |
| 99 | +>T : Symbol(T, Decl(typePredicatesInUnion3.ts, 36, 18)) |
| 100 | +>attribute : Symbol(attribute, Decl(typePredicatesInUnion3.ts, 36, 28)) |
| 101 | + |
| 102 | +class Type1 { |
| 103 | +>Type1 : Symbol(Type1, Decl(typePredicatesInUnion3.ts, 36, 49)) |
| 104 | + |
| 105 | + attribute: number | null = null; |
| 106 | +>attribute : Symbol(Type1.attribute, Decl(typePredicatesInUnion3.ts, 38, 13)) |
| 107 | + |
| 108 | + predicate(): this is HasAttribute<Type1> { |
| 109 | +>predicate : Symbol(Type1.predicate, Decl(typePredicatesInUnion3.ts, 39, 36)) |
| 110 | +>HasAttribute : Symbol(HasAttribute, Decl(typePredicatesInUnion3.ts, 32, 1)) |
| 111 | +>Type1 : Symbol(Type1, Decl(typePredicatesInUnion3.ts, 36, 49)) |
| 112 | + |
| 113 | + return true; |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +class Type2 { |
| 118 | +>Type2 : Symbol(Type2, Decl(typePredicatesInUnion3.ts, 43, 1)) |
| 119 | + |
| 120 | + attribute: number | null = null; |
| 121 | +>attribute : Symbol(Type2.attribute, Decl(typePredicatesInUnion3.ts, 45, 13)) |
| 122 | + |
| 123 | + predicate(): boolean { |
| 124 | +>predicate : Symbol(Type2.predicate, Decl(typePredicatesInUnion3.ts, 46, 36)) |
| 125 | + |
| 126 | + return true; |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +function assertType<T>(_val: T) { |
| 131 | +>assertType : Symbol(assertType, Decl(typePredicatesInUnion3.ts, 50, 1)) |
| 132 | +>T : Symbol(T, Decl(typePredicatesInUnion3.ts, 52, 20)) |
| 133 | +>_val : Symbol(_val, Decl(typePredicatesInUnion3.ts, 52, 23)) |
| 134 | +>T : Symbol(T, Decl(typePredicatesInUnion3.ts, 52, 20)) |
| 135 | +} |
| 136 | + |
| 137 | +declare const val: Type1 | Type2; |
| 138 | +>val : Symbol(val, Decl(typePredicatesInUnion3.ts, 55, 13)) |
| 139 | +>Type1 : Symbol(Type1, Decl(typePredicatesInUnion3.ts, 36, 49)) |
| 140 | +>Type2 : Symbol(Type2, Decl(typePredicatesInUnion3.ts, 43, 1)) |
| 141 | + |
| 142 | +if (val.predicate()) { |
| 143 | +>val.predicate : Symbol(predicate, Decl(typePredicatesInUnion3.ts, 39, 36), Decl(typePredicatesInUnion3.ts, 46, 36)) |
| 144 | +>val : Symbol(val, Decl(typePredicatesInUnion3.ts, 55, 13)) |
| 145 | +>predicate : Symbol(predicate, Decl(typePredicatesInUnion3.ts, 39, 36), Decl(typePredicatesInUnion3.ts, 46, 36)) |
| 146 | + |
| 147 | + assertType<number>(val.attribute); // Error |
| 148 | +>assertType : Symbol(assertType, Decl(typePredicatesInUnion3.ts, 50, 1)) |
| 149 | +>val.attribute : Symbol(attribute, Decl(typePredicatesInUnion3.ts, 38, 13), Decl(typePredicatesInUnion3.ts, 45, 13)) |
| 150 | +>val : Symbol(val, Decl(typePredicatesInUnion3.ts, 55, 13)) |
| 151 | +>attribute : Symbol(attribute, Decl(typePredicatesInUnion3.ts, 38, 13), Decl(typePredicatesInUnion3.ts, 45, 13)) |
| 152 | +} |
| 153 | + |
0 commit comments