[TypeScript] What's New in TypeScript 1.4
In this unit, we'll look at some of the new features in the latest version of TypeScript 1.4 and talk about why they're important. We'll be covering some of these new features later on in the course so stay tuned! Be sure to watch the video below where Dan talks through many of the features and shows you how they work.
UNION TYPES
Overview
Union types are a powerful way to express a value that can be one of several types. For example, you might have an API for executing a program that takes a command-line as either a string or a string[]. You can now write:
interface RunOptions {
program: string;
commandline: string[]|string;
}
Assignment to union types works very intuitively. Anything you could assign to one of the union type's members is assignable to the union:
var opts: RunOptions = /* ... */;
opts.commandline = '-hello world'; // OK
opts.commandline = ['-hello', 'world']; // OK
opts.commandline = [42]; // Error, number is not string or string[]
When reading from a union type, you can see any properties that are shared by them:
if(opts.commandline.length === 0) { // OK, string and string[] both have 'length' property
console.log("it's empty");
}
Using type guards, you can easily work with a variable of a union type:
function formatCommandline(c: string[]|string) {
if(typeof c === 'string') {
return c.trim();
} else {
return c.join(' ');
}
}
TYPE GUARDS
A common pattern in JavaScript is to use typeof or instanceof to examine the type of an expression at runtime. TypeScript now understands these conditions and will change type inference accordingly when used in an if block.
Using typeof to test a variable:
var x: any = /* ... */;
if(typeof x === 'string') {
console.log(x.subtr(1)); // Error, 'subtr' does not exist on 'string'
}
// x is still any here
x.unknown(); // OK
Note: The above example does not give the error mentioned in the 1.4 release. This will likely be improved in future releases.
Using instanceof with classes and union types:
class Dog { woof() { } }
class Cat { meow() { } }
var pet: Dog|Cat = /* ... */;
if(pet instanceof Dog) {
pet.woof(); // OK
} else {
pet.woof(); // Error
}
STRICTER GENERICS
With union types able to represent a wide range of type scenarios, we've decided to improve the strictness of certain generic calls. Previously, code like this would (surprisingly) compile without error:
function equal<T>(lhs: T, rhs: T): boolean {
return lhs === rhs;
}
// Previously: No error
// New behavior: Error, no best common type between 'string' and 'number'
var e = equal(42, 'hello');
BETTER TYPE INFERENCE
Union types also allow for better type inference in arrays and other places where you might have multiple kinds of values in a collection:
var x = [1, 'world']; // x: Array<string|number>
x[0] = 'hello'; // OK
x[0] = false; // Error, boolean is not string or number
TYPE ALIASES
You can now define an alias for a type using the type keyword:
type PrimitiveArray = Array<string|number|boolean>;
type MyNumber = number;
type NgScope = ng.IScope;
type Callback = () => void;
Type aliases are exactly the same as their original types; they are simply alternative names.
相关文章
- 用 TypeScript 实现斐波那契数列
- typescript 中使用 setTimeout 返回值类型 eslint 检查错误 ‘NodeJS‘ is not defined.eslint(no-undef)
- 快速掌握 TypeScript 新语法:infer extends
- typescript快速入门
- TypeScript(3)基础类型[通俗易懂]
- TypeScript中泛型及泛型函数、泛型类、泛型接口,泛型约束,一文读懂
- typeScript学习总结(二)
- Java和TypeScript开发者之间的区别必须知道
- TypeScript 的装饰器有哪些?
- typescript [接口]
- 调查:React 仍然是使用最广泛的前端框架,TypeScript 是优先选项
- 开心档之TypeScript 联合类型
- MySQL中实现嵌套查询的IN关键字用法(mysql嵌套查询in)
- Oracle中IN操作符的用法(oracle中in的用法)
- MySQL的IN操作符对于查询中给定的值列表长度是有限制的(mysql中in长度限制)
- 深入分析MySQL中IN语句的性能问题(mysql中in性能分析)
- MySQL中使用IN操作符查询字符串(mysql中in字符串)
- 解决MySQL中IN不走索引问题的方法(mysql中in不走索引)
- MySQL中IN操作不去重,如何解决重复数据问题(mysql中in 不去重)
- 中利用IN语句检索Oracle数据库中的数据(in在oracle数据库)
- Oracle中析研IN函数的功能(oracle中的in函数)
- 条件在Oracle中使用IN和OR查询条件的巧妙之处(oracle中in跟or)
- 利用Oracle中的If In语句减少数据处理时间(oracle中if in)
- Oracle OR与IN的比较研究(oracle or与in)