https://github.com/iansan5653/assertion-types
Test your types using just the TypeScript compiler and no external tools.
https://github.com/iansan5653/assertion-types
linting testing types typescript utility-types
Last synced: 11 months ago
JSON representation
Test your types using just the TypeScript compiler and no external tools.
- Host: GitHub
- URL: https://github.com/iansan5653/assertion-types
- Owner: iansan5653
- License: mit
- Created: 2020-06-01T19:03:53.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-07-18T22:22:20.000Z (almost 3 years ago)
- Last Synced: 2025-06-24T08:03:35.964Z (about 1 year ago)
- Topics: linting, testing, types, typescript, utility-types
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/assertion-types
- Size: 146 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: changelog.md
- License: license.md
- Security: security.md
Awesome Lists containing this project
README
#### 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:
```ts
import {Assert, Equals, Extends, NotEquals, NotExtends} from "assertion-types";
// Equals:
type EqualsExample = Assert>;
// NotEquals:
type NotEqualsExample = Assert>;
// Extends:
type ExtendsExample = Assert>;
// NotExtends:
type NotExtendsExample = Assert>;
```
## Getting Started
Install via NPM:
```sh
npm install --save-dev assertion-types
```
After that, just import into your test files:
```ts
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`.