Skip to content

skarab42/tssert

Repository files navigation

@skarab/tssert

Micro TypeScript assertion (test) library.

Add your assertions to your usual test suite and run tsc --noEmit, enjoy!

πŸ“Œ Please note that this library does not perform any check at runtime, but only at compile time (or in your IDE).

Why ?

An attempt to reduce the verbosity of type testing.

Install

pnpm add @skarab/tssert typescript

Usages

With a full description of the error

import { expectType } from '@skarab/tssert';

expectType<boolean>().toAccept(true);
expectType<true>().toAccept(false); // ts-error with description

Without error description, but stricter and more flexible

expectType('Hello').toExtend('42').toBe(true);
expectType<string>().toExtend('42').toBe(true);
expectType('Hello').toExtend<string>().toBe(true);
expectType<string>().toExtend<string>().toBe(true);

expectType<boolean>().toExtend<true>().toBe(true); // ts-error
expectType<true>().toExtend<boolean>().toBe(true);

When you move the mouse over toAccept, toExtend and toEqual you can see the expected and received values.

Sans titre

API

toExtend()

interface TypeA {
	a: 42;
	b: string | number;
}

interface TypeB {
	a: 42;
	b: number;
}

expectType<TypeB>().toExtend<TypeA>().toBe(true);
expectType<TypeA>().toExtend<TypeB>().toBe(true); // ts-error

toEqual()

expectType<TypeA>().toEqual<TypeA>().toBe(true);
expectType<TypeA>().toEqual<TypeB>().toBe(true); // ts-error
expectType<TypeB>().toEqual<TypeA>().toBe(true); // ts-error

Types ...

import type { ExpectExtend, ExpectEqual } from '@skarab/tssert';

type A = ExpectExtend<TypeB, TypeA>; // true
type B = ExpectEqual<TypeA, TypeB>; // false

TypeScript god strict mode

It is strongly recommended to activate the strict mode of TypeScript which will activate all checking behaviours that results in stronger guarantees of the program's correctness.

Contributing πŸ’œ

See CONTRIBUTING.md