/express-typesafe

Primary LanguageTypeScriptMIT LicenseMIT

express-typesafe

This repository contains some personal experiments with type-level programming in Typescript, mostly making use of ts-toolbelt. The end goal, which may or may not be ever reached, is to build an abstraction layer over Express to build fully type-safe REST APIs, largely inspired by Haskell's Servant.

Ideally, we would write something like this:

interface User {
  id: number
  name: string
}

const getUser: Endpoint<["api", "users", Capture<"id">, Handler<GET, [Response<200, User>, Response<404, string>]>>
// This type evaluates to (string) => [200, User] | [404, string]
  = (id: string) => {
    if (+id === 1) {
      return [200, { id: 1, name: 'Test user' }];
    } else {
      return [404, 'User not found'];
    }
  }

and have this generate the following code for Express:

app.get('/api/users/:id', (req, res) => {
  const [code, res] = getUser(req.params.id);
  res.status(code).json(res);
});