Typescript does not resolve definition file for npm package
我刚刚发布了一个用打字稿编写的npm包。 目前,我很难通过打字稿(Webback和vscode)识别该定义。 到目前为止,唯一有效的解决方案是使用
简而言之,这是我的软件包设置:
tsconfig.json
1 2 3 4 5 6 7 8 | { "compilerOptions": { ... "outDir":"./lib/", "declaration": true, "declarationDir":"./src/", } } |
package.json
1 2 3 4 | { ... "types":"./index.d.ts", } |
索引
1 | /// <reference path="src/nano-data-binding.d.ts" /> |
src / nano-data-binding.d.ts
我将其保留在
1 2 3 | import { StringOrHTMLElement } from './interfaces/nano-data-binding'; export declare function nanoBind(parent: HTMLElement, ...selectors: StringOrHTMLElement[]): HTMLElement[]; export declare function nanoBindAll(parent: HTMLElement, ...selectors: string[]): HTMLElement[]; |
随时安装该软件包,也许这只是某个地方的一个小错误。 基本上,我想将
编辑
我尝试过的其他东西。 什么都没有。
package.json-Npm软件包
1 2 3 4 5 6 7 8 | { ... "types":"lib/nano-data-binding.d.ts", "typings":"lib/nano-data-binding.d.ts", "typescript": { "definition":"lib/nano-data-binding.d.ts" }, } |
tsconfig.json-本地项目
1 2 3 4 5 6 | { ... "files": [ "node_modules/nano-data-binding/lib/nano-data-binding.d.ts" ] } |
您遇到的问题是由于tsconfig.json试图显式包括键入文件。
当您在package.json中指定
当您删除tsconfig.json中
您找到的解决方案(添加
稍微讲一点技术,当键入文件(d.ts)不包含顶层import export语句时,它是一个环境脚本文件,并且是全局范围的。这就是为什么您需要
如果我的站点已经在使用moment.min.js,则通常按照如何使用Moment.js TypeScript定义文件中的说明使用它们?
终于找到了行之有效的东西。在根级别使用
问题出在我的定义上。它需要声明一个模块。
索引
1 2 3 4 5 6 7 8 | type StringOrHTMLElement = string | HTMLElement declare var nanoBind: (parent: HTMLElement, ...selectors: StringOrHTMLElement[]) => HTMLElement[]; declare var nanoBindAll: (parent: HTMLElement, ...selectors: string[]) => HTMLElement[]; declare module 'nano-data-binding' { export var nanoBind: (parent: HTMLElement, ...selectors: any[]) => HTMLElement[]; export var nanoBindAll: (parent: HTMLElement, ...selectors: string[]) => HTMLElement[]; } |
而且比导入的方式
主要
1 2 | import * as ndb from 'nano-data-binding' // Imports script and definition require('nano-data-binding') // Ignores definition |
在您的
是的,这里不需要三斜杠指令