Skip to content

Commit

Permalink
fix: types static method for Unit class
Browse files Browse the repository at this point in the history
Changes unit interface to declare class to enable the adding of static methods.
  • Loading branch information
orelbn committed Jul 8, 2024
1 parent b6b76cd commit 88006ea
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
8 changes: 8 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ Chaining examples
)
).toMatchTypeOf<MathJsChain<Unit>>()

expectTypeOf(new Unit(15, 'cm')).toMatchTypeOf<Unit>()

// fraction
expectTypeOf(math.chain(math.fraction('123'))).toMatchTypeOf<
MathJsChain<Fraction>
Expand Down Expand Up @@ -2564,6 +2566,12 @@ Statistics functions' return types
expectTypeOf(
math.quantileSeq([math.unit('5cm'), math.unit('10cm')], 0.75)
).toMatchTypeOf<Unit>()

expectTypeOf(Unit.isValuelessUnit('cm')).toMatchTypeOf<boolean>()
expectTypeOf(Unit.parse('5cm')).toMatchTypeOf<Unit>()
expectTypeOf(
Unit.fromJSON({ value: 5.2, unit: 'inch' })
).toMatchTypeOf<Unit>()
}

/*
Expand Down
19 changes: 18 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4011,7 +4011,11 @@ export interface UnitPrefix {
scientific: boolean
}

export interface Unit {
export declare class Unit {
constructor(
value: number | BigNumber | Fraction | Complex | boolean,
name: string
)
valueOf(): string
clone(): Unit
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -4038,6 +4042,19 @@ export interface Unit {
value: number
fixPrefix: boolean
skipAutomaticSimplification: true
/**
* Parse a string into a unit. The value of the unit is parsed as number, BigNumber, or Fraction depending on the math.js config setting number.
*
* Throws an exception if the provided string does not contain a valid unit or cannot be parsed.
*/
static parse: (str: string) => Unit
/** Test if the given expression is a unit.
*
* The unit can have a prefix but cannot have a value.
*/
static isValuelessUnit: (name: string) => boolean
/** Instantiate a Unit from a JSON object */
static fromJSON: (json: object) => Unit
}

export interface CreateUnitOptions {
Expand Down

0 comments on commit 88006ea

Please sign in to comment.