TypeScript has 'interface' and 'type', so when to use which? interface hasName { firstName: string; lastName: string; } interface hasAddress { address: string } type Player = (hasName & hasAddress) | null; let player: Player = {firstName: 'Joe', last…
[TypeScript] TypeScript对象转JSON字符串范例 Playground http://tinyurl.com/njbrnrv Samples class DataTable { public columns: Array<string> = new Array<string>(); public rows: Array<DataRow> = new Array<DataRow>(); } class DataRow { public c…
TypeScript’s discriminated union types (aka tagged union types) allow you to model a finite set of alternative object shapes in the type system. The compiler helps you introduce fewer bugs by only exposing properties that are known to be safe to acce…
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…
The keyof operator produces a union type of all known, public property names of a given type. You can use it together with lookup types (aka indexed access types) to statically model dynamic property access in the type system. Take away: 1. extends 2…
前言 Union Type和Intersection Type都是将多个类型结合起来的一个等价的"类型",它们并非是实际存在的类型. Union Type Union type(联合类型)使用比特或运算符|进行构造: A | B | C 注意:用|符号来构造Union Type类型只是Java语言的规定,|在这里不代表比特或的含义. 上例中,A | B | C是一个Union type,Union type的含义就是"或",只要满足其中一个即可. 实例:捕获多个异常…
Handling state with Typescript enums, instead of booleans, is preferred because:- Enums are more readable- Enums can have as many states as you need while booleans only have 2- You only need to keep track of state with 1 variable when using enums Typ…