Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/garronej/tsafe

🔩 The missing TypeScript utils
https://github.com/garronej/tsafe

deno hacktoberfest node npm testing type-safety typescript utilities

Last synced: 21 days ago
JSON representation

🔩 The missing TypeScript utils

Awesome Lists containing this project

README

        





A collection of utilities to take your TypeScript development up a notch





















(you can cherrypick what you import)



Documentation

## A few GIFs to convince you that you need `tsafe` in your life




Assert things you know are true, get runtime errors where you were wrong:











Implement compile time unit testing










Playground







Make sure all properties of an object are deconstructed







Playground







Make sure you never forget a case in a switch







Playground






Make TypeScript believe whatever you say without having to write const obj2 = obj as Bar.


The more powerfully is to be able to tell TypeScript that obj ist not of type Bar:












Make sure your zod schema exactly matches a given type:



## Motivations

Powerful TypeScript features like [assertion functions](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions) or [user-defined type guards](https://www.typescriptlang.org/docs/handbook/advanced-types.html#user-defined-type-guards) are only useful if paired with utility functions.

TypeScript, however, only exports type helpers \(e.g. `Record`, `ReturnType`, etc.\).

This module provides _«the missing builtins»_ such as [the assert function](https://docs.tsafe.dev/assert)
and corrects frustrating aspects of default utility types such as [`ReturnType`](https://docs.tsafe.dev/returntype).

[**Documentation website**](https://docs.tsafe.dev)

## Installation

`tsafe` is both an [NPM](https://www.npmjs.com/package/tsafe) and a [Deno](https://deno.land/x/tsafe) module.
(Achieved with [denoify](https://github.com/garronej/denoify))

## Import in deno:

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

## Install elsewhere:

```bash
$ npm install --save tsafe
#OR
$ yarn add tsafe
```