type-fest
Version:
A collection of essential TypeScript types
51 lines (40 loc) • 1.24 kB
TypeScript
import type {CamelCase, CamelCaseOptions} from './camel-case.d.ts';
import type {CamelCasedPropertiesDeep} from './camel-cased-properties-deep.d.ts'; // For `@link`
/**
Convert top-level object properties to camel case.
This can be useful when, for example, converting some API types from a different style.
@see {@link CamelCasedPropertiesDeep}
@see {@link CamelCase}
@example
```
import type {CamelCasedProperties} from 'type-fest';
type User = {
UserId: number;
UserName: string;
};
const result: CamelCasedProperties<User> = {
userId: 1,
userName: 'Tom',
};
const preserveConsecutiveUppercase: CamelCasedProperties<{fooBAR: string}, {preserveConsecutiveUppercase: true}> = {
fooBAR: 'string',
};
const noSplitOnPunctuation: CamelCasedProperties<{'foo::bar': string}, {splitOnPunctuation: false}> = {
'foo::bar': 'string',
};
```
@category Change case
@category Template literal
@category Object
*/
export type CamelCasedProperties<Value, Options extends CamelCaseOptions = {}> = Value extends Function
? Value
: Value extends readonly unknown[]
// Arrays pass through unchanged, since they have no properties to re-case.
? Value
: {
[K in keyof Value as
CamelCase<K, Options>
]: Value[K];
};
export {};