Giter Club home page Giter Club logo

conditional-type-checks's Introduction

Conditional Type Checks

npm version CI deno doc stable

As TypeScript's type system becomes more complex it's useful to be able to write tests for what a type should be.

This library offers reusable conditional types to do these checks.

Type Checks

These will resolve to the type true when they match and false otherwise.

  • IsNullable<T> - Checks if T is possibly null or undefined.
  • IsExact<T, U> - Checks if T exactly matches U.
  • Has<T, U> - Checks if T has U.
  • NotHas<T, U> - Checks if T does not have U.
  • IsAny<T> - Checks if T is the any type.
  • IsNever<T> - Checks if T is the never type.
  • IsUnknown<T> - Checks if T is the unknown type.
  • More to come...

Ways to Test

Use what you prefer:

  1. The AssertTrue, AssertFalse, or Assert types.
  2. The assert function.

Use with AssertTrue, AssertFalse, and Assert

Doing a test:

import {
  AssertFalse,
  AssertTrue,
  Has,
  IsNever,
  IsNullable,
} from "https://deno.land/x/conditional_type_checks/mod.ts";

const result = someFunction(someArg);

type doTest =
  | AssertTrue<Has<typeof result, string> | IsNullable<typeof result>>
  | AssertFalse<IsNever<typeof result>>
  | Assert<Has<typeof result, number>, true>;

Warning: Do not use an intersection type between checks (ex. Has<string | number, string> & IsNever<never>) because it will cause everything to pass if only one of the checks passes.

Use with assert

Doing a test:

import {
  assert,
  IsExact,
} from "https://deno.land/x/conditional_type_checks/mod.ts";

const result = someFunction(someArg);

// compile error if the type of `result` is not exactly `string | number`
assert<IsExact<typeof result, string | number>>(true);

Failure:

// causes a compile error that `true` is not assignable to `false`
assert<IsNullable<string>>(true); // string is not nullable

NPM Install

npm install --save-dev conditional-type-checks

conditional-type-checks's People

Contributors

dsherret avatar rohit-gohri avatar smmoosavi avatar

Stargazers

 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.