import type {_IsStringLiteral} from './is-string-literal.d.ts'; import type {_IsNumericLiteral} from './is-numeric-literal.d.ts'; import type {_IsBooleanLiteral} from './is-boolean-literal.d.ts'; import type {_IsSymbolLiteral} from './is-symbol-literal.d.ts'; import type {IsNever} from './is-never.d.ts'; import type {IsAny} from './is-any.d.ts'; import type {CollapseLiterals} from './collapse-literals.d.ts'; import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts'; /** Returns a boolean for whether the given type is a [literal type](https://www.typescriptlang.org/docs/handbook/2/everyday-types.html#literal-types). Returns `false` for `any`, `never`, and `unknown`. Unions are normalized before classification; mixed results return `boolean`. @example ``` import type {IsLiteral} from 'type-fest'; type A = IsLiteral<1>; //=> true type B = IsLiteral; //=> false type C = IsLiteral<1n>; //=> true type D = IsLiteral; //=> false type E = IsLiteral<'type-fest'>; //=> true type F = IsLiteral; //=> false type G = IsLiteral<`on${string}`>; //=> false declare const symbolLiteral: unique symbol; type H = IsLiteral; //=> true type I = IsLiteral; //=> false type J = IsLiteral; //=> true type K = IsLiteral; //=> true type L = IsLiteral; //=> false type M = IsLiteral<1 | 'foo' | false>; //=> true type N = IsLiteral; //=> false type O = IsLiteral<1000n | string | true>; //=> boolean ``` @category Type Guard @category Utilities */ // The normalized scalar predicates already reject `any`; only `never` needs a separate guard. // `any` is checked before `never`, so generic code that wraps the result and infers from it still sees both outcomes. // Plain primitives need no brand normalization. export type IsLiteral = IsAny extends true ? false : IsNever extends true ? false : _IsLiteral : T>>; // Reuse the normalized input instead of repeating the public scalar predicates' boundary checks. type _IsLiteral = | (Extract extends infer Bools // Check the entire boolean part together and skip it when absent. // This explicit check also preserves known false results for generic object inputs. ? IsNever extends true ? never : _IsBooleanLiteral : never) | (IsLiteralNonBools>); type IsLiteralNonBools = T extends number | bigint ? _IsNumericLiteral : T extends string ? _IsStringLiteral : T extends symbol ? _IsSymbolLiteral : false; export {};