[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 4.9重点特性探索
- TypeScript 实用工具类型之 Omit 类型
- 内存数据库 mysql-mysql in memory_In-Memory:内存数据库
- TypeScript 新语法 satisfies:用声明还是用推导?这是个问题
- 深入学习下 TypeScript 中的泛型
- Typescript 全栈最值得学习的技术栈 TRPC
- TypeScript 5.0 现已发布:全新的装饰器,速度、内存和包大小优化
- TypeScript遭库开发者嫌弃:类型简直是万恶之源
- 调查:React 仍然是使用最广泛的前端框架,TypeScript 是优先选项
- 开心档之TypeScript 联合类型
- MySQL OR和IN:比较和选择(mysqlor和in)
- 关键字使用Oracle中的Replace函数替代IN关键字(oracle替代in)
- 语句使用MySQL中的IN和OR语句(mysql中in和or)
- 微软为大型 JavaScript 应用而开发的 TypeScript 发布了 2.0 版本
- 语句SQL Server中使用IN语句处理多值查询(sqlserver中in)
- 借助MSSQL语句实现IN的强大功能(mssql语句 in)
- MSSQL条件查询IN机制优化技巧(mssql条件查询in)
- MySQL使用IN查询时数量超限问题的解决方法(mysql中in超过数量)
- MySQL中IN操作的高效优化(mysql 中in的优化)
- MySQL中的IN关键字有什么含义(mysql中in什么意思)
- 解决MySQL中IN不走索引问题的方法(mysql中in不走索引)
- MySQL如何使用IN查询语句(mysql不能用in吗)
- 语法 在Oracle中使用IN子句实现查询(oracle中支持in)
- Oracle中IN与OR运算实现多条件查询的最佳选择(oracle中in与or)