Defining a default function export type

I am trying to write a definition for pouch-redux-middleware that exports a single function:

import PouchMiddleware from 'pouch-redux-middleware' PouchMiddleware({...}) 

Here is the definition of types:

 interface PouchMiddleware { (a: any): any; } declare var PouchMiddleware: PouchMiddleware; declare module "pouch-redux-middleware" { export = PouchMiddleware; } 

This results in an error:

The package-reduction-middleware module does not have a default export.

What is the correct way to declare this?

+5
source share
2 answers

To forward to commonjs modules, you may need to modify the definition file:

 declare function PouchMiddleware(a: any): any; declare module "pouch-redux-middleware" { export = PouchMiddleware; } 

Then import it like this:

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

Sucks. Someone might come up with a better answer.


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

You will need to compile with --allowSyntheticDefaultImports to make it 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 situation occurs with the reaction library. More details here ...

+4
source

In general, you can export and import a class using the following structure.

 module pouch-redux-middleware { export class pouch { } } 

To use the exported class, you need to write: -

 import {pouch} from 'FileName' 

Hello

Ajay

0
source

Source: https://habr.com/ru/post/1246430/


All Articles