Skip to content

unacy / WithTypedUnits

Type Alias: WithTypedUnits<M>

ts
type WithTypedUnits<M> = unknown extends M ? WithUnits<any, any> : M extends {
  name: string;
  type: infer TypeField;
} ? TypeField extends keyof PrimitiveTypeMap ? WithUnits<PrimitiveTypeMap[TypeField], M> : TypeField extends SupportedType ? WithUnits<ResolveValueType<TypeField>, M> : never : never;

Defined in: packages/core/src/types.ts:85

Resolve a branded unit type from a TypedMetadata object.

For primitive metadata (where type is a name string like 'number'), maps back through PrimitiveTypeMap to recover the base primitive. For non-primitive metadata (enum, class, record, tuple), resolves to the actual runtime value type via ResolveValueType.

Type Parameters

Type ParameterDescription
M extends TypedMetadata<any>A TypedMetadata instance (e.g., typeof CelsiusMetadata)

Example

typescript
const CelsiusMeta = { name: 'Celsius', type: 'number' } as const;
type Celsius = WithTypedUnits<typeof CelsiusMeta>;
// Celsius = Tagged<number, UNITS, typeof CelsiusMeta>

Use When

You have a metadata object with an explicit type field and want the branded type to reflect the correct base type automatically.

Avoid When

You only have a BaseMetadata object without a type field — use WithUnits<T, M> directly, specifying the base type explicitly.

Pitfalls

NEVER pass any as the type parameter — WithTypedUnits<any> widens to WithUnits<any, any>, bypassing all type checks.

Released under the MIT License.