我想将回调函数类型转换为另一种类型。 例如,使用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)=>{/*..*/});
有没有办法在箭头函数上应用类型转换?
谢谢..
答案 0 :(得分:3)
TypeScript认为你在写这种方式时会尝试编写通用箭头函数。你只需要添加一些括号来消除混淆:
app.post('/login', <D.ExtendedRequestHandler>((req,res)=>{/*..*/}));