★ wanayoo — archive 1999 https://github.com/TypeStrong/ts-expectNouvelle recherche | Portail wanayoo
Skip to content
Please note that GitHub no longer supports Internet Explorer.

We recommend upgrading to the latest Microsoft Edge, Google Chrome, or Firefox.

Learn more
Checks TypeScript types match expected values
Branch: master
Clone or download
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
src Add TypeScript transform to strip usages Mar 17, 2019
.editorconfig . Mar 15, 2019
.gitignore . Mar 15, 2019
.travis.yml . Mar 15, 2019
LICENSE . Mar 15, 2019
README.md Mention `dtslint` which inspired this indirectly Mar 16, 2019
package-lock.json Update `package-lock.json` with fixed Jest Mar 15, 2019
package.json Update `package-lock.json` with fixed Jest Mar 15, 2019
tsconfig.json Add TypeScript transform to strip usages Mar 17, 2019
tslint.json . Mar 15, 2019

README.md

TS Expect

NPM version NPM downloads Build status Test coverage

Checks TypeScript types match expected values.

Why? This is useful for designing type assertions against the TypeScript compiler. It allows you to catch a subset of issues in the compiler before runtime.

Installation

npm install ts-expect --save

Usage

import { expectType, TypeOf } from "ts-expect";

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

Examples

export Type = "foo" | "bar";

function validateType(type: string): type is Type {
  if (type === "foo") {
    expectType<TypeOf<typeof type, Type>>(true); // Compiler error! Forgot `type === "bar"`.
    return true;
  }

  return false;
}

function doSomething(type: Type) {
  if (type === "foo") return; // Do something with `foo`.

  expectType<never>(type); // Compiler error! Forgot `type === "bar"`.

  throw new TypeError(`Unknown type: ${type}`)
}

// Test function signatures.
expectType<TypeOf<[number], Parameters<typeof validateType>>>(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

You can’t perform that action at this time.
You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session.