This directory contains comprehensive TypeScript type-level tests using tsd.
Type tests ensure that the TypeScript types in this library work correctly and catch type regressions before they reach production. Unlike runtime tests, type tests validate the type system itself.
Tests for configuration types:
FilterConfig- Complete configuration objectFilterOptions- Partial configuration optionsComparator- Custom comparator function type
Tests for expression types:
PrimitiveExpression- String, number, boolean, nullPredicateFunction- Function predicatesObjectExpression- Object-based filtersExpression- Union of all expression types
Tests for operator types:
ComparisonOperators- $gt, $gte, $lt, $lte, $eq, $neArrayOperators- $in, $nin, $contains, $sizeStringOperators- $startsWith, $endsWith, $contains, $regex, $matchLogicalOperators- $and, $or, $notOperatorExpression- Combined operator typesExtendedObjectExpression- Objects with operators and logical operators
Tests for the main filter function:
- Return type inference
- Expression type validation
- Options parameter handling
- Error cases
Tests for edge cases and complex scenarios:
- Empty interfaces
- Optional fields
- Readonly types
- Union types
- Generic types
- Nullable types
- Recursive types
- Complex unions
- Intersection types
- Tuple types
- Literal types
- Symbol keys
# Run type tests only
pnpm run test:types
# Run all checks (includes type tests)
pnpm run check
# Run before publishing
pnpm run prepublishType tests use special assertion functions from tsd:
expectType<T>(value)- Asserts value is exactly type TexpectAssignable<T>(value)- Asserts value is assignable to type TexpectNotAssignable<T>(value)- Asserts value is NOT assignable to type TexpectError(expression)- Asserts expression produces a type error
import { expectType } from 'tsd';
import { filter } from '../../src/core/filter';
interface User {
name: string;
age: number;
}
const users: User[] = [
{ name: 'John', age: 25 }
];
// Assert that filter returns User[]
expectType<User[]>(filter(users, { name: 'John' }));
// Assert that invalid input produces error
expectError(filter('not-an-array', 'test'));- Catches breaking changes in type signatures
- Validates generic type constraints
- Ensures operator types are properly typed
- Serves as executable documentation
- Shows expected type behavior
- Demonstrates API usage
- Safe to refactor types
- Immediate feedback on type changes
- Prevents accidental breaking changes
- Fast execution (type-level only)
- No runtime overhead
- Fails build on type regressions
- Create a new
.test-d.tsfile or add to existing file - Import types and functions to test
- Write assertions using
expectType,expectAssignable, etc. - Run
pnpm run test:typesto verify
- Type tests are NOT included in the published package
- They run during development and CI/CD
- The main type test file is
build/index.test-d.ts(auto-generated) - Additional test files in this directory provide comprehensive coverage