A collection of codecs and combinators for use with io-ts
—
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Pending
The risk profile of this skill
Codecs for converting between string representations and typed values including numbers, integers, booleans, and big integers. These codecs are essential for parsing API responses, form data, and configuration files where numeric values are represented as strings.
Converts strings to numbers with validation to ensure the result is not NaN and the input is not empty.
/**
* Codec that parses strings to numbers with NaN validation
* Validates input is a non-empty string and result is not NaN
*/
interface NumberFromStringC extends t.Type<number, string, unknown> {}
const NumberFromString: NumberFromStringC;Usage Examples:
import { NumberFromString } from "io-ts-types";
import { isRight } from "fp-ts/lib/Either";
const result1 = NumberFromString.decode("42.5");
// Right(42.5)
const result2 = NumberFromString.decode("invalid");
// Left([ValidationError])
const result3 = NumberFromString.decode("");
// Left([ValidationError])
// Encoding back to string
const encoded = NumberFromString.encode(42.5);
// "42.5"Parses strings to integers with validation to ensure the result is a valid integer.
/**
* Codec that parses strings to integers with validation
* Uses NumberFromString internally then validates result is an integer
*/
interface IntFromStringC extends t.Type<t.Int, string, unknown> {}
const IntFromString: IntFromStringC;Usage Examples:
import { IntFromString } from "io-ts-types";
const result1 = IntFromString.decode("42");
// Right(42)
const result2 = IntFromString.decode("42.5");
// Left([ValidationError]) - not an integer
const result3 = IntFromString.decode("abc");
// Left([ValidationError]) - not a numberConverts non-empty strings to BigInt values for handling large integers beyond JavaScript's Number.MAX_SAFE_INTEGER.
/**
* Codec that parses non-empty strings to BigInt values
* Validates input is a non-empty trimmed string and can be converted to BigInt
*/
interface BigIntFromStringC extends t.Type<bigint, string, unknown> {}
const BigIntFromString: BigIntFromStringC;Usage Examples:
import { BigIntFromString } from "io-ts-types";
const result1 = BigIntFromString.decode("12345678901234567890");
// Right(12345678901234567890n)
const result2 = BigIntFromString.decode("");
// Left([ValidationError]) - empty string
const result3 = BigIntFromString.decode("not-a-number");
// Left([ValidationError]) - invalid BigInt format
// Encoding back to string
const encoded = BigIntFromString.encode(12345678901234567890n);
// "12345678901234567890"Parses string literals "true" and "false" to boolean values.
/**
* Codec that parses string literals "true" and "false" to boolean values
* Only accepts exact string matches (case-sensitive)
*/
interface BooleanFromStringC extends t.Type<boolean, string, unknown> {}
const BooleanFromString: BooleanFromStringC;Usage Examples:
import { BooleanFromString } from "io-ts-types";
const result1 = BooleanFromString.decode("true");
// Right(true)
const result2 = BooleanFromString.decode("false");
// Right(false)
const result3 = BooleanFromString.decode("True");
// Left([ValidationError]) - case sensitive
const result4 = BooleanFromString.decode("1");
// Left([ValidationError]) - only accepts "true"/"false"
// Encoding back to string
const encoded1 = BooleanFromString.encode(true);
// "true"
const encoded2 = BooleanFromString.encode(false);
// "false"Converts numbers to booleans using JavaScript's truthy/falsy semantics (0 = false, any other number = true).
/**
* Codec that converts numbers to booleans
* 0 becomes false, any other number becomes true
*/
interface BooleanFromNumberC extends t.Type<boolean, number, unknown> {}
const BooleanFromNumber: BooleanFromNumberC;Usage Examples:
import { BooleanFromNumber } from "io-ts-types";
const result1 = BooleanFromNumber.decode(0);
// Right(false)
const result2 = BooleanFromNumber.decode(1);
// Right(true)
const result3 = BooleanFromNumber.decode(-1);
// Right(true)
const result4 = BooleanFromNumber.decode(42.5);
// Right(true)
// Encoding back to number
const encoded1 = BooleanFromNumber.encode(false);
// 0
const encoded2 = BooleanFromNumber.encode(true);
// 1import * as t from "io-ts";
import { NumberFromString, BooleanFromString } from "io-ts-types";
const APIResponse = t.type({
userId: NumberFromString,
score: NumberFromString,
isActive: BooleanFromString,
metadata: t.record(t.string, t.string)
});
// Parse API response with string values
const response = {
userId: "123",
score: "85.5",
isActive: "true",
metadata: { source: "web" }
};
const parsed = APIResponse.decode(response);
// Right({ userId: 123, score: 85.5, isActive: true, metadata: { source: "web" } })import * as t from "io-ts";
import { IntFromString, BooleanFromString } from "io-ts-types";
const FormData = t.type({
age: IntFromString,
count: IntFromString,
newsletter: BooleanFromString
});
// Process form data (all values come as strings from HTML forms)
const formFields = {
age: "25",
count: "3",
newsletter: "false"
};
const processed = FormData.decode(formFields);
// Right({ age: 25, count: 3, newsletter: false })import * as t from "io-ts";
import { NumberFromString, BigIntFromString } from "io-ts-types";
const Config = t.type({
port: NumberFromString,
maxFileSize: BigIntFromString,
timeout: NumberFromString
});
// Parse configuration where values might be strings
const configData = {
port: "3000",
maxFileSize: "9007199254740992", // Beyond Number.MAX_SAFE_INTEGER
timeout: "30000"
};
const config = Config.decode(configData);
// Right({ port: 3000, maxFileSize: 9007199254740992n, timeout: 30000 })