Uppercase And Capitalize Derive Key Names

Intrinsic string types apply case transforms at the type level, so handler names and env keys derive from existing keys and renames propagate automatically.

TypescriptConfig Management

Code

typescriptsnippet
33 lines
// Intrinsic string transforms do case conversion at the type level:
// derive new key names from existing ones and renames propagate automatically
interface ServerSettings {
  max_retries: number;
  timeout_ms: number;
  read_only: boolean;
}

// Uppercase turns snake_case keys into env-var style names
type EnvVarKeys = Uppercase<keyof ServerSettings & string>;
const envName: EnvVarKeys = 'MAX_RETRIES';
console.log('env name:', envName);

// Lowercase normalizes the other way: fold external input to lowercase keys before lookup
type NormalizedQuery = Lowercase<'PAGE' | 'LIMIT'>;
const queryKey: NormalizedQuery = 'page';
console.log('query key:', queryKey);

// Capitalize plus template literals derive handler names
type Events = 'click' | 'submit' | 'focusin';
type HandlerTable = {
  [E in Events as `on${Capitalize<E>}`]: (source: string) => void;
};

const handlers: HandlerTable = {
  onClick: (source) => console.log('click from', source),
  onSubmit: () => console.log('submit captured'),
  onFocusin: () => console.log('focus captured'),
};
console.log('handlers:', Object.keys(handlers).join(','));
handlers.onClick('save-button');

// After the rename a typo is a compile error: onClicked does not exist on HandlerTable

Output

Captured from a sandboxed run of this snippet in the build pipeline.

env name: MAX_RETRIES
query key: page
handlers: onClick,onSubmit,onFocusin
click from save-button

Using it

Running the file prints labeled results such as env name:, query key:, click from — each operation’s effect is visible in the output.

Output is labeled for readability — expect lines prefixed with env name:, query key:, click from.

Key statements

[E in Events as `on${Capitalize<E>}`]: (source: string) => void;

Interactive Tools for This Topic

FAQ

Does this require external packages?
No. It relies only on built-in language and platform APIs, so you can paste it into a project without installing anything.