Skip to content

objectenvy / WithPrefix

Type Alias: WithPrefix<T, Prefix>

ts
type WithPrefix<T, Prefix> = { [K in keyof T as K extends string ? `${Prefix}_${K}` : never]: T[K] };

Defined in: typeUtils.ts:237

Add a Prefix_ to all keys in an env record type.

Type Parameters

Type ParameterDescription
TThe source env record type.
Prefix extends stringThe string prefix to prepend (without trailing underscore).

Remarks

Maps every string key K in T to `${Prefix}_${K}`, preserving value types. Non-string keys are dropped (never). Useful for namespacing env types when combining multiple services or packages that each export their own env shapes.

Use When

  • You're composing env types from multiple packages and need to namespace them with a prefix.
  • You want to generate the prefixed env shape for documentation or .env.example scaffolding.

Avoid When

  • You need to add the prefix at runtime — this is a type-level only utility; use string interpolation in your env object builder.

Pitfalls

  • NEVER use a Prefix that already ends with _ — BECAUSE the type concatenates ${Prefix}_${K}, resulting in double underscores (APP__PORT).

Example

ts
import type { WithPrefix } from 'objectenvy';
type Env = { PORT: string; DEBUG: string };
type PrefixedEnv = WithPrefix<Env, 'APP'>;
// { APP_PORT: string; APP_DEBUG: string }

See

WithoutPrefix for the inverse operation

Released under the MIT License.