What is expect-type?
The expect-type npm package is a TypeScript utility that allows developers to assert and validate types at compile time. It is particularly useful for ensuring that types are as expected during development, which can help catch type-related errors early.
What are expect-type's main functionalities?
Type Assertion
This feature allows you to assert that a given value matches a specific type. In this example, the `expectType` function is used to assert that the `user` object conforms to the `User` interface.
import { expectType } from 'expect-type';
interface User {
name: string;
age: number;
}
const user: User = { name: 'Alice', age: 30 };
expectType<User>(user);
Type Equality
This feature allows you to check if two types are equal. In this example, `expectType` is used to assert that types `A` and `B` are equal.
import { expectType } from 'expect-type';
type A = { a: string };
type B = { a: string };
expectType<A, B>(true);
Type Inference
This feature allows you to assert the inferred type of a value. In this example, `expectType` is used to assert that the result of the `add` function is a `number`.
import { expectType } from 'expect-type';
function add(a: number, b: number) {
return a + b;
}
const result = add(1, 2);
expectType<number>(result);
Other packages similar to expect-type
tsd
The `tsd` package is a TypeScript type assertion library that allows you to write tests for your type definitions. It is similar to `expect-type` in that it helps ensure type correctness, but it is more focused on writing test cases for type definitions.
typescript-is
The `typescript-is` package provides runtime type checking for TypeScript. Unlike `expect-type`, which is used for compile-time type assertions, `typescript-is` allows you to perform type checks at runtime, making it useful for scenarios where you need to validate types dynamically.
io-ts
The `io-ts` package is a runtime type system for IO decoding/encoding with TypeScript. It allows you to define codecs that can validate and decode data at runtime. While `expect-type` focuses on compile-time type assertions, `io-ts` provides a way to ensure type safety at runtime.
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.
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 assertionsdtslint
does type checks via comment directives and tslinttsd-check
is a CLI that runs the TypeScript type checker over assertionstype-plus
comes with various type and runtime TypeScript assertionsstatic-type-assert
type assertion functions