错误 TS2578:未使用的“@ts-expect-error”指令

joh*_*tew 25 typescript ts-migrate

上周我曾经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
    }
}
Run Code Online (Sandbox Code Playgroud)

完整的错误消息示例:

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'.
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Run Code Online (Sandbox Code Playgroud)

Tom*_*bel -7

简单的回答。你用的// @ts-expect-error地方没有错误。我建议通过正则表达式在 VS Code 中替换它\s*\/\/\s*@ts-expect-error(什么都没有)以删除所有注释。