1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
tests/cases/conformance/types/union/discriminatedUnionTypes1.ts(89,9): error TS2367: This condition will always return 'false' since the types '"A" | "B" | "C" | "D"' and '"X"' have no overlap.
==== tests/cases/conformance/types/union/discriminatedUnionTypes1.ts (1 errors) ====
interface Square {
kind: "square";
size: number;
}
interface Rectangle {
kind: "rectangle";
width: number;
height: number;
}
interface Circle {
kind: "circle";
radius: number;
}
type Shape = Square | Rectangle | Circle;
function area1(s: Shape) {
if (s.kind === "square") {
return s.size * s.size;
}
else if (s.kind === "circle") {
return Math.PI * s.radius * s.radius;
}
else if (s.kind === "rectangle") {
return s.width * s.height;
}
else {
return 0;
}
}
function area2(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
}
function assertNever(x: never): never {
throw new Error("Unexpected object: " + x);
}
function area3(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
default: return assertNever(s);
}
}
function area4(s: Shape) {
switch (s.kind) {
case "square": return s.size * s.size;
case "rectangle": return s.width * s.height;
case "circle": return Math.PI * s.radius * s.radius;
}
return assertNever(s);
}
type Message =
{ kind: "A", x: string } |
{ kind: "B" | "C", y: number } |
{ kind: "D" };
function f1(m: Message) {
if (m.kind === "A") {
m; // { kind: "A", x: string }
}
else if (m.kind === "D") {
m; // { kind: "D" }
}
else {
m; // { kind: "B" | "C", y: number }
}
}
function f2(m: Message) {
if (m.kind === "A") {
return;
}
m; // { kind: "B" | "C", y: number } | { kind: "D" }
}
function f3(m: Message) {
if (m.kind === "X") {
~~~~~~~~~~~~~~
!!! error TS2367: This condition will always return 'false' since the types '"A" | "B" | "C" | "D"' and '"X"' have no overlap.
m; // never
}
}
function f4(m: Message, x: "A" | "D") {
if (m.kind == x) {
m; // { kind: "A", x: string } | { kind: "D" }
}
}
function f5(m: Message) {
switch (m.kind) {
case "A":
m; // { kind: "A", x: string }
break;
case "D":
m; // { kind: "D" }
break;
default:
m; // { kind: "B" | "C", y: number }
}
}
function f6(m: Message) {
switch (m.kind) {
case "A":
m; // { kind: "A", x: string }
case "D":
m; // { kind: "A", x: string } | { kind: "D" }
break;
default:
m; // { kind: "B" | "C", y: number }
}
}
function f7(m: Message) {
switch (m.kind) {
case "A":
case "B":
return;
}
m; // { kind: "B" | "C", y: number } | { kind: "D" }
}
function f8(m: Message) {
switch (m.kind) {
case "A":
return;
case "D":
throw new Error();
}
m; // { kind: "B" | "C", y: number }
}
|