From 900a8f162194c33518842b97477a4992963953a7 Mon Sep 17 00:00:00 2001 From: pawkrol Date: Thu, 29 Nov 2018 15:17:52 +0100 Subject: [PATCH 1/4] Add import function --- types/mathjs/index.d.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/types/mathjs/index.d.ts b/types/mathjs/index.d.ts index 6cb49323fa..09b4a9d27c 100644 --- a/types/mathjs/index.d.ts +++ b/types/mathjs/index.d.ts @@ -7,6 +7,7 @@ // TypeScript Version: 2.2 import { Decimal } from "decimal.js"; +import { ImportOptions } from "mathjs"; declare const math: math.MathJsStatic; export as namespace math; @@ -2571,6 +2572,18 @@ declare namespace math { * ‘string’, ‘Array’, ‘Date’. */ typeof(x: any): string; + + /** + * Import functions from an object or a module + * @param object An object with functions to be imported. + * @param options An object with import options. + */ + import(object: object | Array, options: ImportOptions); + + /** + * It may not exists - this is _only_ for import functionality. + */ + [key: string]: any; } interface Matrix { @@ -4542,4 +4555,10 @@ declare namespace math { */ typeof(): MathJsChain; } + + interface ImportOptions { + override?: boolean; + silent?: boolean; + wrap?: boolean; + } } From f6e24ebb8e2b2971102c1203dba773d9cffd1ec1 Mon Sep 17 00:00:00 2001 From: pawkrol Date: Thu, 29 Nov 2018 15:19:50 +0100 Subject: [PATCH 2/4] Remove import and add author --- types/mathjs/index.d.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/types/mathjs/index.d.ts b/types/mathjs/index.d.ts index 09b4a9d27c..7917ce417e 100644 --- a/types/mathjs/index.d.ts +++ b/types/mathjs/index.d.ts @@ -3,11 +3,11 @@ // Definitions by: Ilya Shestakov , // Andy Patterson , // Brad Besserman +// Pawel Krol // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.2 import { Decimal } from "decimal.js"; -import { ImportOptions } from "mathjs"; declare const math: math.MathJsStatic; export as namespace math; From 84f45b6af38fa445a3fcf6099121e7e253fb69c2 Mon Sep 17 00:00:00 2001 From: pawkrol Date: Wed, 5 Dec 2018 12:59:45 +0100 Subject: [PATCH 3/4] Fix tests --- types/mathjs/index.d.ts | 7 ++++++- types/mathjs/mathjs-tests.ts | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/types/mathjs/index.d.ts b/types/mathjs/index.d.ts index 7917ce417e..e335421701 100644 --- a/types/mathjs/index.d.ts +++ b/types/mathjs/index.d.ts @@ -2578,7 +2578,8 @@ declare namespace math { * @param object An object with functions to be imported. * @param options An object with import options. */ - import(object: object | Array, options: ImportOptions); + import(object: ImportObject | ImportObject[], options: ImportOptions): void; + // TypeScript Version: 2.4 /** * It may not exists - this is _only_ for import functionality. @@ -4561,4 +4562,8 @@ declare namespace math { silent?: boolean; wrap?: boolean; } + + interface ImportObject { + [key: string]: any; + } } diff --git a/types/mathjs/mathjs-tests.ts b/types/mathjs/mathjs-tests.ts index babfa3ff37..c5eadb82a5 100644 --- a/types/mathjs/mathjs-tests.ts +++ b/types/mathjs/mathjs-tests.ts @@ -392,3 +392,21 @@ JSON serialization/deserialization const parsed = JSON.parse(stringified, math.json.reviver); parsed.bigNumber === math.bignumber('1.5'); // true } + +/* +Extend functionality with import + */ +{ + const testFun = function() { + return 5; + }; + + math.import({ + testFun, + value: 10 + }, {}); + + math.testFun(); + + const a = math.value * 2; +} From 0df59a63921c700104afd1b1baeb0c07e3707aa3 Mon Sep 17 00:00:00 2001 From: pawkrol Date: Thu, 6 Dec 2018 09:11:42 +0100 Subject: [PATCH 4/4] Change semantics for import --- types/mathjs/index.d.ts | 16 ++++++++++------ types/mathjs/mathjs-tests.ts | 15 ++++++++++++--- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/types/mathjs/index.d.ts b/types/mathjs/index.d.ts index e335421701..a8ec6114eb 100644 --- a/types/mathjs/index.d.ts +++ b/types/mathjs/index.d.ts @@ -2575,16 +2575,20 @@ declare namespace math { /** * Import functions from an object or a module + * To avoid errors when using one of the imported functions extend module like this: + * + * @example + * // imported_math_functions.ts + * declare module 'mathjs' { + * interface MathJsStatic { + * hello(a: number): number; + * } + * } + * * @param object An object with functions to be imported. * @param options An object with import options. */ import(object: ImportObject | ImportObject[], options: ImportOptions): void; - // TypeScript Version: 2.4 - - /** - * It may not exists - this is _only_ for import functionality. - */ - [key: string]: any; } interface Matrix { diff --git a/types/mathjs/mathjs-tests.ts b/types/mathjs/mathjs-tests.ts index c5eadb82a5..0d21be817e 100644 --- a/types/mathjs/mathjs-tests.ts +++ b/types/mathjs/mathjs-tests.ts @@ -1,3 +1,6 @@ +// Import needed for mathjs.import functionality (declaring/extending module) +import * as math from 'mathjs'; + /* Basic usage examples */ @@ -396,10 +399,16 @@ JSON serialization/deserialization /* Extend functionality with import */ + +declare module 'mathjs' { + interface MathJsStatic { + testFun(): number; + value: number; + } +} + { - const testFun = function() { - return 5; - }; + const testFun = () => 5; math.import({ testFun,