24 Star 88 Fork 34

wolfx/TypeScript-Documents-Chinese

Create your Gitee Account
Explore and code with more than 13.5 million developers,Free private repositories !:)
Sign up
文件
.github
docs
zh
assets/images/tutorials/aspnet
breaking-changes
README.md
typescript-1.1.md
typescript-1.4.md
typescript-1.5.md
typescript-1.6.md
typescript-1.7.md
typescript-1.8.md
typescript-2.0.md
typescript-2.1.md
typescript-2.2.md
typescript-2.3.md
typescript-2.4.md
typescript-2.6.md
typescript-2.7.md
typescript-2.8.md
typescript-2.9.md
typescript-3.0.md
typescript-3.1.md
typescript-3.2.md
typescript-3.4.md
typescript-3.5.md
typescript-3.6.md
declaration-files
handbook-v2
handbook
javascript
misc
project-config
reference
release-notes
tutorials
wiki
.gitattributes
.gitignore
.travis.yml
CONTRIBUTING.md
LICENSE
PREFACE.md
README.md
SUMMARY.md
book.toml
gulpfile.js
lint.js
package-lock.json
package.json
Clone or Download
typescript-1.7.md 1.73 KB
Copy Edit Raw Blame History
patrick.zhongsp authored 4 years ago . refactor: structure

TypeScript 1.7

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

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;
}

推荐:

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

Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
TypeScript
1
https://gitee.com/wolfx/TypeScript-Handbook-zh.git
git@gitee.com:wolfx/TypeScript-Handbook-zh.git
wolfx
TypeScript-Handbook-zh
TypeScript-Documents-Chinese
dev

Search