gpt4 book ai didi

typescript - 箭头函数的类型转换

转载 作者:行者123 更新时间:2023-12-05 01:45:58 24 4
gpt4 key购买 nike

我想将回调函数类型转换为另一种类型。例如, express 。

//definitions.ts
interface ExtendedRequest extends express.Request{
user:IUser;
}
interface ExtendedRequestHandler{
(req:ExtendedRequest, res:express.Response, next:express.NextFunction):any;
}
//app.ts
import * as D from './definitions';
...
// if i use with 'function' keyword it works
app.post('/login', <D.ExtendedRequestHandler>function(req,res){/*..*/});
//but if i try casting on anonymous arrow function, it doesnt work,
//i am getting error ',' expected
app.post('/login', <D.ExtendedRequestHandler>(req,res)=>{/*..*/});

有没有办法在箭头函数上应用类型转换?

谢谢..

最佳答案

TypeScript 认为您在尝试以这种方式编写通用箭头函数。您只需添加一些括号即可消除混淆:

app.post('/login', <D.ExtendedRequestHandler>((req,res)=>{/*..*/}));

关于typescript - 箭头函数的类型转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38962646/

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