首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Angular Library 11:在构建中包含index.d.ts

Angular Library 11:在构建中包含index.d.ts
EN

Stack Overflow用户
提问于 2020-11-23 02:03:01
回答 1查看 1.3K关注 0票数 1

我想为我的库创建类型,它使用来自<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

代码语言:javascript
复制
Cannot find type definition file for 'yandex-maps'
/// <reference types="yandex-maps" />

再生产

https://github.com/ddubrava/angular8-yandex-maps/tree/feature/custom-typings

typings/yandex-map/index.d.ts

代码语言:javascript
复制
declare namespace ymaps {
  ...
}

tsconfig.lib.json

代码语言:javascript
复制
{
  "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"]
}
EN

回答 1

Stack Overflow用户

发布于 2020-11-24 00:38:42

.d.ts将不会被复制,您应该改用.ts +在public-api.ts中添加<reference />。因此,编译器将在public-api.d.ts中创建dist/**/typings/yandex-maps/index.d.ts<reference />

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

tsconfig.lib.json

代码语言:javascript
复制
{
  "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-map/index.ts

代码语言:javascript
复制
declare namespace {
  ...
}

public-api.ts

代码语言:javascript
复制
// <reference path="./lib/typings/yandex-maps/index.ts" />

更新:

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

代码语言:javascript
复制
import './lib/typings/yandex-maps/index';
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64957644

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档