From c6883db0d33ea7aa8ab3bca1f49d0a433ae600fe Mon Sep 17 00:00:00 2001 From: "BOUYA Med. Salim" Date: Tue, 18 Oct 2022 09:42:50 +0100 Subject: [PATCH] fix #2603: added type definition for onUndefinedSymbol & onUndefinedFunction (#2610) * fix #2603: added type definition for SymbolNode.onUndefinedSymbol and FuctionNode.onUndefinedFunction * fix return type of onUndefinedFunction * fix return type of onUndefinedFunction * fix onUndefinedSymbol & onUndefinedFunction types, added examples * update onUndefinedFunction ts example --- types/index.d.ts | 4 ++++ types/index.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+) 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 */