silentmatt / expr-eval

Mathematical expression evaluator in JavaScript
http://silentmatt.com/javascript-expression-evaluator/
MIT License
1.18k stars 239 forks source link

Update type definitions #261

Open jesse-r-s-hines opened 2 years ago

jesse-r-s-hines commented 2 years ago

Minor changes to make the type definitions more accurate

Add operators.array to ParserOptions

Add types for Parser.unaryOps, Parser.functions, and Parser.consts

Add boolean and array to Value type, since evaluate can return those.

Parser.evaluate("true") // returns boolean
Parser.evaluate("[1,2,3]") // returns array

Make function Values take any[] instead of Value[]. TypeScript won't let you assign a function that only takes number/string etc. to (...args: Value[]) => Value, so make it take any.

let x: (...args: Value[]) => Value = Math.max // TypeScript will error
let x: (...args: any[]) => Value = Math.max // TypeScript is happy

Update evaluate's and simplify's definition to only take an object of Values instead of a Value (which can also be array/boolean/etc.)

Update evaluate functions to return a Value type instead of just number

alecmev commented 1 year ago

It's better to move these to DefinitelyTyped, until expr-eval is rewritten in TypeScript. More info.