上周我使用
ts-migrate
将我们的代码库迁移到 TypeScript。该工具在 TS 文件中添加了大量 // @ts-expect-error
注释,以使其通过 TS 编译器和类型检查。但是,当我运行 yarn run tsc
来检查我的代码库时,我收到 5k+ 错误,指出 error TS2578: Unused '@ts-expect-error' directive.
我是否缺少一段配置,说明这些类型的错误没有问题?寻求任何建议以使其通过 TS 编译器。
这是我的 TS 配置供参考。
{
"compilerOptions": {
"target": "ES2016",
"module": "commonjs",
"lib": ["esnext", "dom"],
"allowJs": true,
"checkJs": false,
"jsx": "react",
"declaration": false,
"declarationMap": false,
"sourceMap": false,
"skipDefaultLibCheck": true,
"skipLibCheck": true,
"noEmit": true,
"isolatedModules": true,
"strict": false,
"noImplicitAny": false,
"strictNullChecks": false,
"strictFunctionTypes": false,
"strictBindCallApply": false,
"strictPropertyInitialization": false,
"noImplicitThis": false,
"alwaysStrict": false,
"noUnusedLocals": false,
"noUnusedParameters": false,
"noImplicitReturns": false,
"noFallthroughCasesInSwitch": false,
"baseUrl": "./",
"typeRoots": [
"./types/",
"./node_modules/@types"
],
"allowSyntheticDefaultImports": true,
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true
}
}
完整的错误消息示例:
src/WorkspacePicker/__tests__/index.test.tsx:27:1 - error TS2578: Unused '@ts-expect-error' directive.
27 // @ts-expect-error ts-migrate(2582) FIXME: Cannot find name 'describe'. Do you need to instal... Remove this comment to see the full error message
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/WorkspacePicker/__tests__/index.test.tsx:29:3 - error TS2578: Unused '@ts-expect-error' directive.
29 // @ts-expect-error ts-migrate(2582) FIXME: Cannot find name 'test'. Do you need to install ty... Remove this comment to see the full error message
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
src/WorkspacePicker/__tests__/index.test.tsx:38:5 - error TS2578: Unused '@ts-expect-error' directive.
38 // @ts-expect-error ts-migrate(2304) FIXME: Cannot find name 'expect'.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
我成功删除了所有
error TS2578: Unused '@ts-expect-error' directive.
解决方案:
确保从头开始使用
ts-migrate
,然后按照以下步骤操作:
不幸的是,
ts-migrate
的更新速度没有typescript
那么快。所以首先要做的就是通过 ts-migrate
检查当前使用的 typescript 版本。就我而言,我必须将 Typescript 从
5.x.x
回滚到
4.7.2
@types/xxxx
库,例如:
@types/jest
@types/node
@types/react
@types/react-dom
ts-migrate
具体命令:
yarn ts-migrate -- rename .
(这会转换 js/x -> ts/x + 创建
tsconfig.json
文件)
tsconfig.json
(由
ts-migrate
生成)中,确保您具有以下属性:
"types": [
"jest",
"node"
]
src
:
"include": ["src"]
ts-migrate
命令:
yarn ts-migrate migrate
package.json
中,我添加了这个脚本:
"checkTs": "tsc --noEmit",
b.运行yarn checkTs
时,可能会出现一些错误。如果与
Unused '@ts-expect-error' directive
问题无关,请修复它们(因为
ts-migrate
在某些特定情况下确实会破坏代码,但这种情况仍然很少见)
yarn ts-migrate reignore .
这将重新计算
ts-migrate
之前所做的事情,并希望清除上一波中剩余的错误
tsc --noEmit
检查错误,你应该是
0
🎉
ts-migrate
和您的 IDE 的打字稿版本不匹配。我通过在
package.json
中添加打字稿包来修复它,以使两个版本相同
/**
* @ts-expect-error
*/
当然如果你不想更改配置文件。
// @ts-expect-error
,没有错误。我建议通过正则表达式
\s*\/\/\s*@ts-expect-error
在 VS Code 中替换它(什么都没有)以删除所有注释。