Coder Social home page Coder Social logo

conditional-type-checks's Introduction

Conditional Type Checks

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 types to do these checks.

Type Checks

These types do the checks:

  • IsNullableType<T> - Checks if T is possibly null or undefined.
  • IsExactType<T, U> - Checks if T exactly matches U.
  • HasType<T, U> - Checks if T has U.
  • NotHasType<T, U> - Checks if T does not have U.
  • IsNeverType<T> - Checks if T is the never type.
  • More to come...

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

Ways to Test

Use either (whichever you prefer);

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

Use with AssertTrue and AssertFalse

Doing a test:

import { AssertTrue, AssertFalse, HasType, IsNeverType, IsNullableType } from "conditional-type-checks";

const result = someFunction(someArg);

type doTest = AssertTrue<HasType<typeof result, string> | IsNullableType<typeof result>>
    | AssertFalse<IsNeverType<typeof result>>;

Warning: Do not use an intersection type in the type parameter for AssertTrue and AssertFalse (ex. HasType<string | number, string> & IsNeverType<never>) because it will cause the assertion to pass if only one of the checks passes.

Use with assert

Doing a test:

import { assert, IsExactType, IsNullableType } from "conditional-type-checks";

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

Failure:

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

Install

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

conditional-type-checks's People

Contributors

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