【问题标题】:Typescript compiler in AngularAngular 中的打字稿编译器
【发布时间】:2020-04-07 10:02:32
【问题描述】:

我想知道,如果有一些选项,如何从 TSC 命令中排除 node_modules 包。

我正在使用 Typescript v3.5.3。我的 tsconfig.json 是

{
  "compilerOptions": {
    "outDir": "build/dist",
    "module": "esnext",
    "target": "es5",
    "lib": ["es2018", "dom"],
    "sourceMap": true,
    "allowJs": true,
    "jsx": "preserve",
    "moduleResolution": "node",
    "rootDir": "src",
    "forceConsistentCasingInFileNames": true,
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "noImplicitAny": true,
    "strictNullChecks": true,
    "suppressImplicitAnyIndexErrors": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": false,
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "typeRoots": ["node_modules/@types"]
  },
  "exclude": [
    "build",
    "config",
    "node_modules"
  ],
  "include": [
    "src"
  ]
}

还有一个像这样的角脚本

import { Component } from "@angular/core";

@Component({
  selector: "app-home",
  styleUrls: ["./home.component.less"],
  templateUrl: "./home.component.html"
})
export class HomeComponent {
  title = "angular home";
  
  constructor(){
    this.log(123);
    this.log('hello world');
  }

  log(someArg) {
    console.log(someArg);
  }
}

现在,如果我将运行 TSC,我预计会出错,因为函数日志是隐式的任何类型。在这种情况下一切正常!但是如果我会使用这个代码

import { Node } from "@alfresco/js-api";
import { Component } from "@angular/core";

@Component({
  selector: "app-home",
  styleUrls: ["./home.component.less"],
  templateUrl: "./home.component.html"
})
export class HomeComponent {
  title = "angular home";
  selectedNode: Node;
  
  constructor(){
    this.log(123);
    this.log('hello world');
  }

  log(someArg) {
    console.log(someArg);
  }

}

我会从 node_modules 的包中得到错误,比如

node_modules/@alfresco/js-api/src/authentication/processAuth.ts:181:9 - 错误 TS2322:类型 'null' 不可分配给类型 'string |未定义'。

181 this.authentications.basicAuth.username = null; ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

我知道为什么,因为“noImplicitAny”设置为 true,但我想要的是忽略 node_modules 包!因此,在 tsconfig 中的 Angular 排除选项没有任何意义。我认为,构建是包@alfresco/js-api 包含在脚本中并被检查为一个。

有趣的是,如果我在 React.js 中使用类似的代码,一切都如我所料。当我使用带有“noImplicitAny”= true 的命令“TSC”并且 node_modules 包将被忽略时,是否有一些选项或东西?非常感谢!

【问题讨论】:

  • 嗯,我昨天遇到了类似的问题。那么将node_modules/@alfresco 添加到compilerOptions 中的exclude 数组将不起作用?
  • 不,不起作用,即使我添加了脚本的确切名称“processAuth.ts”也不起作用。

标签: angular typescript tsc


【解决方案1】:

使用 import 引用的文件必须通过 tsc 命令编译。排除是为了方便在运行tsc命令时,排除待编译文件中没有导入(或使用)的文件。

默认情况下,tsc命令会忽略node_modules,这就是时没有错误的原因@alfresco/js-api 不会被导入,当它被包含时,TSC 将解析它们并在这些文件上暗示 compilerOptions

参考tsconfig.json document

通过“files”或“include”属性包含的文件引用的任何文件也包括在内。同样,如果一个文件 B.ts 被另一个文件 A.ts 引用,那么 B.ts 不能被排除,除非引用文件 A.ts 也被指定在“排除”列表中。

所以这完全取决于错误和导入的模块,而不是 typescript 编译器。

请阅读此问题线程以获取更多详细信息,https://github.com/microsoft/TypeScript/issues/7432

因此,要解决此问题,请使用其他模块或关闭 Implicits 的编译器选项。

【讨论】:

  • 非常感谢。你知道吗,如何在构建之前只检查我的代码?我想提到所有 tsconfig 选项,但导入的包对我来说毫无意义(我不想检查它们)。谢谢!
  • 只有一个建议可能有效,就是等待 eslint 成为 Angular 的一部分。很少有文章说将 tslint 迁移到 eslint,但不知道效果好不好。
猜你喜欢
  • 1970-01-01
  • 2017-12-16
  • 1970-01-01
  • 1970-01-01
  • 2013-08-19
  • 1970-01-01
  • 2015-12-17
  • 1970-01-01
相关资源
最近更新 更多