zl程序教程

您现在的位置是:首页 >  前端

当前栏目

[TypeScript] What's New in TypeScript 1.4

typescript in &# 39 New 1.4 What
2023-09-14 08:59:20 时间

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.