types/EXPLANATION.md
The code base of Mathjs is written in JavaScript. The TypeScript definitions are maintained separately.
The over all structure is:
create which creates a MathJS instance and returns MathJsInstance.chain, which allows you to use the functions in a chained way, like chain(2).add(3).done(). The function chain returns an interface MathJsChain, which contains all mathjs functions and constants as a method. Unlike the static functions, these methods are defined with the chain instance this as first argument.add, one can do create(addDependencies) for example. To import all functions, one can do create(all).import { add } from 'mathjs'.Maintaining the TypeScript types is done manually. When adding a function, one has to create the following TypeScript definitions:
interface MathJsInstance {...}interface MathJsChain {...}export const {...} : MathJsInstanceexport const {...} : Record<string, FactoryFunctionMap>For example for the function add, we can have the following definitions:
// instance
export interface MathJsInstance extends MathJsFactory {
//...
add<T extends MathType>(x: T, y: T): T
//...
}
// chain
export interface MathJsChain<TValue> {
//...
add<T extends MathType>(this: MathJsChain<T>, y: T): MathJsChain<T>
//...
}
// static
export const {
// ...
add,
// ...
} : MathJsInstance
// dependencies
export const {
// ...
addDependencies,
// ...
} : Record<string, FactoryFunctionMap>
The types are defined in types/index.d.ts.
The tests for the type definitions are located in test/typescript-types/testTypes.ts.
To run the tests for the type definitions:
npm run test:types