Oguzhan Oguzhan - 1 year ago 180
TypeScript Question

Type casting on arrow functions

I would like to cast callback function type to another type.
For example, with express.

interface ExtendedRequest extends express.Request{
interface ExtendedRequestHandler{
(req:ExtendedRequest, res:express.Response, next:express.NextFunction):any;
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)=>{/*..*/});

Is there a way to apply the type casting on arrow functions?


Answer Source

TypeScript thinks you're trying to write a generic arrow function when written that way. You just need to add some parentheses to clear up the confusion:

app.post('/login', <D.ExtendedRequestHandler>((req,res)=>{/*..*/}));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download