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 🙏

© 2024 – Pkg Stats / Ryan Hefner

typer-ts

v0.1.3

Published

Typer can perform both TypeScript type checking and runtime type validation.

Downloads

7

Readme

Typer

Typer can perform both TypeScript type checking and runtime type validation.

Example

Let's assume we have the following code.

interface Children{
    name: string;
    age: number;
    gender: 'male' | 'female' | 'other';
}

interface ResponseData{
    name: string;
    age: number;
    gender: 'male' | 'female' | 'other';
    children: Children[];
}

const responseData: ResponseData = await response.json(); //It does not check if the actual type is correct.

To use this in Typer, you would write it as follows.

import typer, {type TyperToType } from 'typer';

const nameTyper = new typer.String(null);// Using `null` in constructor argument will only check if the value is `string`
const ageTyper = new typer.Number(null);
const genderTyper = new typer.String(['male', 'female', 'other'] as const);// You must use `as const` to check if the value is in specific values

const childrenTyper = new typer.Object({
    name: nameTyper,
    age: ageTyper,
    gender: genderTyper
});
const responseDataTyper = new typer.Object({
    name: nameTyper,
    age: ageTyper,
    gender: genderTyper,
    children: new typer.Array([childrenTyper] as const)
})

const responseData: TyperToType<typeof responseDataTyper> = await response.json(); //Typescript type checking.

if(!responseDataTyper.check(responseData)){// Runtime type checking.
    console.log('Runtime Type Error.')
}

Typer Classes

class typer.String

const stringTyper = new typer.String(null);// Use null to check if the value is string.
const a_or_b_Typer = new typer.String(["a", "b"] as const);// Check if the value is specific value.

stringTyper.check("hi"); //true
a_or_b_Typer.check("a"); //true
a_or_b_Typer.check("as"); //false

class typer.Number

const numberTyper = new typer.Number(null);// Use null to check if the value is number.
const one_or_two = new typer.Number([1, 2] as const);// Check if the value is specific value.

numberTyper.check(283); //true
one_or_two.check(1); //true
one_or_two.check(12); //false

class typer.Boolean

const booleanTyper = new typer.Boolean(null);// Use null to check if the value is boolean.
const trueTyper = new typer.Boolean([true] as const);// Check if the value is specific value.

booleanTyper.check(false); //true
trueTyper.check(true); //true
trueTyper.check(false); //false

class typer.BigInt

const bigintTyper = new typer.BigInt(null);// Use null to check if the value is bigint.
const specificTyper = new typer.BigInt([1n, 10n, 192939192n] as const);// Check if the value is specific value.

bigintTyper.check(2n); //true
specificTyper.check(192939192n); //true
specificTyper.check(3n); //false

class typer.Null

const nullTyper = new typer.Null();

nullTyper.check(null)//true
nullTyper.check(false)//false

class typer.Undefined

const undefinedTyper = new typer.Undefined();

undefinedTyper.check(undefined); //true
undefinedTyper.check(void(1));//true
undefinedTyper.check(false);//false

class typer.Array

const arrayTyper = new typer.Array(null);//Use null to check if the value is an array.
const specificTyper = new typer.Array([
    new typer.String(["asd", "qwe"] as const),
    new typer.Number(null)
] as const)

arrayTyper.check(["kasndksa"]); //true
specificTyper.check(["qwe", "asd", 123, 1232, "qwe"]); //true
specificTyper.check(["asd", 123, 1232, "qwes"]); //false

class typer.Union

const unionTyper = new typer.Union([
    new typer.String(["asd", "qwe"] as const),
    new typer.Number(null)
] as const);

unionTyper.check("qwe"); //true
unionTyper.check(12312313); //true
unionTyper.check(true); //false

class typer.Object

const objectTyper = new typer.Object({
    name: new typer.String(null),
    age: new typer.Number(null),
    gender: new typer.String(["male", "female"] as const);
});

objectTyper.check({
    name: 'kim',
    age: 10,
    gender: "male"
}) //true
objectTyper.check({
    name: 'kim',
    age: 10,
    gender: "hahaha"
}) //true
objectTyper.check({
    name: 'kim',
    age: 10,
    gender: "male",
    option: false
}) //false
objectTyper.check({
    name: 'kim',
    gender: "male"
}) //false

type TyperToType

This type will change Typer Instance to Typescript type.

const objectTyper = new typer.Object({
    name: new typer.String(null),
    age: new typer.Number(null),
    gender: new typer.String(["male", "female"] as const);
});

type ExampleObject = TyperToTyper<typeof objectTyper>;//You must use `typeof`

/**
 * ExampleObject will be
 * {
 *  name: string;
 *  age: number;
 *  gender: 'male' | 'female';
 * } 
 */
const obj: ExampleObject = {
    name: 'kim',
    age: 20,
    gender: 'male'
}