diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 938d5959c5..0563ab4a74 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -33,7 +33,7 @@ module.exports = { } }, { - files: ['types/*.ts'], + files: ['types/*.ts', 'test/**/*.ts'], extends: [ 'plugin:@typescript-eslint/recommended', 'plugin:prettier/recommended' // this should come last diff --git a/test/typescript-tests/nodeExtensions.ts b/test/typescript-tests/nodeExtensions.ts new file mode 100644 index 0000000000..dc8d176b7b --- /dev/null +++ b/test/typescript-tests/nodeExtensions.ts @@ -0,0 +1,52 @@ +import { expectTypeOf } from 'expect-type' +import { + MathNode, + BaseNode, + MathNodeCommon, + ConstantNode, + Node, + FunctionAssignmentNode, +} from 'mathjs' + +interface MyNode extends BaseNode { + type: 'MyNode' + a: MathNode +} +declare module 'mathjs' { + interface MathNodeTypes { + MyNode: MyNode + } +} + +/* +MathNode examples +*/ +{ + class CustomNode extends Node implements MyNode { + a: MathNode + type: 'MyNode' + constructor(a: MathNode) { + super() + this.a = a + } + } + + // Built-in subclass of Node + const instance1 = new ConstantNode(2) + + // Custom subclass of node + const instance2 = new CustomNode(new ConstantNode(2)) + + expectTypeOf(instance1).toMatchTypeOf() + expectTypeOf(instance1).toMatchTypeOf() + expectTypeOf(instance1).toMatchTypeOf() + + expectTypeOf(instance2).toMatchTypeOf() + expectTypeOf(instance2).toMatchTypeOf() + expectTypeOf(instance2).toMatchTypeOf() + + let instance3: MathNode + if (instance3.type === 'FunctionAssignmentNode') { + expectTypeOf(instance3).toMatchTypeOf() + } +} diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 3eb6f8f3e9..1393667ebd 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -41,10 +41,9 @@ import { SimplifyRule, SLUDecomposition, SymbolNode, - MathNodeCommon, Unit, - Node, isSymbolNode, + BaseNode, } from 'mathjs' import * as assert from 'assert' import { expectTypeOf } from 'expect-type' @@ -1098,7 +1097,7 @@ Transform examples expectTypeOf( math.parse('sqrt(3^2 + 4^2)').transform(myTransform1) - ).toMatchTypeOf>() + ).toMatchTypeOf>() assert.deepStrictEqual( math.parse('sqrt(3^2 + 4^2)').transform(myTransform1).toString(), @@ -1110,7 +1109,7 @@ Transform examples .parse('sqrt(3^2 + 4^2)') .transform(myTransform1) .transform(myTransform2) - ).toMatchTypeOf>() + ).toMatchTypeOf>() assert.deepStrictEqual( math @@ -2226,7 +2225,7 @@ Factory Test } if (math.isOperatorNode(x)) { expectTypeOf(x).toMatchTypeOf< - OperatorNode + OperatorNode >() } if (math.isParenthesisNode(x)) { @@ -2308,36 +2307,3 @@ Random examples MathJsChain >() } - -/* -MathNode examples -*/ -{ - class CustomNode extends Node { - a: MathNode - constructor(a: MathNode) { - super() - this.a = a - } - } - - // Basic node - const instance1 = new Node() - - // Built-in subclass of Node - const instance2 = new ConstantNode(2) - - // Custom subclass of node - const instance3 = new CustomNode(new ConstantNode(2)) - - expectTypeOf(instance1).toMatchTypeOf() - expectTypeOf(instance1).toMatchTypeOf() - - expectTypeOf(instance2).toMatchTypeOf() - expectTypeOf(instance2).toMatchTypeOf() - expectTypeOf(instance2).toMatchTypeOf() - - expectTypeOf(instance3).toMatchTypeOf() - expectTypeOf(instance3).toMatchTypeOf() - expectTypeOf(instance3).toMatchTypeOf() -} diff --git a/types/index.d.ts b/types/index.d.ts index 5c1e20c929..2e3d6dd54f 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -400,7 +400,7 @@ declare namespace math { FunctionNode: FunctionNode IndexNode: IndexNode ObjectNode: ObjectNode - OperatorNode: OperatorNode + OperatorNode: OperatorNode ParenthesisNode: ParenthesisNode RangeNode: RangeNode RelationalNode: RelationalNode