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;

複製代碼
相關文章
相關標籤/搜索