2

I've run into several cases where one library extends another library, particularly in my uses with React and Redux.

As an example, say I was using a JS library that exported a function that could be defined like this:

function dispatch(action:IAction):void;

interface IAction {
    type: string;
}

And I use another JS library that enhances the dispatch function to allow callbacks, which could be defined like this:

function dispatch(action:IAction | IActionCallback):void;

interface IActionCallback {
    (dispatch:IDispatch):void;
}

The problem is that the second library augments the first library. How can this be properly expressed in typings? Or can it be?

Aaron Beall
  • 49,769
  • 26
  • 85
  • 103

1 Answers1

0

So what you would do is have the declaration file of the extending library 'augment' the original library.

The redux-thunk declaration file does almost exact that: it augments the Dispatch interface of redux.

For more information, I wrote a more detailed answer on the different possibilities if you want to augment another module, depending on whether your declaration file needs to be global. Or have a look at the page on declaration merging from the typescript handbook.

If you have any more questions, I'm happy to help .

Community
  • 1
  • 1
Pelle Jacobs
  • 2,379
  • 1
  • 21
  • 25
  • Thanks, still trying to digest this... the problem I'm having here with Redux Thunk is that if I `import {IDispatch} from "redux"` it doesn't seem to know about the redux-thunk enhancement to allow functions to be actions. It only works if I `import {IDispatch} from "~redux-thunk~redux"` which is odd an I'm not sure the right way to import something, since that isn't a real module. – Aaron Beall Sep 13 '16 at 14:25