官术网_书友最值得收藏!

Using the never type

The never type was introduced in TypeScript 2.0; it implies a value that never occurs. At first glance, it may seem strange, but it can be used in some situations.

Let's look at what the official documentation says about it:

The  never type represents the types of values that never occur. Specifically, never is the return type for functions that never return, and never is the type for variables under type guards that are never true.

Suppose that a messaging function that is called within another function specifies the callback.

It would look something like the following code:

const myMessage = (text: string): never => {
throw new Error(text);
}
const myError = () => Error('Some text here');

Another example would be checking a value that is a string and number at the same time, such as the following:

function neverHappen(someVariable: any) {
if (typeof someVariable === "string" && typeof someVariable ===
"number") {
console.log(someVariable);
}
}
neverHappen('text');
主站蜘蛛池模板: 孙吴县| 泸西县| 古蔺县| 婺源县| 民乐县| 张北县| 登封市| 黎平县| 吉隆县| 大足县| 刚察县| 沁源县| 黄浦区| 吉林市| 白城市| 常熟市| 正镶白旗| 双牌县| 平武县| 广丰县| 鹤岗市| 武安市| 理塘县| 乳山市| 宁城县| 中江县| 塔河县| 眉山市| 邹城市| 阜城县| 紫阳县| 宿松县| 固安县| 嘉禾县| 安多县| 建昌县| 新乡县| 天峨县| 北川| 阳山县| 镇安县|