简体   繁体   English

Typescript不解析npm包的定义文件

[英]Typescript does not resolve definition file for npm package

I just published a npm package written in typescript. 我刚刚发布了一个用typescript编写的npm Currently I have a lot of trouble getting the definition recognized by typescript (webback and vscode). 目前,我在使用typescript(webback和vscode)识别定义时遇到了很多麻烦。 The only solution that worked so far was to create a folder with the definition in node_modules/@types 到目前为止唯一有效的解决方案是使用node_modules/@types的定义创建一个文件夹

Briefly this is my package setup: 简而言之,这是我的包装设置:

tsconfig.json tsconfig.json

{
    "compilerOptions": {
        ...
        "outDir": "./lib/",
        "declaration": true,
        "declarationDir": "./src/",
    }
}

package.json 的package.json

{
    ...
    "types": "./index.d.ts",
}

index.d.ts index.d.ts

/// <reference path="src/nano-data-binding.d.ts" />

src/nano-data-binding.d.ts SRC /纳米数据binding.d.ts

I keep it in /src because it's autogenerated and I cannot control the path of the import. 我把它保存在/src因为它是自动生成的,我无法控制导入的路径。 Also if I try to use only declare var ... without import export statements to get a script instead of a module. 另外,如果我尝试仅使用declare var ...而不使用import export语句来获取脚本而不是模块。

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[];

Feel free to install the package, maybe it is just a small mistake somewhere. 随意安装包,也许这只是一个小错误。 Basically I want to get the nanoBind() and nanoBindAll() declared as globals. 基本上我想将nanoBind()nanoBindAll()声明为全局变量。

Edit 编辑

Additional things I tried. 我试过的其他事情。 Nothing works. 什么都行不通。

package.json - Npm package package.json - Npm包

{
    ...
    "types": "lib/nano-data-binding.d.ts",
    "typings": "lib/nano-data-binding.d.ts",
    "typescript": {
        "definition": "lib/nano-data-binding.d.ts"
    },
}

tsconfig.json - Local project tsconfig.json - 本地项目

{
    ...
    "files": [
        "node_modules/nano-data-binding/lib/nano-data-binding.d.ts"
    ]
}

在你的package.json你需要将types字段重命名为typings 。是的,这里不需要三重斜杠指令

Finally found something that works. 终于找到了有用的东西。 It looks like it's enough to use index.d.ts in the root level or specify a custom route in package.json of the package. 看起来在根级别使用index.d.ts或在package.json中指定自定义路由就足够了。

The problem was with my definition. 问题出在我的定义上。 It needs to declare a module. 它需要声明一个模块。

index.d.ts index.d.ts

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[];
}

And than the way it is imported 而不是它的导入方式

main.ts main.ts

import * as ndb from 'nano-data-binding' // Imports script and definition
require('nano-data-binding') // Ignores definition

The problem you have is due to your tsconfig.json tries to explicitly include the typing file. 您遇到的问题是由于您的tsconfig.json尝试显式包含键入文件。 npm package typings file are loaded automatically when you specify either types or typings field in your package.json. 当你在package.json中指定typestypings字段时,会自动加载npm package typings文件。

When you remove the entry in your files array in your tsconfig.json, it should just work. 当您删除tsconfig.json中的files数组中的条目时,它应该可以正常工作。

The solution you found (adding declare module 'nano-data-binding' { } ) is a solution for creating custom typings for some packages without typings. 您找到的解决方案(添加declare module 'nano-data-binding' { } )是一种解决方案,用于为某些包创建自定义类型而无需打字。

To be a bit more technical, when a typings file (d.ts) does not contain top-level import export statement, it is an ambient script file and it is globally scoped. 为了更具技术性,当一个打字文件(d.ts)不包含顶级导入导出语句时,它是一个环境脚本文件,它是全局范围的。 That's why you need declare module '...' to indicate which module your are adding typings for. 这就是为什么你需要declare module '...'来表明你正在为哪个模块添加输入。

You typically use them as in How to consume the Moment.js TypeScript definition file if my site is already using moment.min.js? 如果我的网站已经在使用moment.min.js,您通常会在如何使用Moment.js TypeScript定义文件中使用它们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM