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

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');
主站蜘蛛池模板: 云阳县| 彩票| 唐河县| 永嘉县| 益阳市| 原阳县| 龙井市| 巴林左旗| 南江县| 铜陵市| 竹北市| 高邮市| 云龙县| 宁阳县| 盐山县| 贵州省| 萝北县| 湘潭县| 彰化市| 商都县| 河南省| 蓝山县| 皮山县| 封丘县| 会泽县| 佛坪县| 疏勒县| 自贡市| 慈利县| 平阳县| 乐东| 温宿县| 巧家县| 宜丰县| 襄汾县| 甘谷县| 理塘县| 贵定县| 探索| 德格县| 永定县|