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

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');
主站蜘蛛池模板: 本溪市| 任丘市| 新野县| 雅江县| 莱阳市| 西乡县| 丰顺县| 阿巴嘎旗| 韩城市| 永修县| 镇雄县| 抚顺县| 即墨市| 房山区| 邓州市| 涟源市| 元谋县| 西藏| 桑植县| 新乐市| 绩溪县| 开封市| 梅州市| 广丰县| 渑池县| 民县| 德安县| 临潭县| 策勒县| 桃园市| 格尔木市| 桂平市| 慈利县| 凉城县| 渭源县| 来宾市| 乾安县| 宣城市| 昌图县| 红河县| 聊城市|