Skip to content

Commit

Permalink
feat: simplify IsNumericLiteral
Browse files Browse the repository at this point in the history
  • Loading branch information
tommy-poetic committed Mar 6, 2023
1 parent 8b387f4 commit d238dfc
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions source/is-literal.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Primitive} from './primitive';
import type {Numeric} from './numeric';
import type {IsNever, IsNotFalse} from './internal';

/** @link https://stackoverflow.com/a/52806744/10292952 */
Expand All @@ -14,14 +15,15 @@ type LiteralCheck<T, LiteralType extends Primitive> = (

type LiteralChecks<T, LiteralUnionType> = (
// Conditional type to force union distribution
LiteralUnionType extends Primitive
? IsNotFalse<LiteralCheck<T, LiteralUnionType>>
: false
IsNotFalse<LiteralUnionType extends Primitive
? LiteralCheck<T, LiteralUnionType>
: never
>
);

export type IsStringLiteral<T> = LiteralCheck<T, string>;

export type IsNumericLiteral<T> = IsNotFalse<LiteralCheck<T, number> | LiteralCheck<T, bigint>>;
export type IsNumericLiteral<T> = LiteralChecks<T, Numeric>;

export type IsBooleanLiteral<T> = LiteralCheck<T, boolean>;

Expand Down

0 comments on commit d238dfc

Please sign in to comment.