Skip to content

Instantly share code, notes, and snippets.

@bolencki13
Created March 17, 2023 13:06
Show Gist options
  • Save bolencki13/1bb6b87c66da6b7e6182d601a2dd5ab4 to your computer and use it in GitHub Desktop.
Save bolencki13/1bb6b87c66da6b7e6182d601a2dd5ab4 to your computer and use it in GitHub Desktop.
type CompareNumbers<
T extends number,
U extends number,
A extends any[] = [],
> = T extends U
? 0 : A['length'] extends T
? -1
: A['length'] extends U
? 1
: CompareNumbers<T, U, ['a', ...A]>
type Not<T> =
T extends true ? false : true;
type Equal<L extends number, R extends number> = CompareNumbers<L, R> extends 0 ? true : false;
type LessThan<L extends number, R extends number> = CompareNumbers<L, R> extends -1 ? true : false;
type GreaterThan<L extends number, R extends number> = CompareNumbers<L, R> extends 1 ? true : false;
type Operator = '==' | '!=' | '<' | '>' | '>=' | '<=';
type Conditional<L extends number, O extends Operator, R extends number> = {
left: L,
operator: O,
right: R
}
type Evaluate<C extends Conditional<any, any, any>> =
C extends Conditional<infer L, '==', infer R>
? Equal<L, R>
: C extends Conditional<infer L, '!=', infer R>
? Not<Equal<L, R>>
: C extends Conditional<infer L, '<', infer R>
? LessThan<L, R>
: C extends Conditional<infer L, '<=', infer R>
? Not<GreaterThan<L, R>>
: C extends Conditional<infer L, '>', infer R>
? GreaterThan<L, R>
: C extends Conditional<infer L, '>=', infer R>
? Not<LessThan<L, R>>
: unknown;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment