• Stars
    star
    193
  • Rank 193,502 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created about 5 years ago
  • Updated 4 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Checks TypeScript types match expected values

TS Expect

NPM version NPM downloads Build status Test coverage

Checks values in TypeScript match expectations.

Installation

npm install ts-expect --save

Usage

TS Expect exports a function, named expectType, that does nothing at all. Instead, it depends on the TypeScript compiler and a generic to test the type of a "value" passed to expectType is assignable to its generic in the type system.

import { expectType } from "ts-expect";

expectType<string>("test");
expectType<number>(123);
expectType<number>("test"); // Compiler error!

How does this work?

TypeScript generics allow you to pass any value that implements the generic type. In this case, we're defining the generic explicitly as we pass the value so any value that isn't implementing our type is rejected by the TypeScript compiler. It's really that simple! The technical implementation is just <T>(value: T) => void.

TypeScript has a "top type" named unknown and a "bottom type" named never. Using the top type to check assignability would mean every value is accepted, and the bottom type would mean nothing is accepted (except never itself). As a result, you probably wouldn't want to use unknown because everything would pass that check.

A quick note on any: it's an "off switch" for TypeScript. It acts as a magical every type, both a top and a bottom type. This means it's assignable to everything and passing an any value to expectType will always pass the check.

Testing definitions

Use with built-in or custom TypeScript utility types to implement a simple testing framework for your type definitions. If it compiles, it's valid!

import { expectType, TypeEqual } from "ts-expect";
import { add } from "./adder";

expectType<number>(add(1, 2));
expectType<TypeEqual<number, ReturnType<typeof add>>>(true);
expectType<TypeEqual<[number, number], Parameters<typeof add>>>(true);

Exhaustive checks

Use with TypeScript's type narrowing to test that value is what you expect. If you expand SupportedValue with other values in the future, it'll fail an expectType<never> or expectNever check because you haven't used all the possible values.

import { expectNever } from "ts-expect";

type SupportedValue = "a" | "b";

function doSomething(value: SupportedValue) {
  switch (value) {
    case "a":
      return true;
    case "b":
      return true;
    default:
      return expectNever(value);
  }
}

Tip: Use expectNever(value) when you need to return never (i.e. throw an error if the code runs), use expectType<never>(value) when you want to do tests in your code and expect the actual expression to be executed (i.e. do type checks but ignore the runtime).

Exported Types

TS Expect comes with some utility types built-in to make testing easier. File an issue if you think something is missing!

TypeEqual<Target, Value>

Checks that Value is equal to the same type as Target. This is a stricter check that avoids issues with testing sub-types. If you want to verify that an object is identical shape, not just "implements" Target, this is the type you need.

TypeOf<Target, Value>

Checks that Value is assignable to Target. This is effectively the same as expectType<Type>(value), except it's implemented in the type system directly so you can use it to test types instead of values by checking the result is true or false.

Prior Works

Some great prior works have been mentioned after publishing this package:

  • dtslint does type checks via comment directives and inspired this approach of using the compiler
  • tsd-check is a CLI that runs the TypeScript type checker over assertions
  • type-plus comes with various type and runtime TypeScript assertions
  • static-type-assert exposes a similar API surface with some type assertion functions

License

MIT

More Repositories

1

ts-node

TypeScript execution and REPL for node.js
TypeScript
12,395
star
2

typedoc

Documentation generator for TypeScript projects.
TypeScript
7,258
star
3

ts-loader

TypeScript loader for webpack
TypeScript
3,413
star
4

fork-ts-checker-webpack-plugin

Webpack plugin that runs typescript type checker on a separate process.
TypeScript
1,916
star
5

atom-typescript

The only TypeScript package you will ever need
TypeScript
1,129
star
6

learn-typescript

The complete workshop for picking up TypeScript
TypeScript
679
star
7

tsify

Browserify plugin for compiling TypeScript
JavaScript
339
star
8

grunt-ts

A grunt task to manage your complete typescript development to production workflow
JavaScript
326
star
9

dts-bundle

Export TypeScript .d.ts files as an external module definition
TypeScript
307
star
10

ntypescript

Nicer TypeScript for API devs
JavaScript
115
star
11

tsconfig

Resolve and parse `tsconfig.json`, replicating TypeScript's behaviour
TypeScript
102
star
12

typedoc-default-themes

Handlebars
56
star
13

typedoc-site

Website for TypeDoc
TypeScript
37
star
14

grunt-typedoc

Grunt plugin to generate TypeScript docs with TypeDoc
JavaScript
16
star
15

tspms

An abstraction on top TypeScript language service, that let you consume it in the context of a project.
TypeScript
13
star
16

typedoc-auto-docs

An idea from TS Discord to automatically render docs for the ecosystem, similar to docs.rs and doc.deno.land
TypeScript
10
star
17

typescript-compiler-docs

Community docs about typescript's compiler and APIs
9
star
18

atom-typescript-examples

Temporary location of examples for mutual testing
JavaScript
8
star
19

tscs

TypeScript Compiler Services
JavaScript
7
star
20

ts-node-repros

Dockerfile
6
star
21

grunt-ts-clean

Grunt plugin to cleanup TypeScript builds for release.
JavaScript
6
star
22

typedoc-repros

Shell
5
star
23

csproj2ts

Library to parse TypeScript config info from a Visual Studio Project file
TypeScript
5
star
24

fs-fixture-builder

TypeScript
5
star
25

typedoc-action

TypeScript
5
star
26

tsproj

A specification for a file format + Parser Implementation for specifying TypeScript projects
TypeScript
5
star
27

grunt-dts-bundle

Grunt plugin to export TypeScript .d.ts files as an external module definition
JavaScript
4
star
28

typestrong.github.io

Website
HTML
4
star
29

typestrong-compiler

experimental compiler module
JavaScript
3
star
30

discussions

Open organization discussions
3
star
31

ts-node-examples

TypeScript
3
star