Last week I used ts-migrate
to migrate our codebase to TypeScript. The tool adds a lot of // @ts-expect-error
comments in TS files to get it to pass the TS compiler and type checking. However, when I run yarn run tsc
to type check my codebase I get 5k+ errors stating error TS2578: Unused '@ts-expect-error' directive.
Am I missing a piece of config stating that these types of errors are okay? Looking for any advice to get this to pass TS compiler.
Here is my TS config for reference.
{
"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
}
}
Full example error message:
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'.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
I succeed to remove all error TS2578: Unused '@ts-expect-error' directive.
Solution:
Make sure you start from scratch with ts-migrate
, then follow those steps:
Unfortunately, ts-migrate
does not get updated as fast as typescript
. So first thing to do is to check the typescript version currently used by ts-migrate
. In my case I had to rollback Typescript from 5.x.x
to 4.7.2
Install most important @types/xxxx
libraries, such as:
@types/jest
@types/node
@types/react
@types/react-dom
Run ts-migrate
specific command:
yarn ts-migrate -- rename .
(this transforms js/x -> ts/x + create the tsconfig.json
file)In your tsconfig.json
(being generated by ts-migrate
), make sure you have those attributes:
"types": [
"jest",
"node"
]
src
: "include": ["src"]
Run this ts-migrate
command:
yarn ts-migrate migrate
Check TS errors:
a. In my package.json
, I added this script:
"checkTs": "tsc --noEmit",
b. when running yarn checkTs
, you might have some errors. Fix them if it's not related to the Unused '@ts-expect-error' directive
issue (because ts-migrate
does break the code in some particular case, still rare scenario though)
Run yarn ts-migrate reignore .
this will re-compute what ts-migrate
did before and hopefully clean remaining mistakes from previous wave
Running again tsc --noEmit
to check errors, you should be to 0
🎉
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With