string-converters
Version:
A utility library for converting data types and working with arrays. Provides converters for boolean, number, and string values, along with array conversion functions and support for values with predefined options.
15 lines (14 loc) • 858 B
TypeScript
import { Converter } from "../types";
type AnyArray<T> = Array<T> | ReadonlyArray<T>;
type ValueOf<T> = T extends AnyArray<infer K> ? K : T extends {} ? T[keyof T] : never;
type Generalize<T> = T extends string ? string : T extends number ? number : T;
/**
* Creates a converter for values that should be one of the specified options.
*
* @template T - The type of the possible values.
* @param {Converter<Generalize<ValueOf<T>>>} generalConverter - The converter for the generalized value type.
* @param {T} possibleValues - The possible values that the input should match.
* @returns {Converter<ValueOf<T>>} The converter for the specified options.
*/
declare const createOneOfConverter: <const T extends {} | []>(generalConverter: Converter<Generalize<ValueOf<T>>>, possibleValues: T) => Converter<ValueOf<T>>;
export default createOneOfConverter;