1

我想为我的库创建类型,该库使用来自<script>. 如果我构建库 ( 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"]
}
4

1 回答 1

2

.d.ts不会被复制,你应该.ts改用 + add <reference />in public-api.ts。结果编译器将创建dist/**/typings/yandex-maps/index.d.ts<reference />public-api.d.ts.

一些进一步的信息:Typescript 不会复制 d.ts 文件来构建

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"]
}

打字/yandex-maps/index.ts

declare namespace {
  ...
}

公共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';
于 2020-11-23T16:38:42.233 回答