Skip to content

Commit 56032da

Browse files
committed
Merge remote-tracking branch 'origin/develop' into develop
2 parents 2b8ecc4 + 09f6da9 commit 56032da

File tree

4 files changed

+61
-10
lines changed

4 files changed

+61
-10
lines changed

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,5 +267,6 @@ Delaney Sylvans <delaneysylvans@gmail.com>
267267
Rani D. <73716554+ranidam@users.noreply.github.com>
268268
Don McCurdy <dm@donmccurdy.com>
269269
Jay Chang <96050090+JayChang4w@users.noreply.github.com>
270+
Frederik Tilkin <977655+mrft@users.noreply.github.com>
270271

271272
# Generated by tools/update-authors.js

test/typescript-tests/testTypes.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,8 +86,29 @@ Basic usage examples
8686
math.add(math.pow(math.sin(angle), 2), math.pow(math.cos(angle), 2))
8787
math.add(2, 3, 4)
8888
math.add(2, 3, math.bignumber(4))
89+
// @ts-expect-error: string arguments are not supported by the types, but it works (if the string contains a number)
90+
math.add(2, '3')
91+
// @ts-expect-error: string arguments are not supported by the types, but it works (if the string contains a number), but should throw an error if it is something else
92+
assert.throws(() => math.add(2, '3 + 5'))
93+
// @ts-expect-error: string arguments are not supported by the types, but it works (if the string contains a number), but should throw an error if it is something else
94+
assert.throws(() => math.add(2, '3 cm'))
95+
// @ts-expect-error: no arguments are not supported by the types, and should throw an error
96+
assert.throws(() => math.add())
97+
// @ts-expect-error: 1 argument is not supported by the types, and should throw an error
98+
assert.throws(() => math.add(1))
99+
89100
math.multiply(2, 3, 4)
90101
math.multiply(2, 3, math.bignumber(4))
102+
// @ts-expect-error: string arguments are not supported by the types, but it works (if the string contains a number)
103+
math.multiply(2, '2') // currently not supported by the types, but turns out to work
104+
// @ts-expect-error: string arguments are not supported by the types, but it works (if the string contains a number), but should throw an error if it is something else
105+
assert.throws(() => math.multiply(2, '3 + 5'))
106+
// @ts-expect-error: string arguments are not supported by the types, but it works (if the string contains a number), but should throw an error if it is something else
107+
assert.throws(() => math.multiply(2, '3 cm'))
108+
// @ts-expect-error: no arguments are not supported by the types, and should throw an error
109+
assert.throws(() => math.multiply())
110+
// @ts-expect-error: 1 argument is not supported by the types, and should throw an error
111+
assert.throws(() => math.multiply(1))
91112

92113
// std and variance check
93114

