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.
Code
// 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 HandlerTableOutput
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-buttonUsing 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
Dotenv File ParserParse .env file content into a JSON object, handling comments, export prefixes, single quotes, and double-quoted escape sequences.INI File ParserParse INI configuration files into nested JSON with section objects, global keys, and both semicolon and hash comment support.JSON to TOML ConverterTurn a JSON object into TOML, emitting nested objects as tables and arrays of objects as array-of-tables blocks.JSON to YAML ConverterConvert JSON to readable YAML with nested block mappings, sequences, and quoting only where it is required.
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.
Report a problem with this exampleOpens a pre-filled issue on GitHub.