gpt4 book ai didi

typescript - 默认函数导出类型定义

转载 作者:搜寻专家 更新时间:2023-10-30 20:37:54 25 4
gpt4 key购买 nike

我正在尝试为 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;
}

这会导致错误:

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

声明这个的正确方法是什么?

最佳答案

要转译为 commonjs 模块,您可能只需要将定义文件更改为:

declare function PouchMiddleware(a: any): any;

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

然后像这样导入它:

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

虽然有点糟透了。有人可能会提出更好的答案。


我相信我之前在这里的回答仅适用于针对 ES6 并将模块设置为“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...

关于typescript - 默认函数导出类型定义,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36411590/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com