Leverage the more advanced features of TypeScript
A collection of utilities that makes your TS code cleaner and safer

Home - Documentation

Powerful features like assertion functions or user-defined type guards are only useful if paired with utility functions. TypeScript, however, only exports type helpers (e.g. Record, RetunType, etc.). tsafe is here to export «the missing builtins» such as the assert function.

tsafe also tries to make TypeScript more practical by patching frustrating aspect of to the built-in types helper see RetunType for example.

This module is both an NPM module and a Deno module

Import in deno:

import { assert, typeGuard, ... } from "https://deno.land/x/tsafe/mod.ts";

Install elsewhere:

$ npm install --save tsafe
import { assert } from "tsafe/assert";
import { typeGuard } from "tsafe/typeGuard";

type Circle = { radius: number };
type Square = { sideLength: number };
type Shape = Circle | Square;

const shape: Shape = { "radius": 100 };

//You: Trust me TypeScript, I know that shape is a Circle.
assert(typeGuard<Circle>(shape));

//TypeScript: Ok if you say so...it must have a radius then.
shape.radius;

Learn more