normalizeBoolean
Normalizes a value into a boolean or returns null. This helper accepts native booleans and string representations of booleans ("true" and "false").
It never throws and never mutates input. Use it when you need a predictable boolean before validation or further processing.
Signature
function normalizeBoolean(value: unknown): boolean | null
Parameters
| Name | Data type | Description |
|---|---|---|
| value | unknown |
The value to normalize into a boolean. |
Returns
One of:
trueorfalse.null: If the value cannot be interpreted as a boolean.
Behavior
- If the value is already a boolean, it is returned unchanged.
- If the value is a string, it is trimmed and lowercased.
"true"becomestrue,"false"becomesfalse.- All other values return
null. - No mutation, no exceptions, no hidden coercion.
Examples
normalizeBoolean(true) // true
normalizeBoolean(false) // false
normalizeBoolean("true") // true
normalizeBoolean(" TRUE ") // true
normalizeBoolean("false") // false
normalizeBoolean(" False ") // false
normalizeBoolean("yes") // null
normalizeBoolean(1) // null
normalizeBoolean(0) // null
normalizeBoolean(null) // null
normalizeBoolean(undefined) // null
Notes
- Only the exact strings
"true"and"false"(case‑insensitive, trimmed) are accepted. - This helper does not coerce truthy/falsy values like
1,0,"yes","no", or empty strings. - Use
validateBooleanif you need aResult<T>instead ofnull.