【问题标题】:Angular Library 11: include index.d.ts in buildAngular Library 11:在构建中包含 index.d.ts
【发布时间】:2021-03-05 12:46:32
【问题描述】:

我想为我的库创建类型,它使用来自<script> 的外部 API。如果我构建库 (ng build angular8-yandex-maps --prod) 一切正常,但是当我尝试在 Angular 应用程序中导入构建的库时,它会失败 - Cannot find namespace 'ymaps'Cannot find type definition file for 'yandex-maps' 等。

构建中不包含声明的命名空间,是否可以包含它?

dist/**/*.component.d.ts

Cannot find type definition file for 'yandex-maps'
/// <reference types="yandex-maps" />

复制

打字/yandex-maps/index.d.ts

declare namespace ymaps {
  ...
}

tsconfig.lib.json

{
  "extends": "../../tsconfig.json",
  "compilerOptions": {
    "outDir": "../../out-tsc/lib",
    "target": "es2015",
    "declaration": true,
    "declarationMap": true,
    "inlineSources": true,
    "types": ["yandex-maps"],
    "typeRoots": ["../../node_modules/@types", "src/lib/typings"],
    "lib": ["dom", "es2018"]
  },
  "angularCompilerOptions": {
    "skipTemplateCodegen": true,
    "strictMetadataEmit": true,
    "enableResourceInlining": true
  },
  "exclude": ["src/test.ts", "**/*.spec.ts"]
}

【问题讨论】:

    标签: angular typescript angular-cli angular-library angular-devkit


    【解决方案1】:

    .d.ts 不会被复制,你应该使用.ts 代替 + 在public-api.ts 中添加&lt;reference /&gt;。因此编译器将在public-api.d.ts 中创建dist/**/typings/yandex-maps/index.d.ts&lt;reference /&gt;

    更多信息:Typescript does not copy d.ts files to build

    tsconfig.lib.json

    {
      "extends": "../../tsconfig.json",
      "compilerOptions": {
        "outDir": "../../out-tsc/lib",
        "target": "es2015",
        "declaration": true,
        "declarationMap": true,
        "inlineSources": true,
        "types": [],
        "lib": ["dom", "es2018"]
      },
      "angularCompilerOptions": {
        "skipTemplateCodegen": true,
        "strictMetadataEmit": true,
        "enableResourceInlining": true
      },
      "exclude": ["src/test.ts", "**/*.spec.ts"]
    }
    

    typings/yandex-maps/index.ts

    declare namespace {
      ...
    }
    

    public-api.ts

    // <reference path="./lib/typings/yandex-maps/index.ts" />
    

    更新:

    ESLint:不要对 ./lib/typings/yandex-maps/index.ts 使用三斜杠引用,而是使用 import 样式。(@typescript-eslint/triple-slash-reference)

    import './lib/typings/yandex-maps/index';
    

    【讨论】:

      猜你喜欢
      • 2021-11-01
      • 1970-01-01
      • 2020-05-26
      • 2021-03-11
      • 2021-10-26
      • 2017-01-25
      • 1970-01-01
      • 2020-07-19
      • 1970-01-01
      相关资源
      最近更新 更多