<万博manbetx平台> TypeScript 1.7 - TypeScript 中文文档(v2.4) - 万博manbetx平台中文网
购物车
登陆 / 注册
微信扫码登陆

推荐手册

TypeScript 1.7

TypeScript 1.7
在类里,this值的类型将被推断成this类型。 这意味着随后使用原始类型赋值时可能会发生错误。
血雨探花 血雨探花 更新时间:2019-03-25 13:15:01

TypeScript 1.7


完整的破坏性改动列表请到这里查看:。

this中推断类型发生了变化

在类里,this值的类型将被推断成this类型。 这意味着随后使用原始类型赋值时可能会发生错误。

例子:

class Fighter {
    /** @returns the winner of the fight. */
    fight(opponent: Fighter) {
        let theVeryBest = this;
        if (Math.rand() < 0.5) {
            theVeryBest = opponent; // error
        }
        return theVeryBest
    }
}

推荐:

添加类型注解:

class Fighter {
    /** @returns the winner of the fight. */
    fight(opponent: Fighter) {
        let theVeryBest: Fighter = this;
        if (Math.rand() < 0.5) {
            theVeryBest = opponent; // no error
        }
        return theVeryBest
    }
}

类成员修饰符后面会自动插入分号

关键字abstract,public,protectedprivate是ECMAScript 3里的保留关键字并适用于自动插入分号机制。 之前,在这些关键字出现的行尾,TypeScript是不会插入分号的。 现在,这已经被改正了,在上例中abstract class D不再能够正确地继承C了,而是声明了一个m方法和一个额外的属性abstract

注意,asyncdeclare已经能够正确自动插入分号了。

例子:

abstract class C {
    abstract m(): number;
}
abstract class D extends C {
    abstract
    m(): number;
}

推荐:

在定义类成员时删除关键字后面的换行。通常来讲,要避免依赖于自动插入分号机制。

网站导航
标签地图
学习路径
视频教程
开发软件
旗下子站
技术文章
文档工具
关于我们
企业合作
人才招聘
联系我们
讲师招募
QQ交流群
QQ官方交流群
微信公众号
微信公众号