【问题标题】:How can I disable all typescript type checking?如何禁用所有打字稿类型检查?
【发布时间】:2019-06-27 15:20:02
【问题描述】:

我想在将来使用 TypeScript,但目前我选择在 Create React App 中安装 TypeScript。 (稍后我会回去添加类型)

因此,我想禁用所有类型检查。

现在,当我做这样的事情时:

<PlaceSearchBar
    placeSearchChanged={this.placeSearchChanged}
/>


class PlaceSearchBar extends React.Component {
...
}

我收到一个错误:

Type error: Type '{ placeSearchChanged: (place: any) => void; }' is not assignable to type 'IntrinsicAttributes & IntrinsicClassAttributes<PlaceSearchBar> & Readonly<{ children?: ReactNode; }> & Readonly<{}>'.
  Property 'placeSearchChanged' does not exist on type 'IntrinsicAttributes & IntrinsicClassAttributes<PlaceSearchBar> & Readonly<{ children?: ReactNode; }> & Readonly<{}>'.  TS2322

显然我需要在 React.Component&lt;placeSearchChanged:function&gt; 或类似的东西中声明类型。

我觉得这很烦人,我想禁用我的 tsconfig.json 中的所有检查。

如何禁用所有检查(但仍保持安装 TypeScript,以防未来)?

这是我当前的 tsconfig.json:

{
  "compilerOptions": {
    "target": "es6",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext",
    ],
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "forceConsistentCasingInFileNames": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "preserve",
    "noImplicitAny": false,
  },
  "include": [
    "src"
  ]
}

【问题讨论】:

  • 如果您想使用 .js 文件而不使用任何类型,或者如果您想使用 .ts 文件并在使用过程中慢慢引入类型,这个问题会很困惑。如果是后者,那么您最好的选择是TSC_COMPILE_ON_ERROR env 变量中投票率最低的答案之一

标签: javascript reactjs typescript


【解决方案1】:

没有类型的打字稿是 Javascript。 在没有 Typescript 的情况下启动您的项目,并在您准备好时进行转换。 以&lt;any&gt; 开头不是一个好习惯,我认为没有任何意义。

【讨论】:

  • 虽然这个答案在 OP 的上下文中是有意义的,但它没有回答标题(这就是我来这里的原因)。仅供参考:我正在寻找禁用生成的客户端文件中的所有类型检查,其中包含property not definitely accessed from constructor 等错误。
  • 接受的解决方案是关于 .js 和 .ts。这指向与这篇文章相同的方向。准备好使用 js 并切换到 ts。 @Karol 刚刚公开了 tsconfig 设置。
  • 在 Typescript Express 控制器中考虑这样的事情:``` // 绑定内部方法 Object.getOwnPropertyNames(ProjectController.prototype) .filter((methodName: string) => methodName !== 'constructor ') // @ts-ignore .forEach((method: string) => (this[method] = this[method].bind(this))); ``` 似乎不可能以 TS 喜欢的方式编写它,甚至允许和编译它,对于这种极端情况,我相信这个“​​关闭”评论/功能已经实现。
【解决方案2】:

我同意 nologin,这样做没有意义,但是如果你真的想要,我可以想到几种方法,这里有几个:

按文件禁用

在文件顶部添加此评论/* tslint:disable */

排除您的 src 文件夹

从 tslint.json 中排除您的代码文件夹(可能也需要在 tsconfig.json 上执行此操作

{
 // some linting options
  linterOptions: {
    exclude: ['src/**','components/**'],
  }
}

清空 tslint.json 和 tsconfig

只需将 tslint.json 和 tsconfig.json 文件替换为空对象

【讨论】:

  • 只有第二种解决方案有效。 TSLint 不是 typescript 编译器,空的 tsconfig 只会使用默认设置。
  • 是的,第二种解决方案是绕过编译器,但可能 TIMEX 也安装了一些需要绕过的 linting,这可能是必需的。前段时间空的配置文件还在工作,可能已经恢复(谢天谢地)
【解决方案3】:

Typescript 的 USP 是在编译时进行类型检查。它最终编译为 javascript。从没有打字稿开始。当您想要进行一些重构时,您始终可以将其包含在您的项目中。

https://www.typescriptlang.org/docs/handbook/migrating-from-javascript.html

【讨论】:

    【解决方案4】:

    将此添加到您的tsconfig.json

    {
      "compilerOptions": {
        ...
        "checkJs": false
        ...
      }
    }
    

    并暂时坚持使用.js/.jsx 文件。仅当您准备好使用类型时才使用.ts/.tsx 扩展。

    如果您希望逐行抑制错误,可以使用// @ts-ignore 注释。

    【讨论】:

    【解决方案5】:

    即使存在打字稿错误,您也可以让 CRA 构建发生。

    只需设置环境变量TSC_COMPILE_ON_ERROR=true

    查看CRA documentation了解更多详细信息

    【讨论】:

    • 构建将运行,但错误会使控制台混乱。我建议在tsconfig.json 中使用exclude 指令
    • 我认为在“排除”中指定会排除最终捆绑包中的那些文件,并且无法在其他模块中使用它们。如果文件只是类型定义文件(.d.ts),那没关系
    【解决方案6】:

    我遇到了同样的问题,我尝试修改tsconfig.json文件如下。

    之前:

    /* To learn more about this file see: https://angular.io/config/tsconfig. */
    {
      "compileOnSave": false,
      "compilerOptions": {
          "baseUrl": "./",
          "outDir": "./dist/out-tsc",
          "forceConsistentCasingInFileNames": true,
        "strict": true,
        "noImplicitOverride": true,
        "noPropertyAccessFromIndexSignature": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2017",
        "module": "es2020",
        "lib": [
          "es2020",
          "dom"
        ]
      },
      "angularCompilerOptions": {
        "enableI18nLegacyMessageIdFormat": false,
        "strictInjectionParameters": true,
        "strictInputAccessModifiers": true,
        "strictTemplates": true
      }
    }
    

    之后

    /* To learn more about this file see: https://angular.io/config/tsconfig. */
    {
      "compileOnSave": false,
      "compilerOptions": {
          "baseUrl": "./",
          "outDir": "./dist/out-tsc",
          "forceConsistentCasingInFileNames": true,
        "strict": true,
        "noImplicitOverride": true,
        "noPropertyAccessFromIndexSignature": true,
        "noImplicitReturns": true,
        "noFallthroughCasesInSwitch": true,
        "sourceMap": true,
        "declaration": false,
        "downlevelIteration": true,
        "experimentalDecorators": true,
        "moduleResolution": "node",
        "importHelpers": true,
        "target": "es2017",
        "module": "es2020",
        "lib": [
          "es2020",
          "dom"
        ]
      },
      "angularCompilerOptions": {
        "enableI18nLegacyMessageIdFormat": false,
        "strictInjectionParameters": false,
        "strictInputAccessModifiers": true,
        "strictTemplates": false
      }
    }
    

    更改 "strictInjectionParameters": false,"strictTemplates": false 对我有用。

    【讨论】:

      猜你喜欢
      • 2018-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-16
      • 2020-11-18
      • 2019-07-29
      • 2021-01-09
      • 1970-01-01
      相关资源
      最近更新 更多