[TypeScript] Use the never type to avoid code with dead ends using TypeScript

Example 1: A never stop while loop return a never type.

function run(): never {
   while(true){
      let foo = "bar";
   }
} 

Example 2: Never run If block

const foo = 123;

if(foo !== 123) {
 let bar: never = foo;
}

You can use this to do exhaustive checks in union types.

For example, let‘s say you have a variable returned from the server that can be a string or a number. You can easily add code that handles different cases using the JavaScript typeof operator. You can add an additional else, and assign the variable to a never to ensure that all types were eliminated.

declare var foo:
    | string
    | number;

if(typeof foo === "string") {
  /* todo */
} else if (typeof foo === "number"){
  /* todo */
} else {
  const check: never = foo;
}

Later, if you need to add another type to the union, for example, a Boolean, you will now get nice errors at all the places where the new type was not handled, because only a never is assignable to a never. Now, if you go ahead and add another typeof to handle this new case, the error goes away.

时间: 2024-07-30 20:35:42

[TypeScript] Use the never type to avoid code with dead ends using TypeScript的相关文章

[TypeScript] Union Types and Type Aliases in TypeScript

Sometimes we want our function arguments to be able to accept more than 1 type; e.g. a string or an array. This lesson will show us how to assign more than 1 type to a variable with Typescript union types and type aliases. type types = string | boole

WDK-build 添加Listing file type:assembly,Machine Code,and Source

依照VS6样式,Listing file type:assembly,Machine Code,and Source sources文件中: TARGETNAME=firstTARGETTYPE=DRIVERSOURCES=first.cTARGETPATH=objUSER_C_FLAGS = /FAms MSC_OPTIMIZATION = /Od /OiTARGETLIBS = $(SDK_LIB_PATH)\ws2_32.lib # the only new line...

[TypeScript] Define a function type

type DigitValidator = (char) => boolean; const numericValidator = (char) => /[0-9]{1}/.test(char); export const digitValidators: {[key: string]: DigitValidator} = { '9': numericValidator }; We can use 'type' keyword to define a function type. 'digit

[TypeScript] Generic Functions, class, Type Inference and Generics

Generic Fucntion: For example we have a set of data and an function: interface HasName { name: string; } const heros: HasName[] = [ {name: 'Jno'}, {name: 'Miw'}, {name: 'Ggr'}, {name: 'Gew'}, {name: 'Wfe'} ]; function cloneArray(ary: any[]): any[] {

Visual Studio Code开发TypeScript

[Tool] 使用Visual Studio Code开发TypeScript [Tool] 使用Visual Studio Code开发TypeScript 注意 依照本篇操作步骤实作,就可以在「Windows」.「OS X」操作系统上,使用Visual Studio Code开发TypeScript. 前言 为了解决JavaScript:缺少面向对象语法.缺少编译期间错误检查...等等问题.微软提供了一个开源的TypeScript语言,让开发人员能够使用面向对象撰写TypeScript程序代

使用TypeScript拓展你自己的VS Code!

0x00 前言 在前几天的美国纽约,微软举行了Connect(); //2015大会.通过这次大会,我们可以很高兴的看到微软的确变得更加开放也更加务实了.当然,会上放出了不少新产品和新功能,其中就包括了VS Code的beta版本.而且微软更进一步,已经在github将VS Code的代码开源了.除了这些让人兴奋的消息,我们还应该注意到VS Code提供了对拓展的支持.所以本文就来聊一聊使用TypeScript开发VS Code拓展的话题吧. 本文所使用的拓展的应用商店页面: https://m

[Tool] 使用Visual Studio Code开发TypeScript

[Tool] 使用Visual Studio Code开发TypeScript 注意 依照本篇操作步骤实作,就可以在「Windows」.「OS X」操作系统上,使用Visual Studio Code开发TypeScript. 前言 为了解决JavaScript:缺少面向对象语法.缺少编译期间错误检查...等等问题.微软提供了一个开源的TypeScript语言,让开发人员能够使用面向对象撰写TypeScript程序代码,接着再透过TypeScript编译程序将程序代码编译成为JavaScript

[TypeScript] Define Custom Type Guard Functions in TypeScript

One aspect of control flow based type analysis is that the TypeScript compiler narrows the type of a variable within a type guard. This lesson explores how you can define functions and type predicates to create your own type guards similar to the Arr

0前端 框架 库_千万别去碰js呀 混合APP_webAPP_美工 选有类型的语言,比如TypeScript

component 组件 成分; 零件; [数]要素; 组分; Angular2怎么使用第三方的component库(如 jquery,easyUI ,Bootstrap 等) PWA  增强web app helloWorld跑起来了,之前失败是因为Chrome服务器插件要FQ才能下载 https://developers.google.cn/web/fundamentals/getting-started/codelabs/your-first-pwapp/ 安装谷歌插件 web-serve