Skip to content

Commit 2ab26e0

Browse files
authored
Properly type return value of clone() and cloneDeep() (#2572)
* Properly type `clone` and `cloneDeep` return types * Format
1 parent a731024 commit 2ab26e0

File tree

2 files changed

+39
-4
lines changed

2 files changed

+39
-4
lines changed

types/index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3115,7 +3115,7 @@ declare namespace math {
31153115
* @returns A clone of object x
31163116
*/
31173117
// eslint-disable-next-line @typescript-eslint/no-explicit-any
3118-
clone(x: any): any
3118+
clone<TType>(x: TType): TType
31193119

31203120
/**
31213121
* Test whether a value is an numeric value. In case of a string,
@@ -3785,12 +3785,12 @@ declare namespace math {
37853785
* Create a shallow clone of the node. The node itself is cloned, its
37863786
* childs are not cloned.
37873787
*/
3788-
clone(): MathNode
3788+
clone(): this
37893789
/**
37903790
* Create a deep clone of the node. Both the node as well as all its
37913791
* childs are cloned recursively.
37923792
*/
3793-
cloneDeep(): MathNode
3793+
cloneDeep(): this
37943794
/**
37953795
* Compile an expression into optimized JavaScript code. compile returns
37963796
* an object with a function evaluate([scope]) to evaluate. Example:
@@ -6290,7 +6290,7 @@ declare namespace math {
62906290
*/
62916291

62926292
// eslint-disable-next-line @typescript-eslint/no-explicit-any
6293-
clone(this: MathJsChain<any>): MathJsChain<any>
6293+
clone<TValue>(this: MathJsChain<TValue>): MathJsChain<TValue>
62946294

62956295
/**
62966296
* Test whether a value is an integer number. The function supports

types/index.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ import {
2323
SLUDecomposition,
2424
MathType,
2525
MathNumericType,
26+
ConstantNode,
27+
OperatorNode,
2628
} from 'mathjs'
2729
import * as assert from 'assert'
2830
import { expectTypeOf } from 'expect-type'
@@ -189,6 +191,11 @@ Chaining examples
189191
MathJsChain<MathCollection>
190192
>()
191193

194+
// chain
195+
expectTypeOf(math.chain(12).bignumber().clone()).toMatchTypeOf<
196+
MathJsChain<BigNumber>
197+
>()
198+
192199
// boolean
193200
expectTypeOf(math.chain(math.boolean(true))).toMatchTypeOf<
194201
MathJsChain<boolean>
@@ -1515,6 +1522,34 @@ Function round examples
15151522
assert.throws(() => math.round([3.21, 3.82], [1, 2]), TypeError)
15161523
}
15171524

1525+
/*
1526+
Clone examples
1527+
*/
1528+
{
1529+
const math = create(all, {})
1530+
expectTypeOf(new math.ConstantNode(1).clone()).toMatchTypeOf<ConstantNode>()
1531+
expectTypeOf(
1532+
new math.OperatorNode('*', 'multiply', [
1533+
new math.ConstantNode(3),
1534+
new math.SymbolNode('x'),
1535+
]).clone()
1536+
).toMatchTypeOf<OperatorNode>()
1537+
1538+
expectTypeOf(
1539+
new math.ConstantNode(1).cloneDeep()
1540+
).toMatchTypeOf<ConstantNode>()
1541+
expectTypeOf(
1542+
new math.OperatorNode('*', 'multiply', [
1543+
new math.ConstantNode(3),
1544+
new math.SymbolNode('x'),
1545+
]).cloneDeep()
1546+
).toMatchTypeOf<OperatorNode>()
1547+
1548+
expectTypeOf(
1549+
math.clone(new math.ConstantNode(1))
1550+
).toMatchTypeOf<ConstantNode>()
1551+
}
1552+
15181553
/*
15191554
JSON serialization/deserialization
15201555
*/

0 commit comments

Comments
 (0)