UNPKG

742 BTypeScriptView Raw
1import type {DelimiterCase} from './delimiter-case';
2
3/**
4Convert a string literal to snake-case.
5
6This can be useful when, for example, converting a camel-cased object property to a snake-cased SQL column name.
7
8@example
9```
10import type {SnakeCase} from 'type-fest';
11
12// Simple
13
14const someVariable: SnakeCase<'fooBar'> = 'foo_bar';
15
16// Advanced
17
18type SnakeCasedProperties<T> = {
19 [K in keyof T as SnakeCase<K>]: T[K]
20};
21
22interface ModelProps {
23 isHappy: boolean;
24 fullFamilyName: string;
25 foo: number;
26}
27
28const dbResult: SnakeCasedProperties<ModelProps> = {
29 'is_happy': true,
30 'full_family_name': 'Carla Smith',
31 foo: 123
32};
33```
34
35@category Change case
36@category Template literal
37*/
38export type SnakeCase<Value> = DelimiterCase<Value, '_'>;