Socket
Socket
Sign inDemoInstall

conditional-type-checks

Package Overview
Dependencies
0
Maintainers
1
Versions
16
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    conditional-type-checks

Types for testing TypeScript types.


Version published
Maintainers
1
Install size
7.44 kB
Created

Readme

Source

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 help test your types.

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 type {
  AssertFalse,
  AssertTrue,
  Has,
  IsNever,
  IsNullable,
} from "https://deno.land/x/conditional_type_checks/mod.ts";

const result = someFunction(someArg);

type _test =
  | 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

Keywords

FAQs

Last updated on 18 Jun 2022

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc