【问题标题】:Automatically include declaration files of libraries outside of node_modules in Typescript Angular cli project在 Typescript Angular cli 项目中自动包含 node_modules 之外的库的声明文件
【发布时间】:2019-03-04 06:10:12
【问题描述】:

我有一个 Angular 项目,它有一个 lib 文件夹,用于需要修改 node_module 的情况。我可以使用 tsconfigs 路径选项连接 lib 文件夹,并且实际上 可以编译 应用程序以无错误地运行。问题是 vscodes tslint 仍然无法识别 lib 文件夹。

lib文件夹在项目的根目录下:

root/lib/ng2-smart-table

项目的 tsconfig 设置是:

tsconfig.json

{
  "compileOnSave": false,
  "compilerOptions": {
    "baseUrl": "./",
    "outDir": "./dist/out-tsc",
    "sourceMap": true,
    "declaration": false,
    "moduleResolution": "node",
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "target": "es5",
    "typeRoots": [
      "node_modules/@types"
    ],
    "lib": [
      "es2017",
      "dom"
    ]
  }
}

tsconfig.app.json

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "./",
    "types": [],
    "paths": {
      "@angular/*": [
        "../node_modules/@angular/*"
      ],
      "@lib/*": [
        "../lib/*"
      ],
    }
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts",
    "../node_modules/@nebular/**/*.spec.ts"
  ],
  "include": [
    "../src/*.ts",
    "../src/**/*.ts",
    "../node_modules/@nebular/**/*.ts",
    "../lib/**/*.ts",
  ]
}

如果您想下载此问题的基本示例,还有一个公共 repo。链接:https://github.com/Jonathan002/tslint-cannot-find-module-example

如何消除 vscode 中的 lint 错误?

【问题讨论】:

  • 你试过重启ide吗?
  • 是的,我已经多次重新启动它。示例项目是否可以在没有 lint 错误的情况下为您工作?

标签: angular typescript


【解决方案1】:

Below 配置文件对我有用。看看路径,做了一点修改:

{
  "extends": "../tsconfig.json",
  "compilerOptions": {
    "outDir": "../out-tsc/app",
    "module": "es2015",
    "baseUrl": "./",
    "types": [],
    "paths": {
      "@angular/*": [
        "../node_modules/@angular/*"
      ],

      "@lib/*": ["lib/*"]
    }
  },
  "exclude": [
    "test.ts",
    "**/*.spec.ts",
    "../node_modules/@nebular/**/*.spec.ts"
  ],
  "include": [
    "../src/*.ts",
    "../src/**/*.ts",
    "../node_modules/@nebular/**/*.ts",
    "../lib/**/*.ts",
  ]
}

【讨论】:

  • 您使用的是什么版本的 typescript 和 tslint?我已经用 typescript@2.7.2 和 tslint@5.10.0 尝试了你的配置,但我仍然收到找不到模块错误。
猜你喜欢
  • 2017-05-24
  • 2022-09-23
  • 1970-01-01
  • 1970-01-01
  • 2017-03-09
  • 2021-03-11
  • 1970-01-01
  • 2023-01-07
  • 1970-01-01
相关资源
最近更新 更多