Typescript classes make inheritance much easier to write and understand. In this lesson we look into how to set up inheritance with Typescript classes, extends and super. class ComicBookCharacter ( constructor{ public alias: string, public health: nu…
In JavaScript, many libraries use string arguments to change behavior. In this lesson we learn how Typescript catches string related errors at compile time by assigning a string literal as a type. type whiteList = "DOG" | "CAT" | "…
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…
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…
Sometimes the compiler needs help figuring out a type. In this lesson we learn how to help out the compiler with Typescript type assertion. We have a SuperHero and a BadGuy. Let's make a function that saves the day if the function's argument is a Sup…
It’s easy to pass the wrong value to a function. Typescript interfaces are great because they catch errors at compile time or in an IDE. In this lesson we’ll learn how to describe a type shape with Typescript interfaces. Using interface to describe a…
this is probably the most tricky thing to use in JavaScript and therefore TypeScript. Fortunately there is a TypeScript compiler flag noImplicit this that can help catch unsafe usages so beginners don't get caught off guard. // tsconfig.json { "compi…
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 correct = (val !== undefined && val !== null) ? val : 0.5; // const in…
1.ts 中的 any 和 unknown 有什么区别? unknown 和 any 的主要区别是 unknown 类型会更加严格:在对 unknown 类型的值执行大多数操作之前,我们必须进行某种形式的检查.而在对 any 类型的值执行操作之前,我们不必进行任何检查. 举例说明: let foo: any = 123; console.log(foo.msg); // 符合TS的语法 let a_value1: unknown = foo; // OK let a_value2: any =…
写在前面 作者并没有任何可以作为背书的履历来证明自己写作这份手册的分量. 其内容大都来自于TypeScript官方资料或者搜索引擎获得,期间掺杂少量作者的私见,并会标明. 大部分内容来自于http://www.infoq.com/minibooks/typescript-c-sharp-programmers 你甚至可以认为这就是对这本英文小册子的翻译,实际上80%如此. 写给那些已经有编程基础,尤其是掌握c语言.c#.java这一类型的静态类型语言的同好. 鸣谢 先谢国家,虽然并不知道要谢些什…