为导出函数的commonjs模块添加libdef

时间:2017-04-25 21:34:36

标签: commonjs flowtype

我试图为微型库及其main exported function添加类型。我为它创建了一个这样的libdef:

流类型/ micro.js

// @flow
import type { Server, IncomingMessage, ServerResponse } from "http"

declare module "micro" {
  declare module.exports: {
    (): (fn: (req: IncomingMessage, res: ServerResponse) => void) => Server,
  }
}

我试图在这段代码中使用main函数:

的src / index.js

// @flow
const micro = require("micro")

const server = micro((req, res) => {
  res.end("Hello world!")
})

server.listen(5000)

在该代码段中,变量serverreqres都被输入为any,即使micro变量似乎确实有些变量从libdef中输入。这可以在flow创建的coverage report中看到。我已经尝试了几种不同的方法来定义函数,但似乎没有任何工作。没有找到语法错​​误,似乎找到了libdef。我做错了什么?

1 个答案:

答案 0 :(得分:0)

flow-typed definitions for lru-cache

中查看此示例

简而言之,这应该做你想做的事:

declare module 'micro' {
  declare var exports: (fn: (req: IncomingMessage, res: ServerResponse) => void) => Server;
}