皇上,还记得我吗?我就是1999年那个Linux伊甸园啊-----24小时滚动更新开源资讯,全年无休!

TypeScript 3.2 RC 发布,微软推出的 JavaScript 超集

TypeScript 3.2 RC 发布了,主要更新内容包括:

  • TypeScript 3.2 对 bind、call 和 apply 进行了更严格的检查

结合 TypeScript 2.0 中的 this 参数类型和 TypeScript 3.0 中的元组类型建模参数列表 ,可调用对象上的方法由名为 CallableFunction 的新全局类型描述,该类型声明了更严格的 bind、call 和 apply 签名版本。类似地,可构造(但不可调用)对象的任何方法都由名为 NewableFunction 的新全局类型描述。

Function.prototype.apply 在此行为下执行示例:

function foo(a: number, b: string): string {
    return a + b;
}

let a = foo.apply(undefined, [10]);              // error: too few argumnts
let b = foo.apply(undefined, [10, 20]);          // error: 2nd argument is a number
let c = foo.apply(undefined, [10, "hello", 30]); // error: too many arguments
let d = foo.apply(undefined, [10, "hello"]);     // okay! returns a string
  • 支持泛型对象 spread
// Returns 'T & U'
function merge<T, U>(x: T, y: U) {
    return { ...x, ...y };
}

// Returns '{ name: string, age: number, greeting: string } & T'
function foo<T>(obj: T) {
    let person = {
        name: "Daniel",
        age: 26
    };

    return { ...person, greeting: "hello", ...obj };
}
  • 支持泛型对象 rest
interface XYZ { x: any; y: any; z: any; }

type DropXYZ<T> = Pick<T, Exclude<keyof T, keyof XYZ>>;

function dropXYZ<T extends XYZ>(obj: T): DropXYZ<T> {
    let { x, y, z, ...rest } = obj;
    return rest;
}
  • 引入了一个名为 bigint 的新原始类型
let foo: bigint = BigInt(100); // the BigInt function
let bar: bigint = 100n;        // a BigInt literal

// *Slaps roof of fibonacci function*
// This bad boy returns ints that are *so* big!
function fibonacci(n: bigint) {
    let result = 1n;
    for (let last = 0n, i = 0n; i < n; i++) {
        const current = result;
        result += last;
        last = current;
    }
    return result;
}

fibonacci(10000n)
declare let foo: number;
declare let bar: bigint;

foo = bar; // error: Type 'bigint' is not assignable to type 'number'.
bar = foo; // error: Type 'number' is not assignable to type 'bigint'.
console.log(3.141592 * 10000n);     // error
console.log(3145 * 10n);            // error
console.log(BigInt(3145) * 10n);    // okay!
function whatKindOfNumberIsIt(x: number | bigint) {
    if (typeof x === "bigint") {
        console.log("'x' is a bigint!");
    }
    else {
        console.log("'x' is a floating-point number");
    }
}
  • JSX 解决方案变更

解决 JSX 调用的逻辑已与解析函数调用的逻辑统一,虽然这简化了编译器代码库并改进了一些用例,但可能需要协调一些差异。它们本身并没有破坏性变更,但是升级者应该关注遇到的问题。

详情查看发布公告

转自 https://www.oschina.net/news/101837/typescript-3-2-rc-released