Skip to content

Commit

Permalink
Merge pull request #2 from ChristopherChudzicki/cc/discriminated-unions
Browse files Browse the repository at this point in the history
Cc/discriminated unions
  • Loading branch information
mattvague authored Nov 4, 2022
2 parents 534ba8e + 899115d commit 08eb25e
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 40 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
52 changes: 52 additions & 0 deletions test/typescript-tests/nodeExtensions.ts
Original file line number Diff line number Diff line change
@@ -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<MathNode>()
expectTypeOf(instance1).toMatchTypeOf<MathNodeCommon>()
expectTypeOf(instance1).toMatchTypeOf<ConstantNode>()

expectTypeOf(instance2).toMatchTypeOf<MathNode>()
expectTypeOf(instance2).toMatchTypeOf<MathNodeCommon>()
expectTypeOf(instance2).toMatchTypeOf<CustomNode>()

let instance3: MathNode
if (instance3.type === 'FunctionAssignmentNode') {
expectTypeOf(instance3).toMatchTypeOf<FunctionAssignmentNode>()
}
}
42 changes: 4 additions & 38 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -1098,7 +1097,7 @@ Transform examples

expectTypeOf(
math.parse('sqrt(3^2 + 4^2)').transform(myTransform1)
).toMatchTypeOf<OperatorNode<'+', 'add', MathNode[]>>()
).toMatchTypeOf<OperatorNode<'+', 'add', BaseNode[]>>()

assert.deepStrictEqual(
math.parse('sqrt(3^2 + 4^2)').transform(myTransform1).toString(),
Expand All @@ -1110,7 +1109,7 @@ Transform examples
.parse('sqrt(3^2 + 4^2)')
.transform(myTransform1)
.transform(myTransform2)
).toMatchTypeOf<OperatorNode<'-', 'subtract', MathNode[]>>()
).toMatchTypeOf<OperatorNode<'-', 'subtract', BaseNode[]>>()

assert.deepStrictEqual(
math
Expand Down Expand Up @@ -2226,7 +2225,7 @@ Factory Test
}
if (math.isOperatorNode(x)) {
expectTypeOf(x).toMatchTypeOf<
OperatorNode<OperatorNodeOp, OperatorNodeFn, MathNode[]>
OperatorNode<OperatorNodeOp, OperatorNodeFn, BaseNode[]>
>()
}
if (math.isParenthesisNode(x)) {
Expand Down Expand Up @@ -2308,36 +2307,3 @@ Random examples
MathJsChain<number[]>
>()
}

/*
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<MathNode>()
expectTypeOf(instance1).toMatchTypeOf<MathNodeCommon>()

expectTypeOf(instance2).toMatchTypeOf<MathNode>()
expectTypeOf(instance2).toMatchTypeOf<MathNodeCommon>()
expectTypeOf(instance2).toMatchTypeOf<ConstantNode>()

expectTypeOf(instance3).toMatchTypeOf<MathNode>()
expectTypeOf(instance3).toMatchTypeOf<MathNodeCommon>()
expectTypeOf(instance3).toMatchTypeOf<CustomNode>()
}
2 changes: 1 addition & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ declare namespace math {
FunctionNode: FunctionNode
IndexNode: IndexNode
ObjectNode: ObjectNode
OperatorNode: OperatorNode
OperatorNode: OperatorNode<OperatorNodeOp, OperatorNodeFn>
ParenthesisNode: ParenthesisNode
RangeNode: RangeNode
RelationalNode: RelationalNode
Expand Down

0 comments on commit 08eb25e

Please sign in to comment.