diff --git a/types/index.d.ts b/types/index.d.ts index 150571729..24a4f4e68 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -258,6 +258,8 @@ declare namespace math { } interface FunctionNodeCtor { new (fn: MathNode | string, args: MathNode[]): FunctionNode + // eslint-disable-next-line @typescript-eslint/no-explicit-any + onUndefinedFunction: (name: string) => any } interface IndexNode extends MathNode { @@ -383,6 +385,8 @@ declare namespace math { } interface SymbolNodeCtor { new (name: string): SymbolNode + // eslint-disable-next-line @typescript-eslint/no-explicit-any + onUndefinedSymbol: (name: string) => any } /** diff --git a/types/index.ts b/types/index.ts index 451e35a2c..3eb6f8f3e 100644 --- a/types/index.ts +++ b/types/index.ts @@ -1488,6 +1488,19 @@ Units examples math.unit('1 m').splitUnit(['ft', 'in']) } +/** + * Example of custom fallback for onUndefinedSymbol & onUndefinedFunction + */ +{ + const math = create(all, {}) + + math.SymbolNode.onUndefinedSymbol = () => null + + assert.strictEqual(math.evaluate('nonExistingSymbol'), null) + + math.FunctionNode.onUndefinedFunction = () => () => 42 +} + /* Expression tree examples */