Merge pull request #31089 from pawkrol/master

Add import function to MathJS types
This commit is contained in:
Andrew Casey
2018-12-07 10:26:15 -08:00
committed by GitHub
2 changed files with 55 additions and 0 deletions
+28
View File
@@ -3,6 +3,7 @@
// Definitions by: Ilya Shestakov <https://github.com/siavol>,
// Andy Patterson <https://github.com/andnp>,
// Brad Besserman <https://github.com/bradbesserman>
// Pawel Krol <https://github.com/pawkrol>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.2
@@ -2571,6 +2572,23 @@ declare namespace math {
* ‘string’, ‘Array’, ‘Date’.
*/
typeof(x: any): string;
/**
* 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;
}
interface Matrix {
@@ -4542,4 +4560,14 @@ declare namespace math {
*/
typeof(): MathJsChain;
}
interface ImportOptions {
override?: boolean;
silent?: boolean;
wrap?: boolean;
}
interface ImportObject {
[key: string]: any;
}
}
+27
View File
@@ -1,3 +1,6 @@
// Import needed for mathjs.import functionality (declaring/extending module)
import * as math from 'mathjs';
/*
Basic usage examples
*/
@@ -392,3 +395,27 @@ JSON serialization/deserialization
const parsed = JSON.parse(stringified, math.json.reviver);
parsed.bigNumber === math.bignumber('1.5'); // true
}
/*
Extend functionality with import
*/
declare module 'mathjs' {
interface MathJsStatic {
testFun(): number;
value: number;
}
}
{
const testFun = () => 5;
math.import({
testFun,
value: 10
}, {});
math.testFun();
const a = math.value * 2;
}