Leverage the more advanced features of TypeScript

Home - Documentation

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;