[Typescript] Make TypeScript Class Usage Safer with Strict Property Initialization
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的更多相关文章
- Learining TypeScript (一) TypeScript 简介
Learining TypeScript (一) TypeScript 简介 一.TypeScript出现的背景 2 二.TypeScript的架构 2 1. 设计目标 2 2 ...
- TypeScript:TypeScript 百科
ylbtech-TypeScript:TypeScript 百科 TypeScript是一种由微软开发的自由和开源的编程语言.它是JavaScript的一个超集,而且本质上向这个语言添加了可选的静态类 ...
- [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 ...
- 6、什么是TypeScript、TypeScript的安装、转换为.js文件
1.什么是TypeScript (本人用自己的理解梳理了一下,不代表官方意见) TypeScript:Type+ECMAScript6 TypeScript是一种预处理编程语言,遵循es6标准规范,在 ...
- 【TypeScript】TypeScript 学习 5——方法
在 JavaScript 中,有两种方式定义方法. 1.命名的方法 function add(x,y){ return x+y; } 2.匿名方法 var myAdd = function(x,y) ...
- 【TypeScript】TypeScript 学习 4——模块
前端数据验证在改善用户体验上有很大作用,在学了之前的知识的时候,我们很可能会写出以下代码: interface StringValidator { isAcceptable(s: string): b ...
- 【TypeScript】TypeScript 学习 3——类
在 EcmaScript 6 中,我们将会拥有原生的类,而不是像现在通过原型链来实现.使用 TypeScript 我们能提前体验这一特性. 首先来看看一个简单的例子: class Greeter { ...
- 【TypeScript】TypeScript 学习 2——接口
在 TypeScript 中,接口是用作约束作用的,在编译成 JavaScript 的时候,所有的接口都会被擦除掉,因为 JavaScript 中并没有接口这一概念. 先看看一个简单的例子: func ...
- 【TypeScript】TypeScript 学习 1——基本类型
TypeScript 是 JavaScript 的超集,TypeScript 经过编译之后都会生成 JavaScript 代码.TypeScript 最大的特点就是类型化,因此才叫做 TypeScri ...
随机推荐
- native,strictfp,transient,volatile什么java?
1. native 簡單講,呼叫其他語言(c,c++之類)做出來的dll做法1.利用java做個介面供其他語言實作(也就是宣告成native)2.用javah產出.h:3.用其他語言實現native方 ...
- 架设Hmailserver+webmail邮件服务器
架设Hmailserver+webmail邮件服务器 在安裝Hmailserver前先安裝Apache.php.mysql,如果你想懶點直接到http://www.phpnow.org下載phpn ...
- 《Java并发编程实战》学习笔记
第2章 线程安全性 正确性: 某个类的行为与其规范完全一致. 2.1线程安全: 当多个线程访问某个类时,不管运行时环境采用何种调度方式或者这些线程将如何交替执行,并且在主调代码中不需要任何额外的同步或 ...
- a标签实现不跳转点击
<a class="tiao" href="./index.php"></a> JS实现无跳转a标签 <script type=& ...
- k-密码
链接:https://www.nowcoder.com/acm/contest/90/K来源:牛客网 题目描述 ZiZi登录各种账号的时候,总是会忘记密码,所以他把密码都记录在一个记事本上.其中第一个 ...
- 创建展开行明细编辑表单的 CRUD 应用
http://www.runoob.com/jeasyui/jeasyui-app-crud3.html jQuery EasyUI 应用 - 创建展开行明细编辑表单的 CRUD 应用 当切换数据网格 ...
- Codeforces #447 Div2 E
#447 Div2 E 题意 给出一个由有向边构成的图,每条边上有蘑菇,假设有 \(n\) 个蘑菇,那么第一次走过这条边可以获得 \(n\) 个蘑菇,第二次 \(n-1\),第三次 \(n-1-2\) ...
- Coderforces 85 D. Sum of Medians(线段树单点修改)
D. Sum of Medians time limit per test 3 seconds memory limit per test 256 megabytes input standard i ...
- Jenkins上配置Robot Framework测试邮件通知模板
邮件效果 测试成功如下所示: jenkins_robot_success 测试失败如下所示: jenkins_robot_failure 通过这个模板,我们能够很直观地看出测试的执行情况,以及相关的统 ...
- Mysql数据库的安装及配置
本文转载自http://www.cnblogs.com/xiaoluo501395377/archive/2013/04/07/3003278.html 如果要在Linux上做j2ee开发,首先得搭建 ...