expect-type
Compile-time tests for types. Useful to make sure types don't regress into being overly-permissive as changes go in over time.
Last updated 7 years ago by mmkal .
Apache-2.0 · Repository · Bugs · Original npm · Tarball · package.json
$ npm install expect-type 
SYNC missed versions from official npm registry.

expect-type

Compile-time tests for types. Useful to make sure types don't regress into being overly-permissive as changes go in over time.

expectTypeOf

Similar to Jest's expect, but with type-awareness. Gives you access to a number of type-matchers that let you make assertions about the form of a reference or generic type parameter.

Example
expectTypeOf({a: 1}).toMatchTypeOf({a: 2})
expectTypeOf({a: 1}).property('a').toBeNumber()

See the full docs for lots more examples.

Contents

Installation and usage

npm install expect-type
import {expectTypeOf} from 'expect-type'

Documentation

Type-check object references:

expectTypeOf({a: 1}).toEqualTypeOf({a: 1})
expectTypeOf({a: 1, b: 1}).toMatchTypeOf({a: 1})
expectTypeOf({a: 1}).toEqualTypeOf({a: 2})

Assertions can be inverted:

expectTypeOf({a: 1}).not.toMatchTypeOf({b: 1})

Catch any/unknown/never types:

expectTypeOf<unknown>().toBeUnknown()
expectTypeOf<any>().toBeAny()
expectTypeOf<never>().toBeNever()

Test for basic javascript types:

expectTypeOf(() => 1).toBeFunction()
expectTypeOf({}).toBeObject()
expectTypeOf([]).toBeArray()
expectTypeOf('').toBeString()
expectTypeOf(1).toBeNumber()
expectTypeOf(true).toBeBoolean()
expectTypeOf(Promise.resolve(123)).resolves.toBeNumber()
expectTypeOf(Symbol(1)).toBeSymbol()

Nullable types:

expectTypeOf(undefined).toBeUndefined()
expectTypeOf(undefined).toBeNullable()
expectTypeOf(undefined).not.toBeNull()

expectTypeOf(null).toBeNull()
expectTypeOf(null).toBeNullable()
expectTypeOf(null).not.toBeUndefined()

expectTypeOf<1 | undefined>().toBeNullable()
expectTypeOf<1 | null>().toBeNullable()
expectTypeOf<1 | undefined | null>().toBeNullable()

Assertions can be inverted with .not:

expectTypeOf(1).not.toBeUnknown()
expectTypeOf(1).not.toBeAny()
expectTypeOf(1).not.toBeNever()
expectTypeOf(1).not.toBeNull()
expectTypeOf(1).not.toBeUndefined()
expectTypeOf(1).not.toBeNullable()

Make assertions about object properties:

const obj = {a: 1, b: ''}

expectTypeOf(obj)
  .property('a')
  .toEqualTypeOf(1)
expectTypeOf(obj)
  .property('b')
  .toEqualTypeOf<string>()

Assert on function parameters (using .parameter(n) or .parameters) and return values (using .return):

const f = (a: number) => [a, a]

expectTypeOf(f).toBeFunction()

expectTypeOf(f).toBeCallableWith(1)
expectTypeOf(f).not.toBeAny()
expectTypeOf(f).returns.not.toBeAny()
expectTypeOf(f).returns.toEqualTypeOf([1, 2])
expectTypeOf(f).returns.toEqualTypeOf([1, 2, 3])
expectTypeOf(f)
  .parameter(0)
  .not.toEqualTypeOf('1')
expectTypeOf(f)
  .parameter(0)
  .toEqualTypeOf(1)
expectTypeOf(1)
  .parameter(0)
  .toBeNever()

const twoArgFunc = (a: number, b: string) => ({a, b})

expectTypeOf(twoArgFunc).parameters.toEqualTypeOf<[number, string]>()

Promise resolution types can be checked with .resolves:

const asyncFunc = async () => 123

expectTypeOf(asyncFunc).returns.resolves.toBeNumber()

Array items can be checked with .items:

expectTypeOf([1, 2, 3]).items.toBeNumber()
expectTypeOf([1, 2, 3]).items.not.toBeString()

Check that functions never return:

const thrower = () => {
  throw Error()
}

expectTypeOf(thrower).returns.toBeNever()

