io-ts is a TypeScript library providing a robust runtime type system, designed for decoding and encoding data at the boundaries of your application. It allows developers to define types once using its codec combinators, which then serve for both static type checking during development and dynamic validation of external data at runtime. This approach significantly reduces common errors when integrating with uncertain data sources like API responses, user input, or configuration files. The current stable version is 2.2.22. The library maintains an active release cadence with frequent patch updates, often introducing and refining "experimental" features in dedicated modules while ensuring a stable core. A key differentiator is its deep integration with `fp-ts`, leveraging functional programming paradigms for highly composable type definitions and comprehensive error handling, promoting a type-safe and resilient approach to data processing.
npm install io-tsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to define a runtime type for a `User` object, decode both valid and invalid data against that type, and leverage `fp-ts` and `PathReporter` for robust error handling and reporting. It also shows how to compose types with optional fields.
Update your `sum` definitions to use bracket notation for non-string tag values, for example: `D.sum('tag')({ [0]: t.type(...) })` instead of `D.sum('tag')({ 0: t.type(...) })`.Replace calls to `t.type` with `t.struct` and `t.fromType` with `t.fromStruct`.
Review the `io-ts` GitHub issues and documentation frequently if relying on experimental modules. Be prepared for potential refactoring during minor or patch updates that might affect these features.
Ensure `fp-ts` is explicitly installed in your project: `npm install fp-ts`.
Upgrade `io-ts` to at least `2.2.18` to ensure compatibility with `typescript@4.8` and newer versions.
Ensure you are using `import` statements (e.g., `import * as t from 'io-ts';`). Verify `tsconfig.json` has `"module": "NodeNext"` or `"ESNext"` and `"moduleResolution": "NodeNext"` or `"Bundler"`. For Node.js, ensure `"type": "module"` is set in your `package.json` if using ESM.
Install `fp-ts` as a dependency: `npm install fp-ts`. Also, ensure correct import paths like `import { pipe } from 'fp-ts/function';`.Examine the `PathReporter.report()` output for detailed error messages indicating exactly where the type mismatch occurred. Adjust the input data to match the expected `io-ts` codec schema.