The object whose values should be coerced (mutated in place).
Optional configuration options
OptionalparseUnits?: booleanOptional flag to parse unit values like '5m', '10mb'. Default is false.
OptionalskipConversion?: (key: string, value: unknown) => booleanOptional function to skip conversion for specific keys. Default is to convert all keys.
const config = {
debug: 'true',
port: '3000',
nested: {
enabled: 'false',
retries: '5'
}
};
castValuesToTypes(config);
console.log(config);
// {
// debug: true,
// port: 3000,
// nested: {
// enabled: false,
// retries: 5
// }
// }
Coerces string values in an object to their appropriate types. Converts "true"/"false" to booleans, numeric strings to numbers, etc. Recursively processes nested objects.
IMPORTANT: This function mutates the input object in place.
INTENTION: This is a best-effort coercion based on common string patterns. It does not handle all edge cases and should be used with caution. The goal is to convert typical string representations of booleans and numbers to their actual types, while leaving other strings unchanged.
USE CASE: Parsing environment variables or other flatmap configuration objects.