Giter Site home page Giter Site logo

tsdotnet / reflection Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 585 KB

A set of classes and utilities for JavaScript type inspection and validation.

License: MIT License

TypeScript 96.86% JavaScript 3.14%
reflection type-assertion type-inspection runtime-type runtime-typechecking

reflection's Introduction

alt text tsdotnet / reflection

GitHub license npm-publish npm version

A set of classes and utilities for JavaScript type inspection and validation.

Docs

tsdotnet.github.io/reflection

TypeValidator<T>

Benefits

  • Allows for run-time validation and checking of dynamic types as well as integrated type-guarding.
  • Super easy to use, basically only requires a copy paste.
  • Works with literals!

Usage

Step 1: Declare the expected type/interface.

interface MyType
{
    a: object;
    b: string;
    c: number;
    d: boolean;
    e: {
        f: string;
        g: boolean;
        h: [
            number,
            boolean,
            string
        ];
    };
}

Step 2: Copy the interface as an actual object and <type> the validator

The following can be done with pure JavaScript and still work.

const myTypeValidator = new TypeValidator<MyType>(
{
    a: Object,
    b: String,
    c: Number,
    d: Boolean,
    e: {
        f: String,
        g: Boolean,
        h: [
            Number,
            Boolean,
            String
        ]
    }
});

Step 3: validate as many times as you want:

const myItem = {
    a: {},
    b: 'hello',
    c: 1,
    d: true,
    e: {
        f: 'whatever',
        g: false,
        h: [
            0,
            true,
            '2'
        ]
    },
    i: 'noise'
};

// no compile-time type errors!
if (MyTypeValidator.isSubsetOf(myItem)) {
    console.log(myItem.e.h.length); // 3
    console.log(myItem.b); // "hello"
} else {
    throw new TypeError('Invalid type!');
}

reflection's People

Contributors

dependabot[bot] avatar electricessence avatar

Stargazers

 avatar  avatar

Watchers

 avatar  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.