conditional-type-checks
Types for testing TypeScript types.
Last updated 7 months ago by dsherret .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install conditional-type-checks 
SYNC missed versions from official npm registry.

Conditional Type Checks

npm version 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 { AssertTrue, AssertFalse, Has, IsNever, IsNullable } from "conditional-type-checks";

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 "conditional-type-checks";

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

Install

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

Current Tags

  • 1.0.5                                ...           latest (7 months ago)

15 Versions

  • 1.0.5                                ...           7 months ago
  • 1.0.4                                ...           8 months ago
  • 1.0.3                                ...           8 months ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 0.7.0                                ...           a year ago
  • 0.6.0                                ...           a year ago
  • 0.5.0                                ...           2 years ago
  • 0.4.0                                ...           2 years ago
  • 0.3.0                                ...           2 years ago
  • 0.2.0                                ...           2 years ago
  • 0.1.4                                ...           2 years ago
  • 0.1.2                                ...           2 years ago
  • 0.1.1                                ...           2 years ago
  • 0.1.0                                ...           2 years ago
Maintainers (1)
Downloads
Today 0
This Week 1
This Month 2
Last Day 0
Last Week 2
Last Month 27
Dependencies (0)
None
Dev Dependencies (1)
Dependents (1)

Copyright 2014 - 2016 © taobao.org |