diff --git a/types/index.d.ts b/types/index.d.ts index e4c86229f..fe7b5aef6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -156,6 +156,168 @@ declare namespace math { isHexDigit(c: string): boolean; } + interface AccessorNode extends MathNodeCommon { + type: 'AccessorNode'; + isAccessorNode: true; + object: MathNode; + index: IndexNode; + name: string; + } + interface AccessorNodeCtor { + new(object: MathNode, index: IndexNode): AccessorNode; + } + + interface ArrayNode extends MathNodeCommon { + type: 'ArrayNode'; + isArrayNode: true; + items: MathNode[]; + } + interface ArrayNodeCtor { + new(items: MathNode[]): ArrayNode; + } + + interface AssignmentNode extends MathNodeCommon { + type: 'AssignmentNode'; + isAssignmentNode: true; + object: SymbolNode | AccessorNode; + index: IndexNode | null; + value: MathNode; + name: string; + } + interface AssignmentNodeCtor { + new(object: SymbolNode, value: MathNode): AssignmentNode; + new(object: SymbolNode | AccessorNode, index: IndexNode, value: MathNode): AssignmentNode; + } + + interface BlockNode extends MathNodeCommon { + type: 'BlockNode'; + isBlockNode: true; + blocks: Array<{node: MathNode, visible: boolean}>; + } + interface BlockNodeCtor { + new(arr: Array<{node: MathNode} | {node: MathNode, visible: boolean}>): BlockNode; + } + + interface ConditionalNode extends MathNodeCommon { + type: 'ConditionalNode'; + isConditionalNode: boolean; + condition: MathNode; + trueExpr: MathNode; + falseExpr: MathNode; + } + interface ConditionalNodeCtor { + new(condition: MathNode, trueExpr: MathNode, falseExpr: MathNode): ConditionalNode; + } + + interface ConstantNode extends MathNodeCommon { + type: 'ConstantNode'; + isConstantNode: true; + value: any; + } + + interface ConstantNodeCtor { + new(constant: number): ConstantNode; + } + + interface FunctionAssignmentNode extends MathNodeCommon { + type: 'FunctionAssignmentNode'; + isFunctionAssignmentNode: true; + name: string; + params: string[]; + expr: MathNode; + } + interface FunctionAssignmentNodeCtor { + new(name: string, params: string[], expr: MathNode): FunctionAssignmentNode; + } + + interface FunctionNode extends MathNodeCommon { + type: 'FunctionNode'; + isFunctionNode: true; + fn: SymbolNode; + args: MathNode[]; + } + interface FunctionNodeCtor { + new(fn: MathNode | string, args: MathNode[]): FunctionNode; + } + + interface IndexNode extends MathNodeCommon { + type: 'IndexNode'; + isIndexNode: true; + dimensions: MathNode[]; + dotNotation: boolean; + } + interface IndexNodeCtor { + new(dimensions: MathNode[]): IndexNode; + new(dimensions: MathNode[], dotNotation: boolean): IndexNode; + } + + interface ObjectNode extends MathNodeCommon { + type: 'ObjectNode'; + isObjectNode: true; + properties: Record; + } + interface ObjectNodeCtor { + new(properties: Record): ObjectNode; + } + + interface OperatorNode extends MathNodeCommon { + type: 'OperatorNode'; + isOperatorNode: true; + op: string; + fn: string; + args: MathNode[]; + implicit: boolean; + isUnary(): boolean; + isBinary(): boolean; + } + interface OperatorNodeCtor { + new(op: string, fn: string, args: MathNode[], implicit?: boolean): OperatorNode; + } + + interface ParenthesisNode extends MathNodeCommon { + type: 'ParenthesisNode'; + isParenthesisNode: true; + content: MathNode; + } + interface ParenthesisNodeCtor { + new(content: MathNode): ParenthesisNode; + } + + interface RangeNode extends MathNodeCommon { + type: 'RangeNode'; + isRangeNode: true; + start: MathNode; + end: MathNode; + step: MathNode | null; + } + interface RangeNodeCtor { + new(start: MathNode, end: MathNode, step?: MathNode): RangeNode; + } + + interface RelationalNode extends MathNodeCommon { + type: 'RelationalNode'; + isRelationalNode: true; + conditionals: string[]; + params: MathNode[]; + } + interface RelationalNodeCtor { + new(conditionals: string[], params: MathNode[]): RelationalNode; + } + + interface SymbolNode extends MathNodeCommon { + type: 'SymbolNode'; + isSymbolNode: true; + name: string; + } + interface SymbolNodeCtor { + new(name: string): SymbolNode; + } + + type MathNode = AccessorNode | ArrayNode | AssignmentNode | BlockNode | ConditionalNode | ConstantNode | + FunctionAssignmentNode | FunctionNode | IndexNode | ObjectNode | OperatorNode | ParenthesisNode | RangeNode | + RelationalNode | SymbolNode; + + type MathJsFunctionName = keyof MathJsStatic; interface MathJsStatic extends FactoryDependencies { @@ -173,6 +335,23 @@ declare namespace math { SQRT2: number; tau: number; + // Class-like constructors + AccessorNode: AccessorNodeCtor; + ArrayNode: ArrayNodeCtor; + AssignmentNode: AssignmentNodeCtor; + BlockNode: BlockNodeCtor; + ConditionalNode: ConditionalNodeCtor; + ConstantNode: ConstantNodeCtor; + FunctionAssignmentNode: FunctionAssignmentNodeCtor; + FunctionNode: FunctionNodeCtor; + IndexNode: IndexNodeCtor; + ObjectNode: ObjectNodeCtor; + OperatorNode: OperatorNodeCtor; + ParenthesisNode: ParenthesisNodeCtor; + RangeNode: RangeNodeCtor; + RelationalNode: RelationalNodeCtor; + SymbolNode: SymbolNodeCtor; + /** * If null were to be included in this interface, it would be * auto-suggested as an import in VSCode. This causes issues because @@ -500,6 +679,7 @@ declare namespace math { */ qr(A: Matrix | MathArray): { Q: MathArray | Matrix; R: MathArray | Matrix }; + rationalize(expr: MathNode | string, optional?: object | boolean, detailed?: false): MathNode; /** * Transform a rationalizable expression in a rational fraction. If * rational fraction is one variable polynomial then converts the @@ -517,7 +697,7 @@ declare namespace math { optional?: object | boolean, detailed?: true ): { expression: MathNode | string; variables: string[]; coefficients: MathType[] }; - rationalize(expr: MathNode | string, optional?: object | boolean, detailed?: false): MathNode; + /** * Simplify an expression tree. @@ -530,13 +710,7 @@ declare namespace math { * @param [options] (optional) An object with simplify options * @returns Returns the simplified form of expr */ - simplify( - expr: MathNode | string, - rules?: Array<{ l: string; r: string } | string | ((node: MathNode) => MathNode)>, - scope?: object, - options?: SimplifyOptions, - ): MathNode; - simplify(expr: MathNode | string, scope?: object, options?: SimplifyOptions): MathNode; + simplify: Simplify; /** * Calculate the Sparse Matrix LU decomposition with full pivoting. @@ -649,6 +823,7 @@ declare namespace math { * @returns Quotient, x / y */ divide(x: Unit, y: Unit): Unit | number; + divide(x: Unit, y: number): Unit; divide(x: number, y: number): number; divide(x: MathType, y: MathType): MathType; @@ -941,6 +1116,8 @@ declare namespace math { * @param y Value to subtract from x * @returns Subtraction of x and y */ + subtract(x: number, y: number): number; + subtract(x: Unit, y: Unit): Unit; subtract(x: MathType, y: MathType): MathType; /** @@ -2540,7 +2717,7 @@ declare namespace math { * Factory and Dependencies ************************************************************************/ interface FactoryDependencies { - create: (factories: FactoryFunctionMap, config?: ConfigOptions) => Partial; + create: (factories: FactoryFunctionMap, config?: ConfigOptions) => MathJsStatic; factory: ( name: string, dependencies: MathJsFunctionName[], @@ -2941,8 +3118,8 @@ declare namespace math { pow(unit: Unit): Unit; abs(unit: Unit): Unit; to(unit: string): Unit; - toNumber(unit: string): number; - toNumeric(unit: string): number | Fraction | BigNumber; + toNumber(unit?: string): number; + toNumeric(unit?: string): number | Fraction | BigNumber; toSI(): Unit; toString(): string; toJSON(): MathJSON; @@ -2969,6 +3146,26 @@ declare namespace math { fractionsLimit?: number; } + type SimplifyRule = { l: string; r: string } | string | ((node: MathNode) => MathNode); + + interface Simplify { + ( + expr: MathNode | string, + rules?: SimplifyRule[], + scope?: object, + options?: SimplifyOptions, + ): MathNode; + ( + expr: MathNode | string, + scope?: object, + options?: SimplifyOptions, + ): MathNode; + + rules: SimplifyRule[]; + + simplifyCore(expr: MathNode): MathNode; + } + interface UnitDefinition { definition?: string | Unit; prefixes?: string; @@ -2982,32 +3179,14 @@ declare namespace math { evaluate(scope?: any): any; } - interface MathNode { - isNode: boolean; - isAccessorNode?: boolean; - isArrayNode?: boolean; - isAssignmentNode?: boolean; - isBlockNode?: boolean; - isConditionalNode?: boolean; - isConstantNode?: boolean; - isFunctionAssignmentNode?: boolean; - isFunctionNode?: boolean; - isIndexNode?: boolean; - isObjectNode?: boolean; - isOperatorNode?: boolean; - isParenthesisNode?: boolean; - isRangeNode?: boolean; - isRelationalNode?: boolean; - isSymbolNode?: boolean; + interface MathNodeCommon { + isNode: true; + comment: string; + type: 'AccessorNode' | 'ArrayNode' | 'AssignmentNode' | 'BlockNode' | 'ConditionalNode' | 'ConstantNode' | + 'FunctionAssignmentNode' | 'FunctionNode' | 'IndexNode' | 'ObjectNode' | 'OperatorNode' | 'ParenthesisNode' | + 'RangeNode' | 'RelationalNode' | 'SymbolNode'; + isUpdateNode?: boolean; - comment?: string; - content?: MathNode; - op?: string; - fn?: string; - args?: MathNode[]; - type: string; - name?: string; - value?: any; /** * Create a shallow clone of the node. The node itself is cloned, its @@ -3445,7 +3624,7 @@ declare namespace math { * can be specified as an object, string, or function. * @param scope Scope to variables */ - simplify(rules?: Array<{ l: string; r: string } | string | ((node: MathNode) => MathNode)>, scope?: object): MathJsChain; + simplify(rules?: SimplifyRule[], scope?: object): MathJsChain; /** * Calculate the Sparse Matrix LU decomposition with full pivoting.