IsBooleanLiteral: LiteralCheck<T, boolean>

Returns a boolean for whether the given type is a true or false literal type.

Useful for: - providing strongly-typed functions when given literal arguments - type utilities, such as when constructing parsers and ASTs

Type Parameters

  • T
import type {IsBooleanLiteral} from 'type-fest';

const id = 123;

type GetId<AsString extends boolean> =
IsBooleanLiteral<AsString> extends true
? AsString extends true
? `${typeof id}`
: typeof id
: number | string;

function getId<AsString extends boolean = false>(options?: {asString: AsString}) {
return (options?.asString ? `${id}` : id) as GetId<AsString>;
}

const numberId = getId();
//=> 123

const stringId = getId({asString: true});
//=> '123'

declare const runtimeBoolean: boolean;
const eitherId = getId({asString: runtimeBoolean});
//=> number | string