RecipesChapter 12: Type Development Strategies

Refining Types Step by Step

Recipe 12.2 from The TypeScript Cookbook

app.get("/api/users/:userID", function(req, res) {
  if (req.method === "POST") {
//   ^ This condition will always return 'false' since 
//     the types 'Methods' and '"POST"' have no overlap.(2367)
    res.status(20).send({
//             ^
//  Argument of type '20' is not assignable to parameter of
//  type 'StatusCode'.(2345)
      message: "Welcome, user " + req.params.userId 
//                                           ^
//    Property 'userId' does not exist on type 
//    '{ userID: string; }'. Did you mean 'userID'?
    });
  }
});
Open in TypeScript Playground →