Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error TS2578: Unused '@ts-expect-error' directive

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'.
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
like image 705
johnstew Avatar asked Sep 01 '25 17:09

johnstew


1 Answers

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:

  1. 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

  2. Install most important @types/xxxx libraries, such as:

    • @types/jest
    • @types/node
    • @types/react
    • @types/react-dom
  3. Run ts-migrate specific command:

    • yarn ts-migrate -- rename . (this transforms js/x -> ts/x + create the tsconfig.json file)
  4. In your tsconfig.json (being generated by ts-migrate), make sure you have those attributes:

    • If you're using jest:
     "types": [
       "jest",
       "node"
     ]
    
    • The ts config runs in specific desired path(s), in my case src:
     "include": ["src"]
    
  5. Run this ts-migrate command:

    • yarn ts-migrate migrate
  6. 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)

  7. Run yarn ts-migrate reignore . this will re-compute what ts-migrate did before and hopefully clean remaining mistakes from previous wave

  8. Running again tsc --noEmit to check errors, you should be to 0 🎉

like image 80
Emidomenge Avatar answered Sep 04 '25 05:09

Emidomenge