1.0.1 ⢠Published 3 years ago
@skarab/tssert v1.0.1
@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 typescriptUsages
With a full description of the error
import { expectType } from '@skarab/tssert';
expectType<boolean>().toAccept(true);
expectType<true>().toAccept(false); // ts-error with descriptionWithout 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,toExtendandtoEqualyou can see the expected and received values.
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-errortoEqual()
expectType<TypeA>().toEqual<TypeA>().toBe(true);
expectType<TypeA>().toEqual<TypeB>().toBe(true); // ts-error
expectType<TypeB>().toEqual<TypeA>().toBe(true); // ts-errorTypes ...
import type { ExpectExtend, ExpectEqual } from '@skarab/tssert';
type A = ExpectExtend<TypeB, TypeA>; // true
type B = ExpectEqual<TypeA, TypeB>; // falseTypeScript 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
