简体   繁体   English

如何创建“外部模块”打字稿定义文件以包含在npm包中?

[英]How to create an “external module” typescript definition file to include with an npm package?

I recently added a typescript definition file for the open source redux-ui-router library, but I'm now getting errors like the following with Typescript 1.7.3: 我最近为开源redux-ui-router库添加了一个打字稿定义文件 ,但是现在我收到类似打字稿1.7.3的错误:

error TS2656: Exported external package typings file 'C:/.../node_modules/redux-ui-router/index.d.ts' is not a module. 错误TS2656:导出的外部软件包类型文件'C:/.../ node_modules / redux-ui-router / index.d.ts'不是一个模块。 Please contact the package author to update the package definition. 请与软件包作者联系以更新软件包定义。

I am trying to import this library with code like this in my typescript files: 我正在尝试在我的打字稿文件中使用以下代码导入该库:

import ngReduxUiRouter from "redux-ui-router";

I'm new to Typescript, and I can't find a clear description of what exactly this definition file should look like when included with an npm package. 我是Typescript的新手,在npm软件包中包含该定义文件时,我找不到确切的描述。 There's a wiki entry that talks about typings for npm packages, but outside of repeating the direction that an external module should be used, there's not a concrete example to work from. 有一个Wiki条目讨论了npm软件包的类型,但是除了重复说明应该使用外部模块的方向外,没有具体的示例可以使用。

CORRECTION I've tried removing the declare module "redux-ui-router" { code, and that seemed to work after restarting webpack, which I'm using to compile everything (I removed the comments for brevity): 更正了我尝试删除declare module "redux-ui-router" {代码, 并且在重启webpack之后似乎可以正常工作,我正在使用它来编译所有内容 (为简洁起见,我删除了注释):

export interface ReduxUIRouterAction {
    type: string;
    payload: any;
}
export interface ReduxUIRouterState {
    currentState: Object;
    currentParams: Object;
    prevState: Object;
    prevParams: Object;
}
export function router(state: ReduxUIRouterState, action: ReduxUIRouterAction): ReduxUIRouterState;
export var ngReduxUiRouter: string;
export function stateGo(to: string, params?: Object, options?: Object): ReduxUIRouterAction;
export function stateReload(state: any): ReduxUIRouterAction;
export function stateTransitionTo(to: string, params?: Object, options?: Object): ReduxUIRouterAction;
export default ngReduxUiRouter;

Is this set of changes what would be expected when including this in an npm package? 当将其包含在npm软件包中时,这套更改会是什么期望吗?

Is this set of changes what would be expected when including this in an npm package? 当将其包含在npm软件包中时,这套更改会是什么期望吗?

Yes. 是。 The exports need to be root level at the file. 导出必须是文件的根级别。

In other words : an ambient file is not an external module 换句话说: 环境文件不是外部模块

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

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