ts-expect provides a set of lightweight utilities designed for compile-time TypeScript type assertions. Its primary function, `expectType`, takes a value and a generic type, leveraging the TypeScript compiler to ensure the value is assignable to the generic type without performing any runtime checks. The current stable version is v1.3.0, with releases typically adding new utility functions or refining existing type definitions. Key differentiators include its minimalistic approach, relying solely on TypeScript's type system for validation, making it an excellent tool for testing type definitions within a project's codebase. It avoids the overhead of more complex type-checking frameworks like `dtslint` by focusing on direct, in-code type assertions, making it ideal for maintaining type correctness during refactoring or when developing complex generics.
npm install ts-expectVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic compile-time type assertions with `expectType`, strict type comparisons using `TypeEqual`, and ensuring exhaustive handling of union types with `expectNever`, all without runtime execution.
For runtime validation, use a dedicated runtime validation library (e.g., Zod, Yup) or implement explicit runtime checks alongside `ts-expect` for compile-time safety.
Avoid using `any` when type safety is critical. Prefer `unknown` when you need a top type and explicitly narrow its type before assertion, or use more specific types.
Use `expectNever(value)` when you need to return `never` (e.g., throwing an error in an unhandled case), and `expectType<never>(value)` for purely testing that a type resolves to `never` without affecting runtime flow.
Ensure the type of the value passed to `expectType` is assignable to the generic type. For example, `expectType<string>("hello")` or `expectType<number>(123)`.Verify that `Target` and `Value` are indeed strictly equal in the TypeScript type system. If they are intentionally different, assert `expectType<TypeEqual<Target, Value>>(false)` instead.
Ensure that `expectNever` is used in contexts where its return value (`never`) aligns with the function's return type (e.g., by throwing an error or in a `void` function). Alternatively, make sure all other branches explicitly return a value matching the function's return type.
No dependency data recorded yet.