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

建议补充一节列举ts 不那么好用的地方 #80

Closed
qiulang opened this issue Sep 11, 2023 · 1 comment
Closed

建议补充一节列举ts 不那么好用的地方 #80

qiulang opened this issue Sep 11, 2023 · 1 comment

Comments

@qiulang
Copy link

qiulang commented Sep 11, 2023

从我自己使用经验,我真的体会到ts有时候不那么好用,引入的问题比起要解决问题反而更麻烦,举几个例子

  1. nodejs 18 引入的fetch 没法很好的用ts标注,这个问题单 node@18.x.x version not fetch global api ? DefinitelyTyped/DefinitelyTyped#60924 说得很清楚: 还没人能完全搞清楚怎么标注fetch好, 所以目前变通方法都让人感觉很麻烦

The short version is that no one has implemented the types for fetch in this package yet. There are some ideas about how best to handle it but I don't know if there is actually a consensus among maintainers of this package. If you'd like to help with that, DT has good guidelines for getting started as a contributor, and I'm sure several people on this thread would be eager to review a MR.

  1. narrow 这个功能没法对object的key生效,参见这个问题单 Narrowing for key type by in operator microsoft/TypeScript#43284
    所以在js 本来很简单,遍历 Object key的代码在ts 反而变得很麻烦
let options = {
    foo: 'bar',
    bar: 'baz',
}

for (const [key, value] of process.argv) {
    if (key in options) {
        options[key] = value; // 报错,尽管我已经在if 里
    }
}

进一步数组的 reduce 方法标注就会变得很复杂,参见 https://stackoverflow.com/questions/52856496/typescript-object-keys-return-string

@ruanyf
Copy link
Contributor

ruanyf commented Sep 12, 2023

多收集一些材料,可以加这一章,作为使用 TypeScript 的注意点。

目前,只把遇到的问题,插在相应章节里面。

@ruanyf ruanyf closed this as completed Sep 12, 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