By setting the strictPropertyInitialization flag in the .tsconfig file, TypeScript will start throwing errors unless we initialize all properties of classes on construction. We’ll explore how you can fix the errors by assigning to them directly or in the constructor body. And if you can’t initialize directly but you’re sure it will be assigned to at runtime by a dependency injection library, you can use the definite assignment assertion operator to ask TypeScript to ignore that property.

For example, code belkow, 'title' is undefined. WIll cause the problem when we call '.filter'.

class Library {
titles: string[]; constructor() {}
}
const library = new Library(); // sometime later & elsewhere in our codebase.. const shortTitles = library.titles.filter(
title => title.length <
);

First we want our IDE to help us to detect the problem even before compiling...

tsconfig.json:

{
"compilerOptions": {
"strictPropertyInitialization": true,
"strictNullChecks": true
}
}

After setting up 'strictPropertyInitialization' & 'strictNullChecks', IDE will tell us that 'title' is undefined.

In the code, we can also utitlize:

'use strict'

or add some if checking.

Using definite assignment operator from Typescript:

class Library {
titles!: string[] constructor() {}
}

'!' tell typescript, this object is not undefine or null, we will assign the value later, so in this case, IDE won't complain:

class Library {
titles!: string[] constructor() { this.titles = []
}
}

[Typescript] Make TypeScript Class Usage Safer with Strict Property Initialization的更多相关文章

  1. Learining TypeScript (一) TypeScript 简介

    Learining TypeScript (一) TypeScript 简介 一.TypeScript出现的背景    2 二.TypeScript的架构    2 1.    设计目标    2 2 ...

  2. TypeScript:TypeScript 百科

    ylbtech-TypeScript:TypeScript 百科 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类 ...

  3. [TypeScript] Installing TypeScript and Running the TypeScript Compiler (tsc)

    This lesson shows you how to install TypeScript and run the TypeScript compiler against a .ts file f ...

  4. 6、什么是TypeScript、TypeScript的安装、转换为.js文件

    1.什么是TypeScript (本人用自己的理解梳理了一下,不代表官方意见) TypeScript:Type+ECMAScript6 TypeScript是一种预处理编程语言,遵循es6标准规范,在 ...

  5. 【TypeScript】TypeScript 学习 5——方法

    在 JavaScript 中,有两种方式定义方法. 1.命名的方法 function add(x,y){ return x+y; } 2.匿名方法 var myAdd = function(x,y) ...

  6. 【TypeScript】TypeScript 学习 4——模块

    前端数据验证在改善用户体验上有很大作用,在学了之前的知识的时候,我们很可能会写出以下代码: interface StringValidator { isAcceptable(s: string): b ...

  7. 【TypeScript】TypeScript 学习 3——类

    在 EcmaScript 6 中,我们将会拥有原生的类,而不是像现在通过原型链来实现.使用 TypeScript 我们能提前体验这一特性. 首先来看看一个简单的例子: class Greeter { ...

  8. 【TypeScript】TypeScript 学习 2——接口

    在 TypeScript 中,接口是用作约束作用的,在编译成 JavaScript 的时候,所有的接口都会被擦除掉,因为 JavaScript 中并没有接口这一概念. 先看看一个简单的例子: func ...

  9. 【TypeScript】TypeScript 学习 1——基本类型

    TypeScript 是 JavaScript 的超集,TypeScript 经过编译之后都会生成 JavaScript 代码.TypeScript 最大的特点就是类型化,因此才叫做 TypeScri ...

随机推荐

  1. HTML-坦克大战-完成子弹连发功能(三)

    如题,完成子弹连发功能,上一篇博客遗留的问题,不能够连发,且一直按J键则第一颗子弹会消失:那是因为定义的子弹变量只是一个变量,现在定义成一个数组:在之前的代码上修改如下: <!DOCTYPE h ...

  2. [译]lambda表达式对 SAM (单个抽象方法类)type的处理方式

    在阅读Venkat Subramaniam的著作<Functional Programming in Java> 之后,方法模式和lambda完美结合让我印象深刻. 这种模式经常用作数据源 ...

  3. linux中直接进行系统调用和通过C库调用的示例

    深入了解LINUX,这方面内容不可少,这段时间再补补.. #include <syscall.h> #include <unistd.h> #include <stdio ...

  4. yii2.0在model里自定义数据表

    无需多言,直接撸代码 class Zhuanjia extends \yii\db\ActiveRecord { public static function tableName() { return ...

  5. (2)三剑客之grep

    1)grep和egrep定义grep:在文件中全局查找指定的正则表达式,并打印所有包含该表达式的行egrep:扩展的egrep,支持更多的正则表达式元字符2)命令格式语法:grep [选项] patt ...

  6. centos6.5 卸载adobeflash

    # rpm -e flash-plugin # rpm -qa | grep ^flash-plugin

  7. HDU 6343.Problem L. Graph Theory Homework-数学 (2018 Multi-University Training Contest 4 1012)

    6343.Problem L. Graph Theory Homework 官方题解: 一篇写的很好的博客: HDU 6343 - Problem L. Graph Theory Homework - ...

  8. SDL安装小结

    SDL是一个基于C的简易实现,安装过程中也多亏了,各位大神的助攻,这里简单mark一下遇到的问题,以备查找: 关于VS的版本:目前文档里确定支持的VS为2008到2013,我的VS是2013,2015 ...

  9. HDOJ 5009 Paint Pearls

    Dicripntion Lee has a string of n pearls. In the beginning, all the pearls have no color. He plans t ...

  10. POJ 1127 Jack Straws (计算几何)

    [题目链接] http://poj.org/problem?id=1127 [题目大意] 在二维平面中,给出一些木棍的左右端点,当木棍相交或者间接相交时 我们判断其连通,给出一些询问,问某两个木棍是否 ...