Skip to content
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

Init translation of Do and donts #95

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

Init translation of Do and donts #95

wants to merge 2 commits into from

Conversation

hanyujie2002
Copy link
Member

No description provided.

@hanyujie2002 hanyujie2002 changed the title Do and donts Init translation of Do and donts Oct 26, 2024
Comment on lines +176 to 192
TypeScript 通过检查目标的任何签名是否可以使用源的参数调用来解析签名兼容性,*并且允许多余的参数*。例如,这段代码只有在使用可选参数正确编写签名时才会暴露错误:

```ts
function fn(x: (a: string, b: number, c: number) => void) {}
var x: Example;
// When written with overloads, OK -- used first overload
// When written with optionals, correctly an error
// 当使用重载编写时,OK——使用第一个重载
// 当使用可选参数编写时,如预期般出现了错误
fn(x.diff);
```

The second reason is when a consumer uses the "strict null checking" feature of TypeScript.
Because unspecified parameters appear as `undefined` in JavaScript, it's usually fine to pass an explicit `undefined` to a function with optional arguments.
This code, for example, should be OK under strict nulls:
第二个原因是当消费者使用 TypeScript 的“严格空检查”特性时。由于未指定的参数在 JavaScript 中会表现为 `undefined`,通常可以将显式的 `undefined` 传递给具有可选参数的函数。例如,这段代码在严格空检查下应该是 OK 的:

```ts
var x: Example;
// When written with overloads, incorrectly an error because of passing 'undefined' to 'string'
// When written with optionals, correctly OK
// 当使用重载编写时,错误地因为将‘undefined’传递给‘string’而报错
// 当使用可选参数编写时,正确 OK
x.diff("something", true ? undefined : "hour");
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't understand this section

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

第一个重载的签名是 diff(one: string): number;, 怎么能够赋值给 x: (a: string, b: number, c: number) => void 呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant