Skip to content

unacy / UnitAccessor

Type Alias: UnitAccessor<From, Edges>

ts
type UnitAccessor<From, Edges> = {
(...args): From;
  to: { [To in ToUnitsFor<Edges, From> as UnitsFor<To>]: (value: Relax<From>) => To };
  addMetadata: UnitRegistry<Edges extends readonly E[] ? E[] : never> & UnitMap<Edges>;
  register: UnitRegistry<[...Edges[], Edge<From, To>]> & UnitMap<[...Edges[], Edge<From, To>]>;
} & UnitsOf<From>;

Defined in: packages/core/src/registry.ts:89

Callable accessor object returned per unit from the registry.

Provides three capabilities in one surface:

  1. Callable — call it to brand a plain value: registry.Celsius(25)Celsius
  2. .to.<Unit>(value) — convert to another registered unit
  3. .register(toMeta, converter) — extend the registry from this unit

Also reflects the unit's metadata properties directly (e.g., registry.Celsius.symbol).

Type Parameters

Type ParameterDescription
From extends WithTypedUnits<TypedMetadata<SupportedType>>The branded unit type this accessor represents
Edges extends readonly Edge[]Tuple of registered conversion edges in the registry

Example

typescript
const temp = registry.Celsius(25);              // brand a value
const f = registry.Celsius.to.Fahrenheit(temp); // convert
registry.Celsius.symbol;                         // '°C' (if registered in metadata)

Use When

You have a registry and want to convert, brand, or inspect a specific unit.

Pitfalls

NEVER use the accessor's .to.* property at a type that doesn't exist in the registry — TypeScript will report a type error, but at runtime the Proxy throws ConversionError if the path is not reachable.

NEVER call the accessor without arguments for non-primitive types — class-typed units construct via new Constructor(...args), so calling with zero args when the constructor requires arguments will throw at runtime.

See

  • UnitRegistry
  • createRegistry

Released under the MIT License.