wangdoc / typescript-tutorial

TypeScript 教程
https://wangdoc.com/typescript
2.5k stars 269 forks source link

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

Closed qiulang closed 1 year ago

qiulang commented 1 year ago

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

  1. nodejs 18 引入的fetch 没法很好的用ts标注,这个问题单 https://github.com/DefinitelyTyped/DefinitelyTyped/issues/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生效,参见这个问题单 https://github.com/microsoft/TypeScript/issues/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 commented 1 year ago

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

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