type-samurai
`type-samurai` is a TypeScript library offering a collection of advanced utility types designed to extend TypeScript's native type manipulation capabilities. Currently at version 1.1.1, the package provides types for arithmetic operations (e.g., `Sum`), logical statements (`If`, `And`, `Or`), string manipulations, and type introspection (`IsAny`, `IsUnknown`, `IsNever`). It differentiates itself by providing complex, often recursive, type-level computations that are not available in the standard TypeScript utility types. While there's no explicit release cadence mentioned, the library appears to be actively maintained, providing a robust toolkit for highly intricate type-safe programming patterns. This library is entirely focused on compile-time type checking and does not include any runtime code, making it a zero-runtime dependency.
Common errors
-
Type instantiation is excessively deep and possibly infinite. (2589)
cause Too many recursive type evaluations or very deep generic type instantiations.fixSimplify the type arguments passed to `type-samurai` utilities, or break down a single complex type into multiple intermediate types. Increase TypeScript's `maxNodeModuleJsDepth` (though this is often a band-aid). -
Expression produces a union type that is too complex to represent. (2590)
cause The resulting union type from a `type-samurai` operation (especially with large number types or string manipulations) exceeds TypeScript's internal complexity limits.fixRestructure the types to produce less expansive union types. Consider if the full range of values needs to be strictly typed at compile-time or if some runtime checks are more appropriate. -
Cannot find module 'type-samurai' or its corresponding type declarations. (2307)
cause The package was likely installed without `--save-dev` or TypeScript is not configured to include `node_modules/@types` or the library's own type declarations.fixEnsure `type-samurai` is installed using `npm install --save-dev type-samurai`, and `tsconfig.json` includes `node_modules` in its `include` or `files` array, or that `typeRoots` is correctly configured if custom paths are used.
Warnings
- gotcha Extremely complex or deeply recursive type computations using `type-samurai` utilities can lead to TypeScript compiler errors like 'Type instantiation is excessively deep and possibly infinite' (error TS2589) or 'Expression produces a union type that is too complex to represent' (error TS2590).
- gotcha While `type-samurai` is zero-runtime, heavy usage of its advanced types can significantly increase TypeScript compilation times, especially in large codebases.
- breaking As a library heavily relying on advanced TypeScript features, `type-samurai` may introduce breaking changes or require specific minimum TypeScript versions for full functionality due to changes in TypeScript's type system.
Install
-
npm install type-samurai -
yarn add type-samurai -
pnpm add type-samurai
Imports
- Sum
import { Sum } from 'type-samurai'import type { Sum } from 'type-samurai' - If
const If = require('type-samurai')import type { If } from 'type-samurai' - IsAny
import * as TypeSamurai from 'type-samurai'
import type { IsAny } from 'type-samurai'
Quickstart
import type { Sum, IfExtends, IsNever, ReturnItselfIfExtends } from 'type-samurai';
// Example 1: Type-level arithmetic
type Total = Sum<123, 456>; // Evaluates to 579
// console.log(Total); // This is a type, not a runtime value
// Example 2: Conditional types
type ResultBasedOnExtension = IfExtends<string, string | number, 'Is String', 'Not String'>; // 'Is String'
type AnotherResult = IfExtends<number, string, 'Is String', 'Not String'>; // 'Not String'
// Example 3: Type introspection
type CheckNever = IsNever<'hello'>; // false
type CheckNeverActual = IsNever<never>; // true
// Example 4: Returning itself conditionally
type MyType = ReturnItselfIfExtends<{ a: string }, object, { a: string }>; // { a: string }
type FallbackType = ReturnItselfIfExtends<null, object, 'Not an object'>; // 'Not an object'
// These operations are purely compile-time. There is no runtime JavaScript code generated.