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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(7,9): error TS7027: Unreachable code detected.
tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(235,5): error TS2367: This comparison appears to be unintentional because the types 'keyof O' and '"c"' have no overlap.
tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts(247,10): error TS2339: Property 'kind' does not exist on type 'never'.
==== tests/cases/conformance/controlFlow/exhaustiveSwitchStatements1.ts (3 errors) ====
function f1(x: 1 | 2): string {
if (!!true) {
switch (x) {
case 1: return 'a';
case 2: return 'b';
}
x; // Unreachable
~~
!!! error TS7027: Unreachable code detected.
}
else {
throw 0;
}
}
function f2(x: 1 | 2) {
let z: number;
switch (x) {
case 1: z = 10; break;
case 2: z = 20; break;
}
z; // Definitely assigned
}
function f3(x: 1 | 2) {
switch (x) {
case 1: return 10;
case 2: return 20;
// Default considered reachable to allow defensive coding
default: throw new Error("Bad input");
}
}
// Repro from #11572
enum E { A, B }
function f(e: E): number {
switch (e) {
case E.A: return 0
case E.B: return 1
}
}
function g(e: E): number {
if (!true)
return -1
else
switch (e) {
case E.A: return 0
case E.B: return 1
}
}
// Repro from #12668
interface Square { kind: "square"; size: number; }
interface Rectangle { kind: "rectangle"; width: number; height: number; }
interface Circle { kind: "circle"; radius: number; }
interface Triangle { kind: "triangle"; side: number; }
type Shape = Square | Rectangle | Circle | Triangle;
function area(s: Shape): number {
let area;
switch (s.kind) {
case "square": area = s.size * s.size; break;
case "rectangle": area = s.width * s.height; break;
case "circle": area = Math.PI * s.radius * s.radius; break;
case "triangle": area = Math.sqrt(3) / 4 * s.side * s.side; break;
}
return area;
}
function areaWrapped(s: Shape): number {
let area;
area = (() => {
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;
case "triangle": return Math.sqrt(3) / 4 * s.side * s.side;
}
})();
return area;
}
// Repro from #13241
enum MyEnum {
A,
B
}
function thisGivesError(e: MyEnum): string {
let s: string;
switch (e) {
case MyEnum.A: s = "it was A"; break;
case MyEnum.B: s = "it was B"; break;
}
return s;
}
function good1(e: MyEnum): string {
let s: string;
switch (e) {
case MyEnum.A: s = "it was A"; break;
case MyEnum.B: s = "it was B"; break;
default: s = "it was something else"; break;
}
return s;
}
function good2(e: MyEnum): string {
switch (e) {
case MyEnum.A: return "it was A";
case MyEnum.B: return "it was B";
}
}
// Repro from #18362
enum Level {
One,
Two,
}
const doSomethingWithLevel = (level: Level) => {
let next: Level;
switch (level) {
case Level.One:
next = Level.Two;
break;
case Level.Two:
next = Level.One;
break;
}
return next;
};
// Repro from #20409
interface Square2 {
kind: "square";
size: number;
}
interface Circle2 {
kind: "circle";
radius: number;
}
type Shape2 = Square2 | Circle2;
function withDefault(s1: Shape2, s2: Shape2): string {
switch (s1.kind) {
case "square":
return "1";
case "circle":
switch (s2.kind) {
case "square":
return "2";
case "circle":
return "3";
default:
return "never";
}
}
}
function withoutDefault(s1: Shape2, s2: Shape2): string {
switch (s1.kind) {
case "square":
return "1";
case "circle":
switch (s2.kind) {
case "square":
return "2";
case "circle":
return "3";
}
}
}
// Repro from #20823
function test4(value: 1 | 2) {
let x: string;
switch (value) {
case 1: x = "one"; break;
case 2: x = "two"; break;
}
return x;
}
// Repro from #34661
enum Animal { DOG, CAT }
declare const zoo: { animal: Animal } | undefined;
function expression(): Animal {
switch (zoo?.animal ?? Animal.DOG) {
case Animal.DOG: return Animal.DOG
case Animal.CAT: return Animal.CAT
}
}
// Repro from #34840
function foo() {
const foo: number | undefined = 0;
while (true) {
const stats = foo;
switch (stats) {
case 1: break;
case 2: break;
}
}
}
// Repro from #35070
type O = {
a: number,
b: number
};
type K = keyof O | 'c';
function ff(o: O, k: K) {
switch(k) {
case 'c':
k = 'a';
}
k === 'c'; // Error
~~~~~~~~~
!!! error TS2367: This comparison appears to be unintentional because the types 'keyof O' and '"c"' have no overlap.
return o[k];
}
// Repro from #35431
type A = { kind: "abc" } | { kind: "def" };
function f35431(a: A) {
switch (a.kind) {
case "abc":
case "def": return;
default:
a!.kind; // Error expected
~~~~
!!! error TS2339: Property 'kind' does not exist on type 'never'.
}
}
|