type-fest
Version:
A collection of essential TypeScript types
82 lines (70 loc) • 2.02 kB
TypeScript
import type {DelimiterCase} from './delimiter-case.d.ts'; // For `@link`
import type {UnwrapBrandedPrimitive} from './unwrap-branded-primitive.d.ts';
import type {CasedPropertiesDeep} from './internal/cased-properties-deep.d.ts';
import type {WordsOptions} from './words.d.ts';
import type {DelimiterCasedProperties} from './delimiter-cased-properties.d.ts'; // For `@link`
/**
Convert object properties to a custom string delimiter casing recursively.
This can be useful when, for example, converting some API types from a different style.
@see {@link DelimiterCase}
@see {@link DelimiterCasedProperties}
@example
```
import type {DelimiterCasedPropertiesDeep} from 'type-fest';
type User = {
userId: number;
userName: string;
};
type UserWithFriends = {
userInfo: User;
userFriends: User[];
};
const result: DelimiterCasedPropertiesDeep<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: DelimiterCasedPropertiesDeep<{line1: {line2: [{line3: string}]}}, '-', {splitOnNumbers: true}> = {
'line-1': {
'line-2': [
{
'line-3': 'string',
},
],
},
};
const noSplitOnPunctuation: DelimiterCasedPropertiesDeep<{'user@info': {'user::id': number; 'user::name': string}}, '-', {splitOnPunctuation: false}> = {
'user@info': {
'user::id': 1,
'user::name': 'Tom',
},
};
```
@category Change case
@category Template literal
@category Object
*/
// Select once so nested properties and container entries use the same delimiter.
export type DelimiterCasedPropertiesDeep<
Value,
Delimiter extends string,
Options extends WordsOptions = {},
> = [Delimiter] extends [never]
? CasedPropertiesDeep<Value, {delimiter: Delimiter}, Options>
: {[Choice in UnwrapBrandedPrimitive<Delimiter> & string]: CasedPropertiesDeep<
Value,
{delimiter: Choice},
Options
>}[UnwrapBrandedPrimitive<Delimiter> & string];
export {};