Skip to content

objectenvy / WithoutPrefix

Type Alias: WithoutPrefix<T, Prefix> ​

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

Defined in: typeUtils.ts:274

Remove a Prefix_ from all keys in an env record type, keeping only the matching keys.

Type Parameters ​

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

Remarks ​

Conditionally maps each key K in T: if K matches `${Prefix}_${Rest}`, the key becomes Rest and the value type is preserved. Keys that do not start with Prefix_ are dropped (mapped to never). This is the type-level counterpart to the runtime prefix option of objectify().

Use When ​

  • You want to scope a broader env type to a specific namespace at the type level.
  • You're computing the type that objectify({ prefix: '...' }) would operate on.

Avoid When ​

  • You need runtime prefix stripping — use the prefix option of objectify() instead.

Pitfalls ​

  • NEVER use a Prefix that ends with _ — BECAUSE the pattern is `${Prefix}_${Rest}`, which would match keys like APP__PORT instead of APP_PORT.

Example ​

ts
import type { WithoutPrefix } from 'objectenvy';
type Env = { APP_PORT: string; APP_DEBUG: string; OTHER_VAR: string };
type AppEnv = WithoutPrefix<Env, 'APP'>;
// { PORT: string; DEBUG: string }
// (OTHER_VAR is excluded — no APP_ prefix)

See ​

WithPrefix for the inverse operation

Released under the MIT License.