首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >初学者tsconfig.json --对于不使用类型记录的纯javascript项目来说,这是最容易/最松散的吗?

初学者tsconfig.json --对于不使用类型记录的纯javascript项目来说,这是最容易/最松散的吗?
EN

Stack Overflow用户
提问于 2019-12-27 23:05:02
回答 1查看 509关注 0票数 7

我很难找到一个示例/初学者tsconfig.json文件,它是最不严格/最无约束/最宽松的选项。我有一个大型的javascript项目,我真的对修复大量的打字错误不感兴趣,但是,如果我的同事想写,我想让他们写。

我们使用的是创建-反应-应用程序,所以我可以使用这个TSC_COMPILE_ON_ERROR标志使它不影响正常使用的打字稿错误。(“当设置为true时,即使存在TypeScript类型检查错误,也可以运行并正确构建TypeScript项目。这些错误被打印为终端和/或浏览器控制台中的警告。”)

到目前为止,我只知道我需要allowJs: true,以便类型记录在..js/..jsx文件上运行。

所以我想启用类型记录,但是我想从最少数量的类型记录errors+warnings开始。随着我们的发展,我们可以解决我们看到的这些问题,并逐步实现更严格的选择。

更新:发现了一个类似的问题,不完全相同,但有些人可能会认为它是重复的:TypeScript - possible to disable type checking?

我试过用这个tsconfig.json文件..。这是我几个月前的一个旧的,有几处修改:

代码语言:javascript
复制
{
  "compilerOptions": {
    "target": "es5", // cra default
    "lib": [
      "es6", // old cra default?
      "dom", // cra default
      "dom.iterable", // cra default
      "esnext" // cra default
    ],
    "allowJs": true, // cra default
    "skipLibCheck": true, // cra default
    "esModuleInterop": true, // cra default
    "allowSyntheticDefaultImports": true, // cra default
    "strict": false, // cra default
    "forceConsistentCasingInFileNames": true, // cra default
    "module": "esnext", // cra default
    "moduleResolution": "node", // cra default
    "resolveJsonModule": true, // cra default
    "isolatedModules": true, // cra default
    "noEmit": true, // cra default
    "jsx": "react", // cra default

    "sourceMap": true, // old cra default?
    "checkJs": true,
    "noImplicitReturns": false, // old tsconfig was set to true
    "noImplicitThis": true,
    "noImplicitAny": false, // old tsconfig was set to true
    "strictNullChecks": false, // old tsconfig was set to true
    "suppressImplicitAnyIndexErrors": true,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "types": ["jest"],

    "disableSizeLimit": true,
    "downlevelIteration": true,
    "allowUmdGlobalAccess": true,
    "allowUnreachableCode": true,
    "allowUnusedLabels": true,
    "alwaysStrict": false, // default
    "newLine": "lf"
  },
  "awesomeTypescriptLoaderOptions": {
    "useBabel": true,
    "babelCore": "@babel/core"
  },
  "include": ["src"], // cra default
  "exclude": ["node_modules"] // from old tsconfig
}

我被这样的错误困住了:

代码语言:javascript
复制
Property 'languageSettingString' does not exist on type 'Window & typeof globalThis'.  TS2339

    1602 |         action.data.data.account.locale !== state.locale
    1603 |       ) {
  > 1604 |         window.languageSettingString = action.data.data.account.locale
         |                ^
    1605 |         return {
    1606 |           ...state,
    1607 |           locale: action.data.data.account.locale,

当我调用像this.props.setTaskValue这样的redux动作/道具时,我有一个错误.这显然是在redux connect调用中的同一个文件中。

代码语言:javascript
复制
Property 'setTaskValue' does not exist on type 'Readonly<{}> & Readonly<{ children?: ReactNode; }>'.ts(2339)
EN

回答 1

Stack Overflow用户

发布于 2022-11-18 17:02:46

这是我的tsconfig.strictest.json

代码语言:javascript
复制
{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true,

    "importsNotUsedAsValues": "error",
    "forceConsistentCasingInFileNames": true,

    /* Backwards compat flags */
    "noStrictGenericChecks": false,
    "suppressExcessPropertyErrors": false,
    "suppressImplicitAnyIndexErrors": false,

    /* Type Checking */
    "strict": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictBindCallApply": true,
    "strictPropertyInitialization": true,
    "noImplicitThis": true,
    "useUnknownInCatchVariables": true,
    "alwaysStrict": true,
    "noUnusedLocals": true,
    "noUnusedParameters": true,
    "exactOptionalPropertyTypes": true,
    "noImplicitReturns": true,
    "noFallthroughCasesInSwitch": true,
    "noUncheckedIndexedAccess": true,
    "noImplicitOverride": true,
    "noPropertyAccessFromIndexSignature": true,
    "allowUnusedLabels": false,
    "allowUnreachableCode": false
  }
}

您可以翻转所有这些来创建“最宽松”的配置。

我鼓励你首先用这个:

代码语言:javascript
复制
{
  "compileOnSave": true,
  // You could toggle this between "strictest" and "loosest"
  // "extends": "./tsconfig.loosest.json",
  "compilerOptions": {
    "incremental": true,
    "target": "es5",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "checkJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react-jsx"
  },
  "include": ["src"]
}

在此基础上,一个非常理想的选项是用快照跟踪每个文件的类型记录错误的状态。预提交钩子可以:

yarn tsc --pretty --noEmit

  • Breakup运行
  1. 每个文件的
  2. 存储错误在它们的as Foo.tsx.terrs (“类型错误”)

旁边

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59506821

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档