CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-io-ts

TypeScript runtime type system for IO decoding/encoding

72

1.14x
Overview
Eval results
Files

primitives.mddocs/

Primitive Codecs

Built-in codecs for basic JavaScript/TypeScript types with runtime validation.

Capabilities

String Codec

Validates that a value is a string.

/**
 * String codec that validates string values
 */
const string: StringC;

interface StringC extends Type<string, string, unknown> {}

Usage Example:

import * as t from "io-ts";

const result = t.string.decode("hello");
// result: Right("hello")

const invalid = t.string.decode(42);
// result: Left([validation error])

Number Codec

Validates that a value is a number (excluding NaN).

/**
 * Number codec that validates number values
 */
const number: NumberC;

interface NumberC extends Type<number, number, unknown> {}

Usage Example:

import * as t from "io-ts";

const result = t.number.decode(42);
// result: Right(42)

const invalid = t.number.decode("42");
// result: Left([validation error])

Boolean Codec

Validates that a value is a boolean.

/**
 * Boolean codec that validates boolean values
 */
const boolean: BooleanC;

interface BooleanC extends Type<boolean, boolean, unknown> {}

BigInt Codec

Validates that a value is a bigint.

/**
 * BigInt codec that validates bigint values
 */
const bigint: BigIntC;

interface BigIntC extends Type<bigint, bigint, unknown> {}

Null and Undefined Codecs

Codecs for null and undefined values.

/**
 * Null codec that validates null values
 */
const nullType: NullC;
const null: NullC; // exported alias

interface NullC extends Type<null, null, unknown> {}

/**
 * Undefined codec that validates undefined values
 */
const undefined: UndefinedC;

interface UndefinedC extends Type<undefined, undefined, unknown> {}

/**
 * Void codec that validates void/undefined values
 */
const voidType: VoidC;
const void: VoidC; // exported alias

interface VoidC extends Type<void, void, unknown> {}

Universal Codecs

Codecs that handle any or no values.

/**
 * Unknown codec that accepts any value
 */
const unknown: UnknownC;

interface UnknownC extends Type<unknown, unknown, unknown> {}

/**
 * Any codec that accepts any value (deprecated, use unknown)
 */
const any: AnyC;

interface AnyC extends Type<any, any, unknown> {}

/**
 * Never codec that always fails validation
 */
const never: NeverC;

interface NeverC extends Type<never, never, unknown> {}

Function Codec

Validates that a value is a function.

/**
 * Function codec that validates function values
 */
const Function: FunctionC;

interface FunctionC extends Type<Function, Function, unknown> {}

Array and Record Codecs

Generic container codecs for arrays and objects.

/**
 * Unknown array codec that validates arrays of unknown elements
 */
const UnknownArray: UnknownArrayC;

interface UnknownArrayC extends Type<Array<unknown>, Array<unknown>, unknown> {}

/**
 * Unknown record codec that validates objects with string keys and unknown values
 */
const UnknownRecord: UnknownRecordC;

interface UnknownRecordC extends Type<{ [key: string]: unknown }, { [key: string]: unknown }, unknown> {}

Usage Examples:

import * as t from "io-ts";

// Array validation
const arrayResult = t.UnknownArray.decode([1, 2, "hello"]);
// result: Right([1, 2, "hello"])

// Record validation  
const recordResult = t.UnknownRecord.decode({ a: 1, b: "hello" });
// result: Right({ a: 1, b: "hello" })

Special Integer Codec

Branded integer codec for validating integer numbers.

/**
 * Integer codec that validates integer numbers (branded number)
 */
const Int: BrandC<NumberC, IntBrand>;

interface IntBrand {
  readonly Int: unique symbol;
}

type Int = Branded<number, IntBrand>;

Usage Example:

import * as t from "io-ts";

const result = t.Int.decode(42);
// result: Right(42) with Int brand

const invalid = t.Int.decode(42.5);
// result: Left([validation error - not an integer])

Deprecated Primitives

These primitives are maintained for backward compatibility but should be avoided in new code:

/**
 * @deprecated Use UnknownArray instead
 */
const Array: UnknownArrayC;

/**
 * @deprecated Use UnknownRecord instead  
 */
const Dictionary: UnknownRecordC;

/**
 * @deprecated Use UnknownRecord instead
 */
const object: ObjectC;

/**
 * @deprecated Use unknown instead
 */
type mixed = unknown;

Usage Patterns

Basic Validation

import * as t from "io-ts";
import { PathReporter } from "io-ts/PathReporter";

const validatePrimitives = (data: unknown[]) => {
  data.forEach((item, index) => {
    console.log(`Item ${index}:`, item);
    
    // Test against different primitive codecs
    const tests = [
      { codec: t.string, name: 'string' },
      { codec: t.number, name: 'number' },
      { codec: t.boolean, name: 'boolean' },
      { codec: t.unknown, name: 'unknown' }
    ];
    
    tests.forEach(({ codec, name }) => {
      const result = codec.decode(item);
      if (result._tag === 'Right') {
        console.log(`  ✓ Valid ${name}`);
      } else {
        console.log(`  ✗ Invalid ${name}:`, PathReporter.failure(result.left));
      }
    });
  });
};

validatePrimitives([42, "hello", true, null, undefined]);

Type Guards

import * as t from "io-ts";

// Use built-in type guards
const value: unknown = "hello";

if (t.string.is(value)) {
  // value is now typed as string
  console.log(value.toUpperCase());
}

if (t.number.is(value)) {
  // This block won't execute
  console.log(value.toFixed(2));
}

Combining Primitives

import * as t from "io-ts";

// Create a union of primitives
const StringOrNumber = t.union([t.string, t.number]);

const result1 = StringOrNumber.decode("hello");
// result: Right("hello")

const result2 = StringOrNumber.decode(42);
// result: Right(42)

const result3 = StringOrNumber.decode(true);
// result: Left([validation error])

Install with Tessl CLI

npx tessl i tessl/npm-io-ts

docs

codec.md

combinators.md

core-types.md

decoder.md

encoder.md

index.md

infrastructure.md

primitives.md

refinement.md

reporters.md

schema.md

task-decoder.md

validation.md

tile.json