Created
March 17, 2023 13:06
-
-
Save bolencki13/1bb6b87c66da6b7e6182d601a2dd5ab4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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