Unit - Convert between units
API Reference for Unit
This documentation provides complete coverage of the Unit class, a comprehensive unit conversion utility that supports conversions between various measurement systems.
Table of Contents
- Constructor
- Instance Methods
- Static Conversion Methods
- Type Definitions
Constructor
Signature
constructor(value: number, unit?: UnitKey)
Parameters
value
: Numeric value to convertunit
: Optional unit type (e.g., 'kg', 'm', 'kb')
Behavior
- Stores value and unit for instance methods
- Unit parameter is optional for generic conversions
Example
new Unit(100, 'kg'); // 100 kilograms
new Unit(32, 'F'); // 32 degrees Fahrenheit
Type Definitions
UnitKey
type UnitKey = keyof typeof UNITS;
Supported unit abbreviations (e.g., 'kg', 'm', 'F')
UnitLabel
type UnitLabel = (typeof UNITS)[UnitKey];
Full names of units (e.g., 'Kilogram', 'Meter')
SIPrefix
type SIPrefix = keyof typeof PREFIX_MULTIPLIERS;
Scientific prefixes (e.g., 'k', 'm', 'M')
UnitNumberMethods
type UnitNumberMethods = {
[K in keyof typeof Unit]: (typeof Unit)[K] extends (
(value: number) => number
) ? K : never;
}[keyof typeof Unit];
Type-safe names of static conversion methods
UNITS
const UNITS = {
// Length
m: 'Meter',
km: 'Kilometer',
// ...etc
};
Mapping of unit abbreviations to full names
PREFIX_MULTIPLIERS
const PREFIX_MULTIPLIERS = {
y: 1e-24,
z: 1e-21,
// ...etc
};
Scientific prefix multipliers