The postshows you how to use the null coalescing operator (??) instead of logical or (||) to set default values in TypeScript 3.7 to prevent expected bugs in your code.

const val = ;
const correct = (val !== undefined && val !== null) ? val : 0.5;
// const incorrect = val || 0.5; // || check all the falsy value
const incorrect = val ?? 0.5; // ?? check only null and undefined
console.log({ correct, incorrect });

[TypeScript ] Using the Null Coalescing operator with TypeScript 3.7的更多相关文章

  1. Null Coalescing Operator

    w Parse error: syntax error, unexpected '?'

  2. js Nullish Coalescing Operator

    js Nullish Coalescing Operator 空值合并 const n = null ?? 'default string'; console.log(n); // "def ...

  3. nil coalescing operator

    nil coalescing operator ?? 就是 optional和 三元运算符?:的简写形式. 比如一个optional String类型的变量 var a:String? // prin ...

  4. [TypeScript] Use the JavaScript “in” operator for automatic type inference in TypeScript

    Sometimes we might want to make a function more generic by having it accept a union of different typ ...

  5. [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 ...

  6. [TypeScript] Distinguishing between types of Strings in TypeScript

    In JavaScript, many libraries use string arguments to change behavior. In this lesson we learn how T ...

  7. [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 ...

  8. [TypeScript] Using Assertion to Convert Types in TypeScript

    Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the c ...

  9. [TypeScript] Sharing Class Behavior with Inheritance in TypeScript

    Typescript classes make inheritance much easier to write and understand. In this lesson we look into ...

随机推荐

  1. AS3放大镜工具类

    package { import flash.display.Bitmap; import flash.display.BitmapData; import flash.display.Display ...

  2. Jmeter 接口测试之MD5加密函数(函数助手篇)

    转自:https://blog.csdn.net/hhyangel372/article/details/81571058 本文给大家介绍一下如何使用Jmeter自带的函数对常量和变量进行简单的加密处 ...

  3. Deep one-class classification

    Deep one-class classification 2019-03-17 23:09:59 zpainter 阅读数 1027  收藏 文章标签: 单分类问题异常检测 更多 分类专栏: 论文 ...

  4. CacheManager.Core

    GitHub地址:https://github.com/MichaCo/CacheManager CacheManager的优点: 让开发人员的生活更容易处理和配资缓存,即使是非常复杂的缓存方案. C ...

  5. js注意点

    1.在JS中:var a=''; 则 a==0或a==false 结果都为true;  如果是“====” 则为false

  6. 系統启动直接进BIOS

  7. MVC-10HTML助手

    HTML帮助器用于修改HTML输出. HTML帮助器 通过MVC,HTML帮助器类似于传统的ASP.NET Web Form控件. 类似ASP.NET中的web form控件,HTML帮助器用于修改H ...

  8. C#只读属性

    using System; using System.Collections.Generic; using System.Text; namespace 面向对象 { class Person { / ...

  9. 关于vue-svg-icon的使用方式

    前言 工作中用到svg格式的图标,既然是svg,当然不想用古老的img方式引用,希望能凭借定义svg的fill属性,随意定义图标的颜色:同时不想将整段svg代码写入组建内,于是找到了使用vue-svg ...

  10. mongodump

    mongodump工具是MongoDB提供的用来导出数据的工具,具体的用法参考官方文档:https://docs.mongodb.com/manual/reference/program/mongod ...