forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from ChristopherChudzicki/cc/discriminated-unions
Cc/discriminated unions
- Loading branch information
Showing
4 changed files
with
58 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters