通过提示打字稿推断函数参数类型
问题描述:
我通常有这样的代码,但TS不能推断函数参数的类型,因为它不知道上下文。通过提示打字稿推断函数参数类型
有什么办法来提示编译器类型的功能?
router.get('/get', imget);
router.get('/send', imsend);
function imget(req, res, next) { }
function imsend(req, res, next) { }
export = router;
我尝试了不同类型的断言,但所有这些导致语法错误。
喜欢的东西:
function imget(req, res, next) { } as express.RequestHandler
答
使用变量/常量例如
const imget: express.RequestHandler = (req, res, next) => { }
但它可以防止功能提升! :( – OwnageIsMagic