Giter VIP home page Giter VIP logo

as-safely's Introduction

as-safely

A library for safe type assertion in TypeScript.

The function asSafely throws an exception if it fails to check the type at runtime. This function will also perform type checking when transpiling to JavaScript whenever possible.

With this library, you can eliminate dangerous Type Assertions in your project.

Install

npm install as-safely

Quickstart

import { asSafely, isString, isArray, isNumber } from './as-safely';

const str1: string = asSafely('1' as unknown, isString);
// => OK

const strOrUndefined: string | undefined = asSafely(undefined as unknown, [isString, isUndefined]);
// => OK

const numberArray: number[] = asSafely([1, 2] as unknown, isArray(isNumber));
// => OK

const str2: string = asSafely(1, isString);
// => detects a transpile error.

const str3: string = asSafely(1 as unknown, isString);
// => throws a runtime error.

const str4: string = asSafely<string>(1, isString);
// => forces to predict an object as string. This will throw a runtime error.

const strOrUndefined2: string | undefined = asSafely(1 as unknown, isString, () => undefined);
// => not throws a runtime error and returns undefined.

const strWithCustomError: string = asSafely(1 as unknown, isString, (obj) => {
    console.error(obj);
    throw new Error('custom error') 
});
// => throws a custom error.

as-safely's People

Contributors

srkw avatar yuitosato avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

srkw

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.