I'm looking for a way to make Boolean(truthy)
return true
instead of just Boolean
, and similarly for falsy => false
This is what I came up with:
interface BooleanConstructor {
<T extends false | 0 | '' | null | undefined>(value?: T): false;
<T extends Record<any, any> | string | number | true>(value?: T): true;
<T>(value?: T): boolean;
}
It's working well overall, but running into issues with any
and unknown
. Any suggestions?
const A = Boolean(42 as any); // false ??
const B = Boolean(42 as unknown); // boolean
const A2 = Boolean(''); // false
const B2 = Boolean(0); // false
const C = Boolean(42); // true
const D = Boolean('hello'); // true
const E = Boolean(true); // true
const F = Boolean(false); // false
const G = Boolean(null); // false
const H = Boolean(undefined); // false
const I = Boolean([]); // true
const J = Boolean({}); // true
Playground using a local function, but encountering the same issue.