Skip to content

Instantly share code, notes, and snippets.

Created Aug 24, 2019
What would you like to do?
Express handler type constraints.
import { Request, NextFunction } from 'express'
interface JsonResponse<T> {
status(code: number): JsonResponse<T>
json(value: T): JsonResponse<T>
type JsonHandler<T> = (
req: Request,
res: JsonResponse<T>,
next: NextFunction
) => void
export const handleFoo: JsonHandler<Foo> = async (req, res, next) => {
const foo = await getFoo()
res.json(foo) // Error if `foo` is not of type Foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment