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
|
//// [neverType.ts]
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);
//// [neverType.js]
function error(message) {
throw new Error(message);
}
function errorVoid(message) {
throw new Error(message);
}
function fail() {
return error("Something failed");
}
function failOrThrow(shouldFail) {
if (shouldFail) {
return fail();
}
throw new Error();
}
function infiniteLoop1() {
while (true) {
}
}
function infiniteLoop2() {
while (true) {
}
}
function move1(direction) {
switch (direction) {
case "up":
return 1;
case "down":
return -1;
}
return error("Should never get here");
}
function move2(direction) {
return direction === "up" ? 1 :
direction === "down" ? -1 :
error("Should never get here");
}
function check(x) {
return x || error("Undefined value");
}
var C = (function () {
function C() {
}
C.prototype.void1 = function () {
throw new Error();
};
C.prototype.void2 = function () {
while (true) { }
};
C.prototype.never1 = function () {
throw new Error();
};
C.prototype.never2 = function () {
while (true) { }
};
return C;
}());
function f1(x) {
if (typeof x === "boolean") {
x; // never
}
}
function f2(x) {
while (true) {
if (typeof x === "boolean") {
return x; // never
}
}
}
function test(cb) {
var s = cb();
return s;
}
var errorCallback = function () { return error("Error callback"); };
test(function () { return "hello"; });
test(function () { return fail(); });
test(function () { throw new Error(); });
test(errorCallback);
//// [neverType.d.ts]
declare function error(message: string): never;
declare function errorVoid(message: string): void;
declare function fail(): never;
declare function failOrThrow(shouldFail: boolean): never;
declare function infiniteLoop1(): void;
declare function infiniteLoop2(): never;
declare function move1(direction: "up" | "down"): 1 | -1;
declare function move2(direction: "up" | "down"): 1 | -1;
declare function check<T>(x: T | undefined): T;
declare class C {
void1(): void;
void2(): void;
never1(): never;
never2(): never;
}
declare function f1(x: string | number): void;
declare function f2(x: string | number): never;
declare function test(cb: () => string): string;
declare let errorCallback: () => never;
|