File tree 2 files changed +17
-3
lines changed
2 files changed +17
-3
lines changed Original file line number Diff line number Diff line change @@ -3,7 +3,8 @@ import type {CamelCase, PascalCase} from 'type-fest';
3
3
// eslint-disable-next-line @typescript-eslint/ban-types
4
4
type EmptyTuple = [ ] ;
5
5
6
- type ObjectOptional = Record < string , unknown > | undefined ;
6
+ // Allow union with, for example, `undefined` and `null`.
7
+ type ObjectUnion = Record < string , unknown > | unknown ;
7
8
8
9
/**
9
10
Return a default type if input type is nil.
@@ -38,7 +39,7 @@ type AppendPath<S extends string, Last extends string> = S extends ''
38
39
Convert keys of an object to camelcase strings.
39
40
*/
40
41
export type CamelCaseKeys <
41
- T extends ObjectOptional | readonly any [ ] ,
42
+ T extends ObjectUnion | readonly any [ ] ,
42
43
Deep extends boolean = false ,
43
44
IsPascalCase extends boolean = false ,
44
45
PreserveConsecutiveUppercase extends boolean = false ,
@@ -71,7 +72,7 @@ export type CamelCaseKeys<
71
72
]
72
73
? T [ P ]
73
74
: [ Deep ] extends [ true ]
74
- ? T [ P ] extends ObjectOptional | readonly any [ ]
75
+ ? T [ P ] extends ObjectUnion | readonly any [ ]
75
76
? CamelCaseKeys <
76
77
T [ P ] ,
77
78
Deep ,
Original file line number Diff line number Diff line change @@ -471,3 +471,16 @@ function camelcaseKeysPascalCase<
471
471
472
472
expectType < { fooBar : { hogeHoge : string } } > ( camelcaseKeysDeep ( { foo_bar : { hoge_hoge : 'hoge_hoge' } } ) ) ;
473
473
expectType < { FooBar : string } > ( camelcaseKeysPascalCase ( { foo_bar : 'foo_bar' } ) ) ;
474
+
475
+ // Test for union type
476
+ // eslint-disable-next-line @typescript-eslint/ban-types
477
+ const objectCamelcased : CamelCaseKeys < { foo_bar : { foo_prop : string } | null } , true >
478
+ = camelcaseKeys ( { foo_bar : { foo_prop : 'foo_props' } } , { deep : true } ) ;
479
+ // eslint-disable-next-line @typescript-eslint/ban-types
480
+ const nullCamelcased : CamelCaseKeys < { foo_bar : { foo_prop : string } | null } , true >
481
+ = camelcaseKeys ( { foo_bar : null } , { deep : true } ) ;
482
+
483
+ // eslint-disable-next-line @typescript-eslint/ban-types
484
+ expectType < { fooBar : { fooProp : string } | null } > ( objectCamelcased ) ;
485
+ // eslint-disable-next-line @typescript-eslint/ban-types
486
+ expectType < { fooBar : { fooProp : string } | null } > ( nullCamelcased ) ;
You can’t perform that action at this time.
0 commit comments