首页 > 技术文章 > TypeScript 版本相关

revoid 2019-03-28 13:51 原文

TypeScript 1.3

  元组类型

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

TypeScript 1.4

   let 声明

在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 声明

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

  类型别名

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

  const enum(完全嵌入的枚举)

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

Compiles to exactly;
var d = 1;
View Code

Typescript 1.5

  导出声明

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

  重新导出

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

  

推荐阅读