Git Product home page Git Product logo

assertion-types's Introduction

assertion-types

assertion-types is a tiny, fast, and, easy-to-learn type-testing library that requires no extra build/test setup.

Introduction

The assertion-types library provides four simple tests for inspecting types: Equals, Extends, NotEquals, and NotExtends. Along with the included Assert type, these utilities allow you to test that the results of your types are what you expect them to be. Because this method requires no other tools besides the TypeScript compiler, errors will show up right in your editor and you won't be able to build your project if your tests fail.

Usage is simple:

import {Assert, Equals, Extends, NotEquals, NotExtends} from "assertion-types";

// Equals:
type EqualsExample = Assert<Equals<123, 123>>;

// NotEquals:
type NotEqualsExample = Assert<NotEquals<123, 456>>;

// Extends:
type ExtendsExample = Assert<Extends<123, number>>;

// NotExtends:
type NotExtendsExample = Assert<NotExtends<number, 123>>;

Getting Started

Install via NPM:

npm install --save-dev assertion-types

After that, just import into your test files:

import * as assertionTypes from "assertion-types";
// OR
import {Assert, Equals, Extends, NotEquals, NotExtends} from "assertion-types";

No need to run any commands to run the tests - if they error, they will error when the TypeScript compiler runs.

Purpose

Any sufficiently complex TypeScript project will eventually require the development of utility types, which are often left untested because unit tests cannot be written for types. However, testing these types ensures that your type system is sound and therefore you can rely on type errors to prevent the cases that you assume cannot happen based on your understanding of your types.

Comparison With Alternatives

The standard alternative for this is dtslint, which uses $ExpectType comments to test types with an external tool. However, this requires an additional testing step rather than just taking advantage of the compiler's abilities. A failed $ExpectType will only error when someone runs the dtslint command, while an error with these utilities will never make it past tsc.

assertion-types's People

Contributors

dependabot[bot] avatar iansan5653 avatar

Stargazers

 avatar  avatar  avatar

Watchers

 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.