Skip to content

Translate strict type checking options into zh #1295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 26, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
---
display: "严格检查"
---

我们建议启用[编译选项 `strict`](#strict),从而在构建时获得所有可能的改进。

TypeScript 可以非常广泛的支持 JavaScript,并且默认具有相当的灵活性来处理它的编程风格。
一般来说,一个代码库的安全性和潜在的可扩展性可能会与其中的一些技术冲突。

由于 JavaScript 的多样性,升级到新版本的 TypeScript 可能会发现两种类型的错误:

- TypeScript 发现了在你代码库中已经存在的错误,因为 TypeScript 完善了对 JavaScript 的理解。
- 一系列新的错误,它们解决了一个新领域的问题。

TypeScript 通常会为后一种错误添加一个编译选项,并且默认不启用它们。
9 changes: 9 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/alwaysStrict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
display: "始终严格模式"
oneline: "保证始终生成 “use strict”"
---

保证你的文件在 ECMAScript 严格模式下被解析,并且在每个编译后的文件中生成 “use strict”

[ECMAScript 严格](https://developer.mozilla.org/docs/Web/JavaScript/Reference/Strict_mode)模式在 ES5 中被引入,它为 JavaScript 引擎以及运行时提供了行为调整以提高性能。并且会抛出一组之前会被默默忽略的错误。

26 changes: 26 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/noImplicitAny.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
display: "禁止隐式 Any"
oneline: "将具有隐式 ‘Any’ 类型的表达式或声明视为错误。"
---

在一些没有类型标记的情况下,当 TypeScript 不能推断出变量的类型时,它将回退为 ‘any’ 类型。

这可能会导致一些错误被遗漏,例如:

```ts twoslash
// @noImplicitAny: false
function fn(s) {
// 不会提示错误?
console.log(s.subtr(3));
}
fn(42);
```

开启 ‘noImplicitAny’ 后,当 TypeScript 推断出 ‘any’ 时就会提示一个错误:

```ts twoslash
// @errors: 7006
function fn(s) {
console.log(s.subtr(3));
}
```
28 changes: 28 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/noImplicitThis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
display: "禁止隐式 This"
oneline: "将具有 ‘any’ 类型的 ‘this’ 视为错误。"
---

当 ‘this’ 表达式具有隐式的 ‘any’ 类型时抛出错误。

例如,这个类将会返回一个函数,这个函数将会尝试访问 `this.width` 和 `this.height` —— 但在 `getAreaFunction` 函数内返回的函数的上下文中,`this` 并不是 `Rectangle` 的实例。


```ts twoslash
// @errors: 2683
class Rectangle {
width: number;
height: number;

constructor(width: number, height: number) {
this.width = width;
this.height = height;
}

getAreaFunction() {
return function () {
return this.width * this.height;
};
}
}
```
11 changes: 11 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/strict.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
display: "严格模式"
oneline: "启用所有的严格类型检查选项。"
---

`strict` 选项可以启用很多的类型检查行为,从而更好的保证程序的正确性。
启用这个选项相当于启用了所有的 _严格模式系列_ 选项,这些选项在下面有概述。
之后你也可以根据自己的需要关闭任意严格模式系列检查。

TypeScript 的未来版本可能会在这个标志下加入额外的更严格的检查,所以 TypeScript 升级时可能会导致你的程序出现新的类型错误。
在适当和可能的情况下,TypeScript 将会添加一个相应的标志,以允许禁用这些更严格的检查行为。
35 changes: 35 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/strictBindCallApply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
display: "严格的 Bind Call Apply"
oneline: "检查 `bind`,`call` 和 `apply` 的参数是否与原函数匹配。"
---

当开启后,TypeScript 将检查内置函数 `bind`,`call` 和 `apply` 是否接受了被调用函数的正确参数:


```ts twoslash
// @strictBindCallApply: true
// @errors: 2345

// With strictBindCallApply on
function fn(x: string) {
return parseInt(x);
}

const n1 = fn.call(undefined, "10");

const n2 = fn.call(undefined, false);
```

否则,这些函数将接受任意参数,并且返回 `any`:

```ts twoslash
// @strictBindCallApply: false

// With strictBindCallApply off
function fn(x: string) {
return parseInt(x);
}

// Note: No error; return type is 'any'
const n = fn.call(undefined, false);
```
55 changes: 55 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/strictFunctionTypes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
display: "严格函数类型"
oneline: "在赋值函数时,进行用于确保参数和返回值是子类型兼容的检查。"
---

启用后,该选项将会使函数参数更正确的被检查。

这里有一个当 `strictFunctionTypes` 关闭时的简单例子:

```ts twoslash
// @strictFunctionTypes: false
function fn(x: string) {
console.log("Hello, " + x.toLowerCase());
}

type StringOrNumberFunc = (ns: string | number) => void;

// 不安全的赋值
let func: StringOrNumberFunc = fn;
// 不安全的调用 —— 将会异常
func(10);
```

当 `strictFunctionTypes` 选项 _开启_,这个错误将被正确的检测到:

```ts twoslash
// @errors: 2322
function fn(x: string) {
console.log("Hello, " + x.toLowerCase());
}

type StringOrNumberFunc = (ns: string | number) => void;

// 防止不安全的赋值
let func: StringOrNumberFunc = fn;
```

在开发此功能期间,我们发现了大量不安全的类继承结构,包括 DOM 中的一些类。
因此,这个选项仅适用于 _function_ 语法编写的函数,而不适用于 _method_ 语法的函数:

```ts twoslash
type Methodish = {
func(x: string | number): void;
};

function fn(x: string) {
console.log("Hello, " + x.toLowerCase());
}

// 这终究是一个不安全的赋值,但是没有检测到
const m: Methodish = {
func: fn,
};
m.func(10);
```
58 changes: 58 additions & 0 deletions packages/tsconfig-reference/copy/zh/options/strictNullChecks.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
---
display: "严格空检查"
oneline: "在类型检查时考虑 `null` 和 `undefined`。"
---

当 `strictNullChecks` 为 `false`,`null` 和 `undefined` 实际上被语言忽略。
这可能导致运行时出现意外的错误。

当 `strictNullChecks` 为 `true`, `null` 和 `undefined` 将有自己的不同的类型。如果你试图在预期的具体值中使用它们,你将会得到一个类型错误。

例如,在这段 TypeScript 代码中,不能确保 `users.find` 真的可以找到一个 `user`。
但是你可以把代码写得好像它一定可以找到一样:

```ts twoslash
// @strictNullChecks: false
// @target: ES2015
declare const loggedInUsername: string;

const users = [
{ name: "Oby", age: 12 },
{ name: "Heera", age: 32 },
];

const loggedInUser = users.find((u) => u.name === loggedInUsername);
console.log(loggedInUser.age);
```

将 `strictNullChecks` 设置为 `true` 将会抛出一个错误,即你在尝试使用 `loggedInUser` 之前没有确保它存在。

```ts twoslash
// @errors: 2339 2532
// @target: ES2020
// @strictNullChecks
declare const loggedInUsername: string;

const users = [
{ name: "Oby", age: 12 },
{ name: "Heera", age: 32 },
];

const loggedInUser = users.find((u) => u.name === loggedInUsername);
console.log(loggedInUser.age);
```

第二个例子失败了,因为 `array` 的 `find` 可以被看作类似如下简化形式:

```ts
// 当 strictNullChecks: true
type Array = {
find(predicate: (value: any, index: number) => boolean): S | undefined;
};

// 当 strictNullChecks: false undefined 被从类型系统中删除,
// 这将会允许你写出假设总是可以找到结果的代码。
type Array = {
find(predicate: (value: any, index: number) => boolean): S;
};
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
display: "严格属性初始化"
oneline: "检查被声明但是没有被在构造函数中指定初始值的类成员。"
---

当设置为 true 时,TypeScript 会将被声明但是没有在构造函数中指定初始值的类属性视为错误。

```ts twoslash
// @errors: 2564
class UserAccount {
name: string;
accountType = "user";

email: string;
address: string | undefined;

constructor(name: string) {
this.name = name;
// 注意,this.email 没有被赋值
}
}
```

在上述情况下:

- `this.name` 被指定初始值。
- `this.accountType` 被指定默认值。
- `this.email` 没有被指定,并且抛出一个错误。
- `this.address` 被声明为具有 `undefined` 类型,这意味着它不必被指定。