94 lines
1.7 KiB
TypeScript
94 lines
1.7 KiB
TypeScript
// @strictNullChecks: true
|
|
// @declaration: true
|
|
|
|
|
|
function error(message: string): never {
|
|
throw new Error(message);
|
|
}
|
|
|
|
function errorVoid(message: string) {
|
|
throw new Error(message);
|
|
}
|
|
|
|
function fail() {
|
|
return error("Something failed");
|
|
}
|
|
|
|
function failOrThrow(shouldFail: boolean) {
|
|
if (shouldFail) {
|
|
return fail();
|
|
}
|
|
throw new Error();
|
|
}
|
|
|
|
function infiniteLoop1() {
|
|
while (true) {
|
|
}
|
|
}
|
|
|
|
function infiniteLoop2(): never {
|
|
while (true) {
|
|
}
|
|
}
|
|
|
|
function move1(direction: "up" | "down") {
|
|
switch (direction) {
|
|
case "up":
|
|
return 1;
|
|
case "down":
|
|
return -1;
|
|
}
|
|
return error("Should never get here");
|
|
}
|
|
|
|
function move2(direction: "up" | "down") {
|
|
return direction === "up" ? 1 :
|
|
direction === "down" ? -1 :
|
|
error("Should never get here");
|
|
}
|
|
|
|
function check<T>(x: T | undefined) {
|
|
return x || error("Undefined value");
|
|
}
|
|
|
|
class C {
|
|
void1() {
|
|
throw new Error();
|
|
}
|
|
void2() {
|
|
while (true) {}
|
|
}
|
|
never1(): never {
|
|
throw new Error();
|
|
}
|
|
never2(): never {
|
|
while (true) {}
|
|
}
|
|
}
|
|
|
|
function f1(x: string | number) {
|
|
if (typeof x === "boolean") {
|
|
x; // never
|
|
}
|
|
}
|
|
|
|
function f2(x: string | number) {
|
|
while (true) {
|
|
if (typeof x === "boolean") {
|
|
return x; // never
|
|
}
|
|
}
|
|
}
|
|
|
|
function test(cb: () => string) {
|
|
let s = cb();
|
|
return s;
|
|
}
|
|
|
|
let errorCallback = () => error("Error callback");
|
|
|
|
test(() => "hello");
|
|
test(() => fail());
|
|
test(() => { throw new Error(); })
|
|
test(errorCallback);
|