Coder Social home page Coder Social 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

yuitosato avatar srkw avatar

Stargazers

Masaharu Komuro avatar nabe avatar Yoshi avatar  avatar だいすけ avatar hiro08 avatar shirow ozawa avatar  avatar dev-platong avatar masa@stand-io avatar Taiga avatar Kodai Kabasawa avatar yamaimo avatar shogogg avatar  avatar Naoya Shimizu(Shimmy) avatar Tokiya Horikawa avatar Karibash avatar nomuon avatar Eduardo avatar akabeko avatar  avatar Shohei Ueda avatar  avatar YAGITA Yugo avatar Neos21 avatar Suguru Ohki avatar yona avatar  avatar Roman Hossain Shaon avatar syuilo avatar Kuma Taro avatar Yoshihiro Fujimoto avatar Okuto Oyama avatar Tetsuro Higuchi avatar mast1ff avatar りすりす/TwoSquirrels avatar しまぶー avatar Shuhei Akutagawa avatar Yuki Yoshida avatar ryuta sakamoto avatar

Watchers

James Cloos 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.