Typescript 默认函数导出类型定义

Typescript 默认函数导出类型定义,typescript,Typescript,我正在尝试为编写一个类型定义,它导出一个函数: import PouchMiddleware from 'pouch-redux-middleware' PouchMiddleware({...}) 以下是我对打字的定义: interface PouchMiddleware { (a: any): any; } declare var PouchMiddleware: PouchMiddleware; declare module "pouch-redux-middleware" {

我正在尝试为编写一个类型定义,它导出一个函数:

import PouchMiddleware from 'pouch-redux-middleware'

PouchMiddleware({...})
以下是我对打字的定义:

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

declare var PouchMiddleware: PouchMiddleware;

declare module "pouch-redux-middleware" {
  export = PouchMiddleware;
}
这将导致以下错误:

模块“Pocket redux中间件”没有默认导出


正确的声明方式是什么?

要传输到commonjs模块,您可能只需将定义文件更改为:

declare function PouchMiddleware(a: any): any;

declare module "pouch-redux-middleware" {
    export = PouchMiddleware;
}
然后像这样导入它:

import PouchMiddleware = require("pouch-redux-middleware");
不过有点糟糕。也许有人会给出更好的答案


我相信我之前的回答仅适用于针对ES6并将模块设置为“ES2015”:

您需要使用进行编译以使其工作,然后将定义文件中的接口更改为函数:

declare function PouchMiddleware(a: any): any;

declare module "pouch-redux-middleware" {
    export = PouchMiddleware;
}
react库也会发生类似的情况


通常,您可以使用以下结构导出和导入类

module pouch-redux-middleware {
     export class pouch {

     }
}
要使用导出的类,您需要编写:-

import {pouch} from 'FileName'
问候


Ajay

谢谢,尽管当我尝试呼叫它时,我使用了选项get
找不到name
。TypeScript的错误消息太糟糕了。但是我猜定义中有点错误使其编译,但在运行时失败:
Pocket\u redux\u middleware\u 1。默认值不是函数
@jgillich I更新了我的答案。我不知道为什么在将模块类型设置为“commonjs”时它会添加
.default
。这一个也给了我
pocket\u redux\u middleware\u 1。很遗憾,默认值不是一个函数。@jgillich是的,好的,我放弃了这个。我相信在过去,我曾看到有人建议不要使用ES6导入,并建议这样做:
import-pockmiddleware=require(“pockredux中间件”)我无法更改导出的内容。如果您想制作Pocket redux中间件的.d.ts文件,然后参考angular-material.d.ts文件获取示例文件。他们的.d.ts文件中没有导出成员。