Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP Bring back discriminated union for MathNode #2820

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Prev Previous commit
Next Next commit
revert to old operatornode typeargs
ChristopherChudzicki committed Nov 3, 2022
commit 899115db1c686cca47215362f6dc3e1804a81815
26 changes: 17 additions & 9 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
@@ -32,6 +32,8 @@ import {
Matrix,
ObjectNode,
OperatorNode,
OperatorNodeFn,
OperatorNodeOp,
ParenthesisNode,
PolarCoordinates,
QRDecomposition,
@@ -1086,16 +1088,16 @@ Transform examples
{
const math = create(all, {})
{
const myTransform1 = (node: MathNode): OperatorNode<'add'> =>
const myTransform1 = (node: MathNode): OperatorNode<'+', 'add'> =>
new OperatorNode('+', 'add', [node, new ConstantNode(1)])
const myTransform2 = (
node: OperatorNode<'add'>
): OperatorNode<'subtract'> =>
node: OperatorNode<'+', 'add'>
): OperatorNode<'-', 'subtract'> =>
new OperatorNode('-', 'subtract', [node, new ConstantNode(5)])

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

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

assert.deepStrictEqual(
math
@@ -1842,15 +1844,17 @@ Function round examples
new math.ConstantNode(3),
new math.SymbolNode('x'),
])
).toMatchTypeOf<OperatorNode<'divide', (ConstantNode | SymbolNode)[]>>()
).toMatchTypeOf<OperatorNode<'/', 'divide', (ConstantNode | SymbolNode)[]>>()

expectTypeOf(new math.ConstantNode(1).clone()).toMatchTypeOf<ConstantNode>()
expectTypeOf(
new math.OperatorNode('*', 'multiply', [
new math.ConstantNode(3),
new math.SymbolNode('x'),
]).clone()
).toMatchTypeOf<OperatorNode<'multiply', (ConstantNode | SymbolNode)[]>>()
).toMatchTypeOf<
OperatorNode<'*', 'multiply', (ConstantNode | SymbolNode)[]>
>()

expectTypeOf(
new math.ConstantNode(1).cloneDeep()
@@ -1860,7 +1864,9 @@ Function round examples
new math.ConstantNode(3),
new math.SymbolNode('x'),
]).cloneDeep()
).toMatchTypeOf<OperatorNode<'unaryPlus', (ConstantNode | SymbolNode)[]>>()
).toMatchTypeOf<
OperatorNode<'+', 'unaryPlus', (ConstantNode | SymbolNode)[]>
>()

expectTypeOf(
math.clone(new math.ConstantNode(1))
@@ -2218,7 +2224,9 @@ Factory Test
expectTypeOf(x).toMatchTypeOf<ObjectNode>()
}
if (math.isOperatorNode(x)) {
expectTypeOf(x).toMatchTypeOf<OperatorNode>()
expectTypeOf(x).toMatchTypeOf<
OperatorNode<OperatorNodeOp, OperatorNodeFn, BaseNode[]>
>()
}
if (math.isParenthesisNode(x)) {
expectTypeOf(x).toMatchTypeOf<ParenthesisNode>()
13 changes: 8 additions & 5 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -319,12 +319,13 @@ declare namespace math {
type OperatorNodeFn = keyof OperatorNodeMap

interface OperatorNode<
TFn extends OperatorNodeFn = OperatorNodeFn,
TOp extends OperatorNodeMap[TFn] = never,
TFn extends OperatorNodeFn = never,
TArgs extends BaseNode[] = BaseNode[]
> extends BaseNode {
type: 'OperatorNode'
isOperatorNode: true
op: OperatorNodeMap[TFn]
op: TOp
fn: TFn
args: TArgs
implicit: boolean
@@ -342,7 +343,7 @@ declare namespace math {
fn: TFn,
args: TArgs,
implicit?: boolean
): OperatorNode<TFn, TArgs>
): OperatorNode<TOp, TFn, TArgs>
}
interface ParenthesisNode<TContent extends BaseNode = BaseNode>
extends BaseNode {
@@ -399,7 +400,7 @@ declare namespace math {
FunctionNode: FunctionNode
IndexNode: IndexNode
ObjectNode: ObjectNode
OperatorNode: OperatorNode
OperatorNode: OperatorNode<OperatorNodeOp, OperatorNodeFn>
ParenthesisNode: ParenthesisNode
RangeNode: RangeNode
RelationalNode: RelationalNode
@@ -3184,7 +3185,9 @@ declare namespace math {

isObjectNode(x: unknown): x is ObjectNode

isOperatorNode(x: unknown): x is OperatorNode<OperatorNodeFn>
isOperatorNode(
x: unknown
): x is OperatorNode<OperatorNodeOp, OperatorNodeFn>

isParenthesisNode(x: unknown): x is ParenthesisNode