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

Viewed 9776

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'.
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3 Answers

I had the same issue, but my error also showed up on those non-test files. I think it is because there is a typescript version mismatch for ts-migrate and your IDE. I fixed it by adding a typescript package into package.json, to make both verisons the same

I got this same issue, had to restart my IDE (VS Code) after setting the configuration in tsconfig.json, and then it picked up on the new changes.

Simple answer. You used // @ts-expect-error where is no error. I recommend replace it in VS Code via regex \s*\/\/\s*@ts-expect-error (to nothing) to delete all of comments.

Related