Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1822,6 +1822,14 @@ Units examples
expectTypeOf(math.unit(math.fraction(5, 2), 'cm')).toExtend<Unit>()
expectTypeOf(math.unit(math.complex(5, 0), 'cm')).toExtend<Unit>()

// Test unit function with MathNumericType and a valueless Unit
const cm = math.unit('cm')
expectTypeOf(math.unit(5, cm)).toExtend<Unit>()
expectTypeOf(math.unit(math.bignumber(5), cm)).toExtend<Unit>()
expectTypeOf(math.unit(math.fraction(5, 2), cm)).toExtend<Unit>()
expectTypeOf(math.unit(math.complex(5, 0), cm)).toExtend<Unit>()
expectTypeOf(math.chain(5).unit(cm)).toExtend<MathJsChain<Unit>>()

// Test unit function with just MathNumericType (optional unit parameter)
expectTypeOf(math.unit(5)).toExtend<Unit>()
expectTypeOf(math.unit(math.bignumber(5))).toExtend<Unit>()
Expand Down
7 changes: 5 additions & 2 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -928,7 +928,7 @@ export interface MathJsInstance extends MathJsFactory {
* @param unit The unit to be created
* @returns The created unit
*/
unit(value: MathNumericType, unit?: string): Unit
unit(value: MathNumericType, unit?: string | Unit): Unit
unit(value: MathCollection): Unit[]

/*************************************************************************
Expand Down Expand Up @@ -5103,7 +5103,10 @@ export interface MathJsChain<TValue> {
* @param unit The unit to be created
*/
unit(this: MathJsChain<string>, unit?: string): MathJsChain<Unit>
unit(this: MathJsChain<MathNumericType>, unit?: string): MathJsChain<Unit>
unit(
this: MathJsChain<MathNumericType>,
unit?: string | Unit
): MathJsChain<Unit>
unit(this: MathJsChain<MathCollection>): MathJsChain<Unit[]>

/*************************************************************************
Expand Down