Errors when Upgrade TypeScript to 4.4.2

766 阅读1分钟

常见错误

ts(2349)

症状

数组.every is not callable

处方

as 或者繁琐的类型守卫

ts(7057)

症状

yield 缺少返回值类型

处方

function *gen(): Iterator<any, any, any> {
}

ts(2794)

症状

Promsie rs()

处方

new Promise<void>()

ts(2691)

症状

An import path cannot end with a '.ts' extension. Consider importing 'xxx.js' instead.

处方

import 'xxx'

ts(2731)

症状

Implicit conversion of a 'symbol' to a 'string' will fail at runtime. Consider wrapping this expression in 'String(...)'.

处方

模板字符串全类型索引签名,不能用到 symbol,Exclude 掉或者 String

ts(2339)

症状

更精确的流程分析

let groups;
const isNonControll = groups === void 0;
if (isNonControll) {
  // ts(2339)
  if (groups.length === 0) setCurGroupOptions(EMPTY_ARRAY);
  else setCurGroupOptions(groups);
}

处方

修复逻辑错误

ts(2322)

症状

更精确的泛型,推断出来的错误

updateEntities src/features/common/components/Tree/treeUtils.ts

处方

as 或者修复类型错误

工程化

Trip

  • 由于 babel-loader 的配套没有跟上,使用模板字符串类型索引签名断言的时候,会提示 babel-loader 语法错误。