Skip to content

mwashief/type-calculator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

28 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Type Calculator

Simple compile-time type evaluation

Usage

Import the types

import { Add, Subtract, Multiply, Divide, Modulus, Eval } from '../src/index'

Arithmatics

type Test1 = Add<-4339879, 9410365> // Results in 5070486
type Test2 = Subtract<-4339879, 9410365> // Results in -13750244
type Test3 = Multiply<-4339879, 9410365> // Results in -40839845445835
type Test4 = Divide<-4339879, 9410365> // Results in -1
type Test5 = Modulus<-4339879, 9410365> // Results in 5070486

// Operands can be string also. Example:
type Test1 = Add<-4339879, 9410365> // Results in 5070486
type Test2 = Subtract<-4339879, '9410365'> // Results in -13750244
type Test3 = Multiply<'-4339879', 9410365> // Results in -40839845445835
type Test4 = Divide<'-4339879', '9410365'> // Results in -1
type Test5 = Modulus<-4339879, `${9410365}`> // Results in 5070486

Evaluating Expression

type Expression = '4%3+ 5(a + b.x - (b.y - anotherVariable)* 4/2)'
type SymbolTable = { a: 1; b: { x: 2; y: 3 }; anotherVariable: 55 }
type ValueExpression = Eval<Expression, SymbolTable> // Results in 536

About

Typescript type level arithmetics

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published