type-samurai

1.1.1 · active · verified Sun Apr 19

`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

Warnings

Install

Imports

Quickstart

Demonstrates type-level arithmetic, conditional type evaluation, and type introspection, showcasing the library's compile-time utility without generating runtime code.

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.

view raw JSON →