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

问题描述 投票:0回答:5

上周我使用

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'.
       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
typescript ts-migrate
5个回答
3
投票

我成功删除了所有

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

解决方案

确保从头开始使用

ts-migrate
,然后按照以下步骤操作:

  1. 不幸的是,

    ts-migrate
    的更新速度没有
    typescript
    那么快。所以首先要做的就是通过 ts-migrate 检查
    当前使用的 typescript 版本。就我而言,我必须将 Typescript 从 
    5.x.x
     回滚到 
    4.7.2
    
    

  2. 安装最重要的

    @types/xxxx

    库,例如:

    • @types/jest
      
      
    • @types/node
      
      
    • @types/react
      
      
    • @types/react-dom
      
      
  3. 运行

    ts-migrate

    具体命令:

    • yarn ts-migrate -- rename .
      (这会转换 js/x -> ts/x + 创建 
      tsconfig.json
       文件)
  4. 在您的

    tsconfig.json

    (由
    ts-migrate
    生成)中,确保您具有以下属性:

      如果您使用笑话:
    "types": [ "jest", "node" ]
    
    
      ts 配置在特定的所需路径中运行,就我而言
    • src
      :
    "include": ["src"]
    
    
  5. 运行此

    ts-migrate

    命令:

    • yarn ts-migrate migrate
      
      
  6. 检查 TS 错误: 一个。在我的

    package.json

    中,我添加了这个脚本:

    "checkTs": "tsc --noEmit",
    b.运行

    yarn checkTs

    时,可能会出现一些错误。如果与 
    Unused '@ts-expect-error' directive
     问题无关,请修复它们(因为 
    ts-migrate
     在某些特定情况下确实会破坏代码,但这种情况仍然很少见)

  7. 运行

    yarn ts-migrate reignore .

     这将重新计算 
    ts-migrate
     之前所做的事情,并希望清除上一波中剩余的错误

  8. 再次运行

    tsc --noEmit

    检查错误,你应该是
    0
    🎉


1
投票
我遇到了同样的问题,但我的错误也出现在那些非测试文件上。我认为这是因为

ts-migrate

 和您的 IDE 的打字稿版本不匹配。我通过在
package.json
中添加打字稿包来修复它,以使两个版本相同


0
投票
我遇到了同样的问题,在 tsconfig.json 中设置配置后必须重新启动 IDE(VS Code),然后它才会接受新的更改。


0
投票
在某些情况下这会起作用:

/** * @ts-expect-error */
当然如果你不想更改配置文件。


-6
投票
简单的回答。您使用了

// @ts-expect-error

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

© www.soinside.com 2019 - 2024. All rights reserved.