microsoft / TypeScriptSamples

Community Driven Samples for TypeScript
3.19k stars 1.78k forks source link

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

Closed atom-morgan closed 8 years ago

atom-morgan commented 8 years ago

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.

DanielRosenwasser commented 8 years ago

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.

atom-morgan commented 8 years ago

I think that makes sense. Thanks for your response!