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 Parameter | Description |
|---|---|
T | The source env record type. |
Prefix extends string | The 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
prefixoption ofobjectify()instead.
Pitfalls
- NEVER use a
Prefixthat ends with_— BECAUSE the pattern is`${Prefix}_${Rest}`, which would match keys likeAPP__PORTinstead ofAPP_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