- 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');
推薦閱讀
- EJB 3.1從入門(mén)到精通
- GPS/GNSS原理與應(yīng)用(第3版)
- OpenLayers Cookbook
- Learning QGIS 2.0
- 新一代物聯(lián)網(wǎng)架構(gòu)技術(shù):分層算力網(wǎng)絡(luò)
- HCNA網(wǎng)絡(luò)技術(shù)
- Getting Started with Grunt:The JavaScript Task Runner
- 2小時(shí)讀懂物聯(lián)網(wǎng)
- TD-LTE無(wú)線網(wǎng)絡(luò)規(guī)劃與設(shè)計(jì)
- AIoT應(yīng)用開(kāi)發(fā)與實(shí)踐
- 云工廠:開(kāi)啟中國(guó)制造云時(shí)代
- 一本書(shū)讀懂TCP/IP
- 圖神經(jīng)網(wǎng)絡(luò)前沿
- 華為HCIA-Datacom認(rèn)證指南
- 商業(yè)的本質(zhì)和互聯(lián)網(wǎng)