如何创建“外部模块"要包含在 npm 包中的打字稿定义文件? [英] How to create an "external module" typescript definition file to include with an npm package?

查看:73
本文介绍了如何创建“外部模块"要包含在 npm 包中的打字稿定义文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近添加了一个打字稿定义文件 对于开源 redux-ui-router 库,但我现在在 Typescript 1.7.3 中遇到如下错误:

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:

错误 TS2656:导出的外部包类型文件'C:/.../node_modules/redux-ui-router/index.d.ts' 是不是一个模块.请联系包作者更新包定义.

error TS2656: Exported external package typings file 'C:/.../node_modules/redux-ui-router/index.d.ts' is not a module. 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";

我是 Typescript 的新手,我找不到关于这个定义文件在包含在 npm 包中时应该是什么样子的清晰描述.有一个 wiki 条目 讨论了 npm 包的类型,但是除了重复应该使用外部模块的方向之外,没有一个具体的例子可以工作.

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. 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.

更正我已经尝试删除 declare module "redux-ui-router" { 代码,这似乎在重新启动 webpack 后工作,我用它来编译所有内容(为简洁起见,我删除了评论):

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):

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;

将这些更改包含在 npm 包中时,这组更改是否符合预期?

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?

是的.导出需要是文件的根级别.

Yes. The exports need to be root level at the file.

换句话说:环境文件不是外部模块

这篇关于如何创建“外部模块"要包含在 npm 包中的打字稿定义文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