Git Product home page Git Product logo

typescript-type-checker's Introduction

typescript-type-checker

typescript-type-checker is Runtime Type Checker for Typescript.

It generates runtime type checking sanitizer script for Typescript.

typescript-type-checker supports all default types, arrays, and custom types.

How to install

npm install typescript-type-checker

How to use

You can use typescript-type-checker both commandline and script.

Script usage

import { buildScript } from "typescript-type-checker";

const srcDir = "./src/lib";
const outputPath = "./out.ts";
const strictMode = false;

buildScript(srcDir, outputPath, strictMode);

Commandline usage

args:

  • --src: Input directory which contains .ts files
  • --out: Output directory which sanitizer script will be saved to
  • --strict: Whether use strict mode. In strict mode, script does not check string value can be converted to number or boolean

example:
$ npx typescript-type-checker --src "./src/lib" --out "./out/sanitizer.ts" --strict

Then you can get sanitizer script at "./out/sanitizer.ts".

Example

./src/lib/example.ts

export interface Foo {
    number: number;
    boolean: boolean;
    maybeString?: string;
    bar: Bar;
}

interface Bar {
    numbers: number[];
}

With strict mode

function sanitizeFoo(checker: any) {
    if (
        typeof checker.number != "number" ||
        typeof checker.boolean != "boolean" ||
        (checker.maybeString != undefined &&
            typeof checker.maybeString != "string") ||
        !sanitizeBar(checker.bar)
    ) {
        return false;
    }
    return true;
}

function sanitizeBar(checker: any) {
    if (!sanitizenumberArray(checker.numbers)) {
        return false;
    }
    return true;
}

function sanitizenumberArray(checker: any) {
    if (!Array.isArray(checker)) {
        return false;
    }

    for (let i = 0; i < checker.length; i++) {
        if (typeof checker[i] != "number") {
            return false;
        }
    }
    return true;
}

Without strict mode

function sanitizeFoo(checker: any) {
    if (
        typeof checker.number == "string" &&
        Boolean(checker.number.trim()) &&
        !Number.isNaN(Number(checker.number))
    ) {
        checker.number = Number(checker.number);
    }

    if (
        typeof checker.boolean == "string" &&
        (checker.boolean == "true" || checker.boolean == "false")
    ) {
        checker.boolean = checker.boolean == "true";
    }

    if (
        typeof checker.number != "number" ||
        typeof checker.boolean != "boolean" ||
        (checker.maybeString != undefined &&
            typeof checker.maybeString != "string") ||
        !sanitizeBar(checker.bar)
    ) {
        return false;
    }
    return true;
}

function sanitizeBar(checker: any) {
    if (!sanitizenumberArray(checker.numbers)) {
        return false;
    }
    return true;
}

function sanitizenumberArray(checker: any) {
    if (!Array.isArray(checker)) {
        return false;
    }

    for (let i = 0; i < checker.length; i++) {
        if (typeof checker[i] != "number") {
            return false;
        }
    }
    return true;
}

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.