microsoft/TypeScriptSamples

Why are there inconsistencies with "req" and "res"?

atom-morgan opened this issue · 2 comments

In index.ts the req and res objects have their type specified.

export function index(req: express.Request, res: express.Response) { };

But in app.ts they're missing.

app.get('/user/:userid', (req, res) => { };

I'm in the process of converting an ES6 Express API to TypeScript and was wondering if there is a reason for this.

Hey @atom-morgan, in some places, the types of each parameter can be inferred.

In the second case, TypeScript can figure out the parameter types, because it know the type of app.get. But in the first, it's harder to tell what consumers (if any) might use the index function. So without type annotations, TypeScript would've taken the most lenient route and picked any for each parameter.

If you'd like to know the places where TypeScript would've picked any out, you can use the --noImplicitAny compiler option, where the compiler will flag locations where it couldn't pick a better type.

I think that makes sense. Thanks for your response!