Skip to content

intro.md 中可能的描述错误 #8

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

Closed
m1stake opened this issue Aug 9, 2023 · 2 comments
Closed

intro.md 中可能的描述错误 #8

m1stake opened this issue Aug 9, 2023 · 2 comments

Comments

@m1stake
Copy link

m1stake commented Aug 9, 2023

“语言简介(intro.md)”中。
“上面示例中,例一的报错是因为变量一旦赋值了,就不允许再改变类型”,这段描述可能有问题。

变量的类型确定,应该是在变量声明的时候,而不是赋值的时候。

// 这段代码可以通过编译,因为在第一行 c 已经被推断为 any 类型(这只是我的猜测)
let c;
console.log(typeof c);
c = 1;
console.log(typeof c);
c = '1';
console.log(typeof c);
@ruanyf
Copy link
Contributor

ruanyf commented Aug 9, 2023

上面代码不报错,早就被诟病了,变量声明没有给出类型。

[1] microsoft/TypeScript#30899

[2] https://stackoverflow.com/questions/73440042/typescript-prevent-inferring-types-except-at-variable-declaration

@ruanyf ruanyf closed this as completed Aug 9, 2023
ruanyf added a commit that referenced this issue Aug 9, 2023

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
@m1stake
Copy link
Author

m1stake commented Aug 9, 2023

这种确实有点尴尬,其它语言类型推断都是一定要赋值的

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

No branches or pull requests

2 participants