【问题标题】:tsconfig.json doesn't exclude folder node_modules properlytsconfig.json 没有正确排除文件夹 node_modules
【发布时间】:2020-04-07 00:35:15
【问题描述】:

我正在尝试通过命令 npm run ng build 构建我的项目,但它会像这样从文件夹 node_modules 打印我的错误。 (因为 tsconfig.json 中的规则。)

node_modules/@alfresco/js-api/src/api/gs-core-rest-api/model/filePlan.ts:46:13 - error TS2322: Type 'UserInfo | undefined' is not assignable to type 'UserInfo'.
  Type 'undefined' is not assignable to type 'UserInfo'.

node_modules/@alfresco/js-api/src/authentication/oauth2Auth.ts:243:36 - error TS2345: Argument of type 'string | undefined' is not assignable to parameter of type 'string'.
  Type 'undefined' is not assignable to type 'string'.

node_modules/@alfresco/js-api/src/authentication/processAuth.ts:180:9 - error TS2322: Type 'null' is not assignable to type 'string | undefined'.

但文件夹 node_modules 在 tsconfig.json 的 exclude 参数中。规则应忽略文件夹 node_modules

这里是 tsconfig.json。

{
  "compilerOptions": {
    "baseUrl": "src",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "downlevelIteration": true,
    "experimentalDecorators": true,
    "module": "ESNext",
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "noUnusedLocals": true,
    "importHelpers": true,
    "target": "es5",
    "types": [],
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2018",
      "dom"
    ],
    "allowJs": true,
    "jsx": "preserve",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "resolveJsonModule": true,
    "paths": {}
  },
  "include": [
    "./src/**/*", "test.ts"
  ],
  "exclude": [
    "node_modules",
    "**/*.spec.ts"
  ],
  "angularCompilerOptions": {
    "fullTemplateTypeCheck": true,
    "strictInjectionParameters": true
  }
}

我想忽略文件夹 node_modules 中的错误。规则应适用于文件夹 src

感谢您的阅读。我感谢每一个答案。祝你有美好的一天。

【问题讨论】:

    标签: angular build alfresco node-modules tsconfig


    【解决方案1】:

    这很可能不是node_modules 的问题。是因为

    "strictNullChecks": true
    

    在您的代码中的某处,您将一个可为空的变量分配给一个不可为空的变量,或类似的东西。

    【讨论】:

    • 不不,严格的空检查是可以的,但问题是,来自 node_modules 的脚本由构建中的打字稿检查。 tsconfig 规则必须只针对开发者的代码,所以只有目录“src”。如果我将运行构建,那么我希望只看到我的代码中的错误和警告,而不是来自 node_modules。
    • 我将一个可为空的变量分配给文件夹 node_modules 内的一个不可为空的变量。我的意图是忽略文件夹 node_modules 中的错误。
    • @PavelFiala 你用的是什么版本的打字稿?
    • @IraklisGkougkousis 我使用的是打字稿版本 3.5.3。
    • 这可能是您无法避免的问题。如果您要导入的库本身不符合strictNullChecks,那么您无能为力。请同时阅读此主题:stackoverflow.com/questions/40164034/…
    【解决方案2】:

    你可以尝试使用Null- and undefined-aware types,它告诉 TS 有可能得到 null 或 undefined,这是有效的情况

    // Compiled with --strictNullChecks
    let x: number;
    let y: number | undefined;
    let z: number | null | undefined;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-11-01
      • 2018-04-10
      • 2015-07-07
      • 2017-05-24
      • 1970-01-01
      • 2023-02-01
      • 2017-04-07
      相关资源
      最近更新 更多