-
Notifications
You must be signed in to change notification settings - Fork 4
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
hanyujie2002
wants to merge
2
commits into
main
Choose a base branch
from
do-and-donts
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
hanyujie2002
commented
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"); |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.