- Hands-On Full Stack Web Development with Angular 6 and Laravel 5
- Fernando Monteiro
- 168字
- 2021-07-23 19:18:50
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');
推薦閱讀
- CorelDRAW X6 中文版圖形設(shè)計(jì)實(shí)戰(zhàn)從入門到精通
- OpenLayers Cookbook
- 萬(wàn)物互聯(lián):蜂窩物聯(lián)網(wǎng)組網(wǎng)技術(shù)詳解
- HTML5 Game development with ImpactJS
- 企業(yè)網(wǎng)絡(luò)安全管理
- 城域網(wǎng)與廣域網(wǎng)(第2版)
- 物聯(lián)網(wǎng)之霧:基于霧計(jì)算的智能硬件快速反應(yīng)與安全控制
- 物聯(lián)網(wǎng)工程導(dǎo)論(第3版)
- 轉(zhuǎn)化:提升網(wǎng)站流量和轉(zhuǎn)化率的技巧
- TCP/IP基礎(chǔ)(第2版)
- 圖神經(jīng)網(wǎng)絡(luò)前沿
- Building RESTful Web Services with .NET Core
- 物聯(lián)網(wǎng),So Easy!
- React Design Patterns and Best Practices(Second Edition)
- 智能物聯(lián)安防視頻技術(shù)基礎(chǔ)與應(yīng)用