diff --git a/test/typescript-tests/testTypes.ts b/test/typescript-tests/testTypes.ts index 9a6d066481..a95ff8ab3a 100644 --- a/test/typescript-tests/testTypes.ts +++ b/test/typescript-tests/testTypes.ts @@ -1822,6 +1822,14 @@ Units examples expectTypeOf(math.unit(math.fraction(5, 2), 'cm')).toExtend() expectTypeOf(math.unit(math.complex(5, 0), 'cm')).toExtend() + // Test unit function with MathNumericType and a valueless Unit + const cm = math.unit('cm') + expectTypeOf(math.unit(5, cm)).toExtend() + expectTypeOf(math.unit(math.bignumber(5), cm)).toExtend() + expectTypeOf(math.unit(math.fraction(5, 2), cm)).toExtend() + expectTypeOf(math.unit(math.complex(5, 0), cm)).toExtend() + expectTypeOf(math.chain(5).unit(cm)).toExtend>() + // Test unit function with just MathNumericType (optional unit parameter) expectTypeOf(math.unit(5)).toExtend() expectTypeOf(math.unit(math.bignumber(5))).toExtend() diff --git a/types/index.d.ts b/types/index.d.ts index d37ff59f22..b22c65da53 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -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[] /************************************************************************* @@ -5103,7 +5103,10 @@ export interface MathJsChain { * @param unit The unit to be created */ unit(this: MathJsChain, unit?: string): MathJsChain - unit(this: MathJsChain, unit?: string): MathJsChain + unit( + this: MathJsChain, + unit?: string | Unit + ): MathJsChain unit(this: MathJsChain): MathJsChain /*************************************************************************