@@ -1741,6 +1762,35 @@ Units examples
17411762
{
17421763
const math = create(all, {})
17431764

1765+
/*
1766+
Unit function type tests
1767+
*/
1768+
{
1769+
// Test unit function with string argument
1770+
expectTypeOf(math.unit('5 cm')).toExtend<Unit>()
1771+
1772+
// Test unit function with Unit argument
1773+
expectTypeOf(math.unit(math.unit('5 cm'))).toExtend<Unit>()
1774+
1775+
// Test unit function with MathNumericType and string
1776+
expectTypeOf(math.unit(5, 'cm')).toExtend<Unit>()
1777+
expectTypeOf(math.unit(math.bignumber(5), 'cm')).toExtend<Unit>()
1778+
expectTypeOf(math.unit(math.fraction(5, 2), 'cm')).toExtend<Unit>()
1779+
expectTypeOf(math.unit(math.complex(5, 0), 'cm')).toExtend<Unit>()
1780+
1781+
// Test unit function with just MathNumericType (optional unit parameter)
1782+
expectTypeOf(math.unit(5)).toExtend<Unit>()
1783+
expectTypeOf(math.unit(math.bignumber(5))).toExtend<Unit>()
1784+
expectTypeOf(math.unit(math.fraction(5, 2))).toExtend<Unit>()
1785+
// Shouldn't this also work? Currently it does not.
1786+
// expectTypeOf(math.unit(math.complex(5, 0))).toExtend<Unit>()
1787+
1788+
// Test unit function with just MathCollection
1789+
expectTypeOf(math.unit(math.matrix([1, 2, 3]))).toExtend<Unit[]>()
1790+
expectTypeOf(math.unit([1, 2, 3])).toExtend<Unit[]>()
1791+
expectTypeOf(math.unit(math.matrix(['2cm', '5cm']))).toExtend<Unit[]>()
1792+
}
1793+
17441794
// units can be created by providing a value and unit name, or by providing
17451795
// a string with a valued unit.
17461796
const a = math.unit(45, 'cm') // 450 mm
@@ -1803,6 +1853,7 @@ Units examples
18031853

18041854
// units can be converted to a specific type, or to a number
18051855
b.to('cm')
1856+
b.to(math.unit('m'))
18061857
math.to(b, 'inch')
18071858
b.toNumber('cm')
18081859
math.number(b, 'cm')

types/EXPLANATION.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Maintaining the TypeScript types is done manually. When adding a function, one h
2020
3. Add a static definition inside `export const {...} : MathJsInstance`
2121
4. Add a dependencies definition inside `export const {...} : Record<string, FactoryFunctionMap>`
2222

23-
For exampe for the function `add`, we can have the following definitions:
23+
For example for the function `add`, we can have the following definitions:
2424

2525
```ts
2626
// instance

types/index.d.ts

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -867,8 +867,8 @@ export interface MathJsInstance extends MathJsFactory {
867867
* @param unit The unit to be created
868868
* @returns The created unit
869869
*/
870-
unit(value: MathNumericType, unit: string): Unit
871-
unit(value: MathCollection, unit: string): Unit[]
870+
unit(value: MathNumericType, unit?: string): Unit
871+
unit(value: MathCollection): Unit[]
872872

873873
/*************************************************************************
874874
* Expression functions
@@ -1136,9 +1136,9 @@ export interface MathJsInstance extends MathJsFactory {
11361136
* @returns Sum of x and y
11371137
*/
11381138
add<T extends MathType>(x: T, y: T): T
1139-
add<T extends MathType>(...values: T[]): T
1139+
add<T extends MathType>(x: T, y: T, ...values: T[]): T
11401140
add(x: MathType, y: MathType): MathType
1141-
add(...values: MathType[]): MathType
1141+
add(x: MathType, y: MathType, ...values: MathType[]): MathType
11421142

11431143
/**
11441144
* Calculate the cubic root of a value.
@@ -1465,9 +1465,8 @@ export interface MathJsInstance extends MathJsFactory {
14651465
multiply<T extends MathArray>(x: T, y: T): MathScalarType
14661466
multiply(x: Unit, y: Unit): Unit
14671467
multiply(x: number, y: number): number
1468-
multiply(x: MathType, y: MathType): MathType
1469-
multiply<T extends MathType>(...values: T[]): T
1470-
multiply(...values: MathType[]): MathType
1468+
multiply(x: MathType, y: MathType, ...values: MathType[]): MathType
1469+
multiply<T extends MathType>(x: T, y: T, ...values: T[]): T
14711470

14721471
/**
14731472
* Calculate the norm of a number, vector or matrix. The second
@@ -4177,7 +4176,7 @@ export interface Unit {
41774176
divide(unit: Unit): Unit | number
41784177
pow(unit: Unit): Unit
41794178
abs(unit: Unit): Unit
4180-
to(unit: string): Unit
4179+
to(unit: string | Unit): Unit
41814180
toNumber(unit?: string): number
41824181
toNumeric(unit?: string): number | Fraction | BigNumber
41834182
toSI(): Unit
@@ -4827,7 +4826,7 @@ export interface MathJsChain<TValue> {
48274826
*/
48284827
unit(this: MathJsChain<string>, unit?: string): MathJsChain<Unit>
48294828
unit(this: MathJsChain<MathNumericType>, unit?: string): MathJsChain<Unit>
4830-
unit(this: MathJsChain<MathCollection>, unit?: string): MathJsChain<Unit[]>
4829+
unit(this: MathJsChain<MathCollection>): MathJsChain<Unit[]>
48314830

48324831
/*************************************************************************
48334832
* Expression functions

0 commit comments

Comments
 (0)