Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Default function export type definition

Tags:

typescript

I'm attempting to write a typing definition for pouch-redux-middleware, which exports a single function:

import PouchMiddleware from 'pouch-redux-middleware'

PouchMiddleware({...})

Here is the typings definition I have:

interface PouchMiddleware {
  (a: any): any;
}

declare var PouchMiddleware: PouchMiddleware;

declare module "pouch-redux-middleware" {
  export = PouchMiddleware;
}

This results in the error:

Module '"pouch-redux-middleware"' has no default export.

What is the correct way to declare this?

like image 589
jgillich Avatar asked Apr 04 '16 19:04

jgillich


1 Answers

For transpiling to commonjs modules, you might just have to change the definition file to:

declare function PouchMiddleware(a: any): any;

declare module "pouch-redux-middleware" {
    export = PouchMiddleware;
}

Then import it like so:

import PouchMiddleware = require("pouch-redux-middleware");

Kind of sucks though. Someone might come along with a better answer.


I believe my previous answer here only works for targeting ES6 and setting the module as "ES2015":

You will need to compile with --allowSyntheticDefaultImports in order to get it to work and then change the interface in the definition file to a function:

declare function PouchMiddleware(a: any): any;

declare module "pouch-redux-middleware" {
    export = PouchMiddleware;
}

A similar thing happens with the react library. Read more here...

like image 50
David Sherret Avatar answered Nov 09 '22 03:11

David Sherret