Git Product home page Git Product logo

typerun's Introduction

TypeRun

Types, at runtime.

NPM Version Test coverage: 100% Size, minified and gzipped No dependencies Tree shaking: supported

TypeRun is a fast, lightweight, DX-focused, fully tested, dependency-free validation library that works everywhere JS can run. It allows you to define data schemas, then validate these against unknown data, and infer TypeScript types from them.

import { is } from "typerun";
import { object, number, string, array, Infer } from "typerun/schema";

const postSchema = object({
  id: number,
  title: string,
  tags: array(string),
});

type Post = Infer<typeof postSchema>;

const post = {
  id: 1,
  title: "Hello, world!",
  tags: ["dev", "blog"],
};

function getPostTitle(post: Post) {
  return post.title;
}

if (is(postSchema)(post)) {
  console.log(getPostTitle(post)); // "Hello, world!"
}

Get started

Install TypeRun from npm using your project’s package manager.

pnpm add typerun
npm install typerun
bun install typerun
yarn add typerun
deno install npm:typerun

Then, define a schema (= a type) for a resource, using TypeRun’s straightforward syntax.

import { object, number, string, optional, either, value } from "typerun/schema";

const userSchema = object({
  id: number,
  email: string,
  name: optional(string),
  role: either(value("admin"), value("user")),
});

Schemas can be the single source of truth for your data types, using the Infer utility type:

import { Infer } from "typerun/schema";

type User = Infer<typeof userSchema>;
/*    ^? type User = {
           id: number;
           email: string;
           name: string | undefined;
           role: "admin" | "user";
         }
*/

You can then validate some unknown data to be of the correct type, in a safe, non-throwing way:

import { validate } from "typerun";
import { isOk } from "typerun/result";

const unknownData: unknown = await fetchUser(42);
const userResult = validate(userSchema)(unknownData);

if (isOk(userResult)) {
  console.log(userResult.email);
} else {
  console.error(userResult.errors);
}

You can also do it with a throwing function:

import { validate } from "typerun";

const unknownData: unknown = await fetchUser(42);
try {
  const user = validate(userSchema, { throwing: true })(unknownData);
  console.log(user.email);
} catch (errors) {
  console.error(errors);
}

Exported entrypoints

The typerun package exports 4 entrypoints, namely:

typerun's People

Contributors

symsmith avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.