[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 ...
随机推荐
- CSS3制作旋转的小风车
制作旋转小风车 一 我先搭建一个大盒子400x400px大盒子里面嵌套四个小盒子200x200px,放在一起肯定是四个排在一行,我想要的效果是上下各两个, css样式 *{ margin:0; pad ...
- k8s的应用打包工具Helm
每个成功的软件平台都有一个优秀的打包系统,比如 Debian.Ubuntu 的 apt,Redhat.Centos 的 yum.而 Helm 则是 Kubernetes 上的包管理器. 本章我们将讨论 ...
- rabbitmq安装-Erlang
安装Erlang Install Erlang from the Erlang Solutions repository or Follow the instructions under " ...
- CURL命令的使用
原文地址:http://blog.sina.com.cn/s/blog_4b9eab320100slyw.html 可以看作命令行浏览器 1.开启gzip请求curl -I http://www.si ...
- rhel 6.5 yum源的配置
https://blog.csdn.net/error_0_0_/article/details/54962199
- (八)MySQL索引操作
(1)准备环境 mysql> create table t1(id int,name varchar(50)); mysql> \d $$ mysql> create procedu ...
- F - 等式(1/x + 1/y = 1/n)
链接:https://www.nowcoder.com/acm/contest/90/F来源:牛客网 题目描述 给定n,求1/x + 1/y = 1/n (x<=y)的解数.(x.y.n均为正整 ...
- 【python】ipython与python的区别
[python]ipython与python的区别 (2014-06-05 12:27:40) 转载▼ 分类: Python http://mba.shengwushibie.com/itbook ...
- (14)python 文件和流
打开文件 f=open('C:\Temp.txt') 读取数据 f.read(); 关闭文件 f.close();#关闭后将无法再读取 打开文件的方式 不写模式,默认是只读模式 1.r 打开只读文件, ...
- RobotFramework自动化测试框架系统关键字之断言
一.基础 RobotFramework带有丰富的系统关键,使用时无需导入,直接使用,为写自动化用例带来了极大的方便:不能停留在知道或者是会得程度,只有熟练使用各关键字,才能提升自动化用例的写作效率.下 ...