npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ts-typed/express

v0.1.5

Published

Express integration for ts-typed runtime type errors

Readme

@ts-typed/express

Express integration for @ts-typed/core. It converts runtime contract violations into safe HTTP responses without adding schemas or decorators to your application.

Install

npm install @ts-typed/core @ts-typed/express

express remains your application's dependency. This package supports Express 4 and 5 on Node 18+.

Automatic setup

npx ts-typed init detects an Express project through its express dependency. It offers to register typedErrorHandler() in the conventional bootstrap file, placing it right before the app's .listen(...) call. That call must be the single, top-level .listen(...) invoked directly on an app created by express() in that file; if init cannot identify exactly one such call, it leaves the file untouched, and you can register the middleware by hand as shown below.

Register the middleware

Register typedErrorHandler() after your routes and before any fallback error handler:

import express from 'express';
import { typedErrorHandler } from '@ts-typed/express';
import { usersRouter } from './users.router';

const app = express();

app.use(express.json());
app.use('/users', usersRouter);

// Must be after routes: Express only enters error middleware after next(error)
// or a thrown error reaches the end of the route chain.
app.use(typedErrorHandler());

app.listen(3000);

The compiler instruments the TypeScript contract. The Express package only adapts the resulting StrictTypeError to HTTP; it does not replace Express routing, body parsing, or application validation.

What the response looks like

Given a runtime violation such as a string received where a number was declared:

{
  "statusCode": 400,
  "message": "CreateUserDto.age expected number, received string",
  "code": "TYPE_MISMATCH",
  "contractKind": "parameter",
  "propertyPath": "CreateUserDto.age",
  "expected": "number",
  "received": "string",
  "operation": "argument"
}

The response contains safe diagnostic metadata only. It never includes the rejected raw value or a stack trace.

Status mapping

  • 400 is used for client-facing parameter and DTO violations.
  • 500 is used for return/resolve violations, server variables, and fields marked with ServerPopulated().
  • A TypeErrorResponse() status takes precedence when the field explicitly defines one.
  • AggregateStrictTypeError includes its safe errors list in the same response.
  • Errors unrelated to ts-typed are passed to the next Express error handler.

Custom hooks

Use onError for safe logging and formatResponse when your API has a response envelope:

app.use(typedErrorHandler({
  onError(error, request) {
    logger.warn({ path: request.path, propertyPath: error.propertyPath });
  },
  formatResponse(body) {
    return { ok: false, error: body };
  },
}));

The formatter receives the already-sanitized body. The raw received value is never exposed by this package.

Manual conversion

For applications that need to handle the error themselves, use toTypedErrorResponse():

import { StrictTypeError } from '@ts-typed/core';
import { toTypedErrorResponse } from '@ts-typed/express';

try {
  await service.execute();
} catch (error) {
  if (error instanceof StrictTypeError) {
    return response.status(toTypedErrorResponse(error).statusCode).json(toTypedErrorResponse(error));
  }
  throw error;
}

Development

npm install
npm run typecheck
npm test
npm pack --dry-run

The main branch publishes automatically after CI passes when the repository has an NPM_TOKEN secret configured.

License

Apache-2.0