@deessejs/type-testing

A micro library for compile-time type testing in TypeScript.

Installation
npm install @deessejs/type-testing
Or with pnpm:
pnpm add @deessejs/type-testing
Or with yarn:
yarn add @deessejs/type-testing
Quick Start
import { Equal, check, assert, expect } from '@deessejs/type-testing'
type Test = Equal<string, string>
check<string>().equals<string>()
assert<{ a: string }>().hasProperty('a')
expect<string, string>().toBeEqual()
Core Types
Type Equality
import { Equal, NotEqual, SimpleEqual } from '@deessejs/type-testing'
Equal<string, string>
Equal<string, number>
NotEqual<string, number>
SimpleEqual<{ a: string }, { a: string }>
Special Type Detection
import {
IsAny,
IsNever,
IsUnknown,
IsVoid,
IsUndefined,
IsNull,
IsNullable,
IsOptional
} from '@deessejs/type-testing'
IsAny<any>
IsNever<never>
IsUnknown<unknown>
IsVoid<void>
IsUndefined<undefined>
IsNull<null>
IsNullable<string | null>
IsNullable<string | undefined>
IsOptional<string | undefined>
Union, Tuple & Array
import { IsUnion, IsTuple, IsArray } from '@deessejs/type-testing'
IsUnion<'a' | 'b'>
IsUnion<'a'>
IsTuple<[string, number]>
IsTuple<[]>
IsTuple<string[]>
IsArray<string[]>
IsArray<[string]>
Type Inhabitation
import { IsInhabited, IsUninhabited } from '@deessejs/type-testing'
IsInhabited<string>
IsInhabited<never>
IsUninhabited<never>
IsUninhabited<string>
Property Testing
import { HasProperty, PropertyType } from '@deessejs/type-testing'
HasProperty<{ a: string }, 'a'>
HasProperty<{ a: string }, 'b'>
PropertyType<{ a: string }, 'a'>
Function Types
import { Parameters, ReturnType, Parameter } from '@deessejs/type-testing'
Parameters<(a: string, b: number) => void>
ReturnType<(a: string) => number>
Parameter<(a: string, b: number), 0>
Parameter<(a: string, b: number), 1>
Length
import { Length } from '@deessejs/type-testing'
Length<['a', 'b', 'c']>
Length<[]>
Chainable API
check() - Soft type checking
import { check } from '@deessejs/type-testing'
check<string>().equals<string>()
check<string>().equals<number>()
check<string>().extends<string>()
check<string>().extends<any>()
check<{ a: string }>().hasProperty('a')
check<{ a: string }>().hasProperty('b')
check<any>().isAny()
check<never>().isNever()
check<unknown>().isUnknown()
check<string | null>().isNullable()
check<string | undefined>().isOptional()
check<'a' | 'b'>().isUnion()
check<[string, number]>().isTuple()
check<string[]>().isArray()
assert() - Strict type checking
Similar to check() but throws at compile time on failure with a clearer error message.
import { assert } from '@deessejs/type-testing'
assert<string>().equals<number>()
assert<{ a: string }>().hasProperty('b')
expect() - BDD-style API
import { expect } from '@deessejs/type-testing'
expect<string, string>().toBeEqual()
expect<string, number>().toBeNotEqual()
expect<string>().toExtend<string>()
expect<any>().toBeAny()
expect<never>().toBeNever()
expect<string | null>().toBeNullable()
expect<string | undefined>().toBeOptional()
Compile-time Assertions
ExpectTrue & ExpectEqual
import { ExpectTrue, ExpectEqual } from '@deessejs/type-testing'
type Test1 = ExpectTrue<true>
type Test2 = ExpectEqual<string, string>
type IsString<T> = ExpectEqual<T, string>
type Result = IsString<string>
expectFalse
import { expectFalse } from '@deessejs/type-testing'
expectFalse<false>()
expectFalse<true>()
API Reference
Types
Equal<T, U> | Strict equality check |
NotEqual<T, U> | Inequality check |
SimpleEqual<T, U> | Simple equality for plain types |
IsAny<T> | Check if type is any |
IsNever<T> | Check if type is never |
IsUnknown<T> | Check if type is unknown |
IsVoid<T> | Check if type is void |
IsUndefined<T> | Check if type is undefined |
IsNull<T> | Check if type is null |
IsNullable<T> | Check if type is null | undefined |
IsOptional<T> | Check if type may be undefined |
IsUnion<T> | Check if type is a union |
IsTuple<T> | Check if type is a tuple |
IsArray<T> | Check if type is an array |
IsInhabited<T> | Check if type has at least one value |
IsUninhabited<T> | Check if type has no values |
HasProperty<T, K> | Check if type has property K |
PropertyType<T, K> | Get type of property K |
Parameters<T> | Get function parameters as tuple |
ReturnType<T> | Get function return type |
Parameter<T, N> | Get parameter at index N |
Length<T> | Get tuple/array length |
ExpectTrue<T> | Assert T is true |
ExpectEqual<T, U> | Assert T equals U |
Functions
check<T>() | Create a chainable type checker |
assert<T>() | Create an assert type checker (throws on failure) |
expect<T, U>() | Create an expect-style type checker |
expectFalse<T>() | Assert T is false at compile time |
License
MIT