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

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');
主站蜘蛛池模板: 剑河县| 温宿县| 体育| 西乡县| 宜昌市| 萍乡市| 来宾市| 剑川县| 哈巴河县| 汝阳县| 柳江县| 和田县| 揭东县| 黄龙县| 乐平市| 邯郸县| 屯昌县| 新民市| 沙河市| 兴义市| 来凤县| 凌源市| 嘉荫县| 察隅县| 杭锦后旗| 汶川县| 伽师县| 新营市| 彭水| 利辛县| 梧州市| 永春县| 广东省| 郁南县| 仁化县| 鱼台县| 陆河县| 柳江县| 德兴市| 鸡东县| 新龙县|