使用要点
- 直接运行文件,输出会带上标签,比如
env name:、query key:、click from——输出的每一行都能指回产生它的那次调用。
常见问题
需要安装第三方包吗?
不需要。只使用语言和平台的内建 API,粘贴进项目即可使用,无需安装任何东西。
核心语句
[E in Events as `on${Capitalize<E>}`]: (source: string) => void;
代码示例
// 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