tsconfig.json没有正确排除文件夹node_modules

时间:2019-12-13 14:31:47

标签: angular build alfresco node-modules tsconfig

我正在尝试通过命令 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

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

2 个答案:

答案 0 :(得分:1)

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

"strictNullChecks": true

在代码的某些地方,您正在将可为空的变量分配给不可为空的变量,或者类似的东西。

答案 1 :(得分:0)

您可以尝试使用Null- and undefined-aware types,它告诉TS可能会出现null或undefined的情况,这是有效的情况

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