在何处/如何为范围限定的私有npm模块添加d.ts? [英] Where/how to add d.ts for private, scoped npm modules?
问题描述
我正在尝试将Javascript项目移植到Typescript.我有一个像这样的依赖项:@myscope/utils
I am attempting to port a Javascript project to Typescript. I have a dependency named like: @myscope/utils
将其中的文件导入到ts文件中,如下所示:
A file from this is imported into the ts file as follows:
import date = require('@myscope/utils/date');
当我尝试对此进行编译时,出现以下错误:
When I attempt to compile this I get the following error:
src/subfolder/something.ts(12,23): error TS2307: Cannot find module '@myscope/utils/date'.
如何为这样的私有模块添加自己的类型?
How do I add my own typings for private modules like this?
推荐答案
我自己解决了这个问题. 我提到了有关编写声明文件
I managed to solve this myself. I referred to the information on writing declaration files
获取要解决的模块的关键是声明一个具有包完整路径名称的模块.我为模块创建了一个index.d.ts
文件,该文件已添加到tsconfig.json
的files
部分中.
The key to get the module to be resolved is to declare a module with a name of the full path of the package. I created a index.d.ts
file for the module, that I added to my files
section of tsconfig.json
.
文件包含以下声明:
declare namespace date {
function now(): number;
}
declare module "@myscope/utils/date" {
export = date;
}
尽管不是必需的,但我像输入一样构造了导入.在移植了应用程序的其余部分之后,我可能会在以后进行简化.
Though not necessary, I structured my imports like typings does. I may simplify later after I've ported the rest of the application.
我创建了以下文件:
mytypings/index.d.ts
mytypings/modules/@myscope/utils/index.d.ts
请注意,utils路径中没有日期部分.
Note there is no date part of the utils path.
mytypings/index.d.ts
包含到mytypings/modules/@myscope/utils/index.d.ts
的引用路径,然后被添加到tsconfig.json
的部分中.
mytypings/index.d.ts
contains a reference path to mytypings/modules/@myscope/utils/index.d.ts
and then was added to section of tsconfig.json
.
我的计划是以相同的方式添加其他库.
My plan is to add other libraries in the same way.
这篇关于在何处/如何为范围限定的私有npm模块添加d.ts?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!