Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: types static methods and members for Unit class #3230

Merged
Changes from 1 commit
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
Next Next commit
fix: types static method for Unit class
Changes unit interface to declare class to enable the adding of static methods.
orelbn committed Jul 8, 2024

Verified

This commit was signed with the committer’s verified signature.
snyk-bot Snyk bot
commit 88006eae6a3e0db84672e0896c4db8c8d2fb88a0
8 changes: 8 additions & 0 deletions test/typescript-tests/testTypes.ts
Original file line number Diff line number Diff line change
@@ -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>
@@ -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>()
}

/*
19 changes: 18 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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 {