How to create an “external module” typescript defi

2019-04-28 02:48发布

问题:

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:

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";

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.

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;

Is this set of changes what would be expected when including this in an npm package?

回答1:

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.

In other words : an ambient file is not an external module