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).
An attempt to reduce the verbosity of type testing.
pnpm add @skarab/tssert typescript
import { expectType } from '@skarab/tssert';
expectType<boolean>().toAccept(true);
expectType<true>().toAccept(false); // ts-error with description
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
andtoEqual
you can see the expected and received values.
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
expectType<TypeA>().toEqual<TypeA>().toBe(true);
expectType<TypeA>().toEqual<TypeB>().toBe(true); // ts-error
expectType<TypeB>().toEqual<TypeA>().toBe(true); // ts-error
import type { ExpectExtend, ExpectEqual } from '@skarab/tssert';
type A = ExpectExtend<TypeB, TypeA>; // true
type B = ExpectEqual<TypeA, TypeB>; // false
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.
See CONTRIBUTING.md