【发布时间】:2019-04-13 05:46:49
【问题描述】:
我正在开发一个名为'vee-type-safe' 的库,用于运行时类型检查。一切都很顺利,直到我添加了一个子目录/express 和一个文件/express/index.ts,我在其中导出了一些ExpressJS 中间件类型检查工厂。
所以我有以下结构:
vee-type-safe
|- build
|- package.json
|- declarations
| |- is-iso-date.d.ts
|
|- tsconfig.json
|- index.ts // lightweight core library
|- express
|-index.ts // express middleware factories
在express/index.ts 文件中,我导入我的库核心'../index.ts' 模块。
在我的核心模块中,我有以下导入:
import isISODate = require('is-iso-date');
'is-iso-date' 包没有类型,所以我用is-iso-date.d.ts 创建了declarations 目录,就这么简单:
declare module 'is-iso-date' {
function isISODate(suspect: string): boolean;
export = isISODate;
}
我将"typeRoots": [ ..., "declarations"] 添加到tsconfig.json
我将"types": "build/index.d.ts" 添加到package.json
当我在我的包中运行 tsc 时,所有内容都可以编译,没有错误。
但是当我通过 npm 将我的 'vee-type-safe' 库安装为某个项目的依赖项并尝试编译它时,我收到以下错误:
Could not find a declaration file for module 'is-iso-date'.
'/home/tegeran/projects/is-iso-date-issue/node_modules/is-iso-date/index.js'
implicitly has an 'any'type.
Try `npm install @types/is-iso-date` if it exists or add a new declaration (.d.ts)
file containing `declare module 'is-iso-date';`
1 import isISODate = require('is-iso-date');
仅当我导入 'vee-type-safe/express' 子模块时才会发生这种情况。当我导入我的核心 'vee-type-safe' 模块时,不会产生任何错误。我在这里想念什么?
我创建了a github repo with a bare minimum project to demonstrate this error
【问题讨论】:
标签: typescript npm module node-modules