What's new in TypeScript

www.typescriptlang.org/docs/handbo…html

With union types, you can now specify the desired behavior at both the function declaration site and the call site:es6

(用联合类型,在函数的声明和调用的时候,你能够指按期望的行为)typescript

function choose1<T>(a:T , b: T): T{ return Math.random() > 0.5 ? a : b }
let a = choose1('hello',42) //error
let b = choose2<string | number>('hello',42) //OK

function choose2<T, U>(a: T, b: U): T|U{
    return Math.random() > 0.5 ? a : b
}
var c = choose2('haha','foo')//OK c:string
var d = choose2('haha',32)//OK string|number
复制代码

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:express

(联合类型也容许更好的类型推断在数组或者是其余的有多个不一样种类的集合中)数组

var x = [1,'hello'] //x:Array<number|string>
x[0] = 'world' // OK
x[2] = false // Error, boolean is not string or number
复制代码

let declarations

In JavaScript, var declarations are “hoisted” to the top of their enclosing scope. This can result in confusing bugs:安全

(在js中,var声明会在做用域中把变量提高到顶部。这可能会致使一些使人困惑的bug)bash

console.log(x); // meant to write 'y' here
/* later in the same block */
var x = 'hello';
复制代码

The new ES6 keyword let, now supported in TypeScript, declares a variable with more intuitive “block” semantics.dom

(新的ES6的let关键字,在ts中是被支持的,它声明了一个具备更直观的“块”语义的变量。)函数

A let variable can only be referred to after its declaration, and is scoped to the syntactic block where it is defined: (let变量只能在声明后被引用,它的做用域范围限定在它定义的语法块)ui

if (foo) {
    console.log(x); // Error, cannot refer to x before its declaration
    let x = 'hello';
}
else {
    console.log(x); // Error, x is not declared in this block
}
复制代码

const declarations

The other new ES6 declaration type supported in TypeScript is const.

(另外一个新的声明类型在ts中也是被支持的)

A const variable may not be assigned to, and must be initialized where it is declared.

(const变量可能未被赋值,而且必须在声明它的位置初始化。)

This is useful for declarations where you don’t want to change the value after its initialization:

(这对于您不但愿在初始化后更改值的声明颇有用:)

const halfPi = Math.PI / 2;
halfPi = 2; // Error, can't assign to a `const` 复制代码

Template strings

TypeScript now supports ES6 template strings. These are an easy way to embed arbitrary expressions in strings:

(ts如今支持es6的模版字符串。这是一种很简单的方法你能够随意在字符串中嵌入表达式)

var name = "TypeScript";
var greeting  = `Hello, ${name}! Your name has ${name.length} characters`;
复制代码

When compiling to pre-ES6 targets, the string is decomposed:

(当编译的ES6以前的目标是,字符串被分解为:)

var name = "TypeScript!";
var greeting = "Hello, " + name + "! Your name has " + name.length + " characters";

复制代码

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. (JavaScript中的常见模式是使用typeof或instanceof在运行时检查表达式的类型。 TypeScript如今能够理解这些条件,而且在if块中使用时会相应地更改类型推断。)

Using typeof to test a variable: (用typeof来检测一个变量)

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
复制代码

Using typeof with union types and else:

(用typeof和联合类型还有else检测变量)

var x: string | HTMLElement = /* ... */;
if(typeof x === 'string') {
    // x is string here, as shown above
}
else {
    // x is HTMLElement here
    console.log(x.innerHTML);
}
复制代码

Using instanceof with classes and union types:

(用instanceof、classes还有联合类型检测变量)

class Dog { woof() { } }
class Cat { meow() { } }
var pet: Dog|Cat = /* ... */;
if (pet instanceof Dog) {
    pet.woof(); // OK
}
else {
    pet.woof(); // Error
}

复制代码

Type Aliases

You can now define an alias for a type using the type keyword: (你如今能够给一个类型定义一个别名用类型关键字:type)

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.

(类型别名与其原始类型彻底相同; 它们只是替代名称。)

const enum (completely inlined enums)

Enums are very useful, but some programs don’t actually need the generated code and would benefit from simply inlining all instances of enum members with their numeric equivalents.

(枚举很是有用,可是某些程序实际上并不须要生成的代码,而且只需简单地使用其数字等价物内联枚举成员的全部实例便可获益。)

The new const enum declaration works just like a regular enum for type safety, but erases completely at compile time.

(新的枚举声明就像类型安全的常规枚举同样,但在编译时会彻底擦除。)

const enum Suit { Clubs, Diamonds, Hearts, Spades }
var d = Suit.Diamonds;

复制代码

Compiles to exactly:

最终编译成:

var d = 1;
复制代码

TypeScript will also now compute enum values when possible:

ts 如今也会在可能的状况下计算枚举值:

enum MyFlags {
  None = 0,
  Neat = 1,
  Cool = 2,
  Awesome = 4,
  Best = Neat | Cool | Awesome
}
var b = MyFlags.Best; // emits var b = 7;

复制代码
相关文章
相关标签/搜索