Generics can be used rather than references:

expectTypeOf<{a: number; b?: number}>().not.toEqualTypeOf<{a: number}>()
expectTypeOf<{a: number; b?: number | null}>().not.toEqualTypeOf<{a: number; b?: number}>()
expectTypeOf<{a: number; b?: number | null}>().toEqualTypeOf<{a: number; b?: number | null}>()

Similar projects

Other projects with similar goals:

  • ts-expect exports several generic helper types to perform type assertions
  • dtslint does type checks via comment directives and tslint
  • 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 type assertion functions

Current Tags

  • 1.4.0                                ...           latest (5 months ago)
  • 0.4.1-master-2020-03-06-00-05-12.11                                ...           master (7 years ago)
  • 1.0.0-rc.0                                ...           next (2 years ago)

63 Versions

  • 1.4.0                                ...           5 months ago
  • 1.3.0                                ...           a year ago
  • 1.2.2                                ...           a year ago
  • 1.2.1                                ...           2 years ago
  • 1.2.0                                ...           2 years ago
  • 1.1.0                                ...           2 years ago
  • 1.0.0                                ...           2 years ago
  • 1.0.0-rc.0                                ...           2 years ago
  • 0.20.0                                ...           2 years ago
  • 0.20.0-0                                ...           2 years ago
  • 0.19.0                                ...           3 years ago
  • 0.18.0                                ...           3 years ago
  • 0.17.3                                ...           3 years ago
  • 0.17.3-0                                ...           3 years ago
  • 0.17.2                                ...           3 years ago
  • 0.17.1                                ...           3 years ago
  • 0.17.0                                ...           3 years ago
  • 0.17.0-2                                ...           3 years ago
  • 0.17.0-1                                ...           3 years ago
  • 0.16.0                                ...           4 years ago
  • 0.15.0                                ...           4 years ago
  • 0.14.2                                ...           4 years ago
  • 0.14.1                                ...           4 years ago
  • 0.14.0                                ...           4 years ago
  • 0.14.0-0                                ...           4 years ago
  • 0.13.0                                ...           5 years ago
  • 0.12.0                                ...           5 years ago
  • 0.11.0                                ...           6 years ago
  • 0.10.0                                ...           6 years ago
  • 0.9.2                                ...           6 years ago
  • 0.9.1                                ...           6 years ago
  • 0.9.0                                ...           6 years ago
  • 0.8.0                                ...           6 years ago
  • 0.7.11                                ...           6 years ago
  • 0.7.10                                ...           6 years ago
  • 0.7.9                                ...           6 years ago
  • 0.7.8                                ...           6 years ago
  • 0.7.7                                ...           6 years ago
  • 0.7.6                                ...           6 years ago
  • 0.7.5                                ...           6 years ago
  • 0.7.4                                ...           7 years ago
  • 0.7.3                                ...           7 years ago
  • 0.7.2                                ...           7 years ago
  • 0.7.1                                ...           7 years ago
  • 0.7.0                                ...           7 years ago
  • 0.6.0                                ...           7 years ago
  • 0.5.5                                ...           7 years ago
  • 0.5.4                                ...           7 years ago
  • 0.5.3                                ...           7 years ago
  • 0.5.2                                ...           7 years ago
  • 0.5.1                                ...           7 years ago
  • 0.5.0                                ...           7 years ago
  • 0.4.6                                ...           7 years ago
  • 0.4.5                                ...           7 years ago
  • 0.4.4                                ...           7 years ago
  • 0.4.3                                ...           7 years ago
  • 0.4.2                                ...           7 years ago
  • 0.4.1                                ...           7 years ago
  • 0.4.1-master-2020-03-06-00-05-12.11                                ...           7 years ago
  • 0.4.0                                ...           7 years ago
  • 0.3.1                                ...           7 years ago
  • 0.3.2-master-2020-03-02-22-57-03.79                                ...           7 years ago
  • 0.3.2-master-2020-03-02-22-01-34.76                                ...           7 years ago
Maintainers (1)
Downloads
Total 201
Today 0
This Week 0
This Month 0
Last Day 0
Last Week 3
Last Month 145
Dependencies (2)
Dev Dependencies (0)
None
Dependents (2)

© 2010 - cnpmjs.org x YWFE | Home | YWFE