我试图为微型库及其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)
在该代码段中,变量server
,req
和res
都被输入为any
,即使micro
变量似乎确实有些变量从libdef中输入。这可以在flow创建的coverage report中看到。我已经尝试了几种不同的方法来定义函数,但似乎没有任何工作。没有找到语法错误,似乎找到了libdef。我做错了什么?
答案 0 :(得分:0)
从flow-typed
definitions for lru-cache
简而言之,这应该做你想做的事:
declare module 'micro' {
declare var exports: (fn: (req: IncomingMessage, res: ServerResponse) => void) => Server;
}