type-fest
Version:
A collection of essential TypeScript types
73 lines (61 loc) • 1.53 kB
TypeScript
import type {DelimiterCasedPropertiesDeep} from './delimiter-cased-properties-deep.d.ts';
import type {WordsOptions} from './words.d.ts';
import type {SnakeCase} from './snake-case.d.ts'; // For `@link`
import type {SnakeCasedProperties} from './snake-cased-properties.d.ts'; // For `@link`
/**
Convert object properties to snake case recursively.
This can be useful when, for example, converting some API types from a different style.
```
import type {SnakeCasedPropertiesDeep} from 'type-fest';
type User = {
userId: number;
userName: string;
};
type UserWithFriends = {
userInfo: User;
userFriends: User[];
};
const result: SnakeCasedPropertiesDeep<UserWithFriends> = {
user_info: {
user_id: 1,
user_name: 'Tom',
},
user_friends: [
{
user_id: 2,
user_name: 'Jerry',
},
{
user_id: 3,
user_name: 'Spike',
},
],
};
const splitOnNumbers: SnakeCasedPropertiesDeep<{line1: {line2: [{line3: string}]}}, {splitOnNumbers: true}> = {
line_1: {
line_2: [
{
line_3: 'string',
},
],
},
};
const noSplitOnPunctuation: SnakeCasedPropertiesDeep<{'user@info': {'user::id': number; 'user::name': string}}, {splitOnPunctuation: false}> = {
'user@info': {
'user::id': 1,
'user::name': 'Tom',
},
};
```
*/
export type SnakeCasedPropertiesDeep<
Value,
Options extends WordsOptions = {},
> = DelimiterCasedPropertiesDeep<Value, '_', Options>;
export {};