TypeScript 版本相關

TypeScript 1.3dom

  元組類型ide

// Declare a tuple type
var x: [string, number];
// 初始化
x = ["hello", 10];    // ok
// 錯誤的初始化
x = [10, "hello"];    // Error
View Code

TypeScript 1.4函數

   let 聲明ui

在JavaScript裏, var聲明會被"提高"到所在做用域的頂端,這可能會引起一些讓人不解的bugs:

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

TypeScript已經支持新的ES6的關鍵字let, 聲明一個塊級做用域的變量.一個let變量只能在聲明以後的位置被引用,而且做用域爲聲明它的塊裏:

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
}
View Code

  const 聲明this

const halfPi = Math.PI / 2;
halfPi = 2;    // Error, cant' assign to a 'const'
View Code

  類型別名spa

type PrimitiveArray = Array<string|number|boolean>;
type MyNumber = number;
type NgScope = ng.IScope;
type Callback = () => void;
View Code

  const enum(徹底嵌入的枚舉)3d

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

Compiles to exactly;
var d = 1;
View Code

Typescript 1.5code

  導出聲明orm

interface Stream { ... }
function writeToStream(stream: Stream, data: string) { ... }
export { Stream, writeToStream as write };    // writeToStream 導出爲 write

import { read, write, standardOutput as stdout } from "./inout";
var s = read(stdout);
write(stdout, s);

import * as io from "./inout";
var s = io.read(io.standardOutput);
io.write(io.standardOutput, s);
View Code

  從新導出blog

export { read, write, standardOutput as stdout } from "./inout";

export function transform(s: string): string { ... }
export * from "./mod1";
export * from "./mod2";
View Code

  默認導出項

export default class Greeter {
    sayHello() {
        console.log("Greeting!");
    }
}

import Greeter from "./greeter";
var g = new Greeter();
g.sayHello();
View Code

  無導入加載

import "./polyfills"
View Code

TypeScript 1.6

  交叉類型(intersection types)

Typescript 1.6 引入了交叉類型做爲聯合類型(union types)邏輯上的補充,聯合類型 A | B 表示一個類型爲A或B的實體, 而交叉類型 A & B 表示一個類型同時爲 A 或 B的實體

function extend<T, U>(first: T, second: U): T & U {
    let result = <T & U> {};
    for (let id in first) {
        result[id] = first[id];
    }
    for (let id in second) {
        if (!result.hasOwnProperty(id)) {
            result[id] = second[id];
        }
    }

    return result;
}

var x = extend({ a: "hello" }, { b: 42 });
var s = x.a;
var n = x.b;


type LinkedList<T> = T & { next: LinkedList<T> };

interface Person {
    name: string;
}

var people: LinkedList<Person>;
var s = people.name;
var s = people.next.naem;
var s = people.next.next.name;
var s = people.next.next.next.name;
interface A { a: string }
interface B { b: string }
interface C { c: string }

var abc: A & B & C;
abc.a = "hello";
abc.b = "hello";
abc.c = "hello";
View Code

  類表達式

TypeScript 1.6 增長了對ES6類表達式的支持. 在一個類表達式中, 類的名稱是可選的, 若是指明, 做用域僅限於類表達式自己. 這和函數表達式可選的名稱相似. 在類表達式外沒法引用其實例類型. 可是天然也可以從類型結構上匹配.

let Point = class {
    constructor(public x: number, public y: number) { }
    public length() {
        return Math.sqrt(this.x * this.x + this.y * this.y);
    }
};

var p = new Point(3, 4);
console.log(p.length());
View Code

  繼承表達式

// 繼承內建類
class MyArray extends Array<number> { }
class MyError extends Error { }

// 繼承表達式
class ThingA {
    getGreeting() { return "Hello from A"; }
}

class ThingB {
    getGreeting() { return "Hello from B"; }
}

interface Greeter {
    getGreeting(): string; 
}

interface GreeterConstructor() {
    new (): Greeter;
}

function getGreeterBase(): GreeterConstructor {
    return Math.random() >= 0.5 ? ThingA : ThingB;
}

class Test extends getGreeterBase() {
    sayHello() {
        console.log(this.getGreeting());
    }
}
View Code

TypeScript 1.7

TypeScript 1.8

  隱式返回

function f(x) {    // 錯誤: 不是全部分支都返回了值
    if (x) {
        return false;
    }

    // 隱式返回了 "undefined"
}
View Code
相關文章
相關標籤/搜索