Skip to content

Commit c5b69f5

Browse files
committed
fix: camelize types
- adds support for Arrays - fixes types for nested RegExp and Date types - update tests
1 parent 178c0cd commit c5b69f5

2 files changed

Lines changed: 22 additions & 7 deletions

File tree

types/camelize/camelize-tests.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,20 @@ camelize("foo-bar" as const);
3030
// $ExpectType Date
3131
camelize(new Date());
3232

33+
// $ExpectType Date
34+
camelize({ "my-date": new Date() }).myDate;
35+
3336
// $ExpectType RegExp
3437
camelize(/foo/);
38+
39+
// $ExpectType RegExp
40+
camelize({ "my-regexp": /foo/ }).myRegexp;
41+
42+
// $ExpectType string[]
43+
camelize(["foo-bar", "bar-baz"]);
44+
45+
// $ExpectType string[]
46+
camelize({ "my-array": ["foo-bar", "bar-baz"] }).myArray;
47+
48+
// $ExpectType { fooBar: number; }[]
49+
camelize({ "my-array": [{ "foo-bar": 1 }] }).myArray;

types/camelize/index.d.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ type CamelCase<S extends string> = S extends `${infer FirstWord}${CamelSeparator
77
? `${Lowercase<FirstWord>}${Uppercase<SecondChar>}${CamelCase<Remaining>}`
88
: Lowercase<S>;
99

10-
type Camelize<T> = {
11-
[K in keyof T as CamelCase<string & K>]: T[K] extends {} ? Camelize<T[K]> : T[K];
12-
};
13-
14-
declare function camelize<T>(obj: T): T extends string ? CamelCase<T>
10+
type Camelize<T> = T extends Date ? T
1511
: T extends RegExp ? T
16-
: T extends Date ? T
17-
: T extends {} ? Camelize<T>
12+
: T extends Array<infer I> ? Array<Camelize<I>>
13+
: T extends object ? {
14+
[K in keyof T as CamelCase<string & K>]: Camelize<T[K]>;
15+
}
1816
: T;
1917

18+
declare function camelize<T>(obj: T): T extends string ? CamelCase<T> : Camelize<T>;
19+
2020
export = camelize;

0 commit comments

Comments
 (0)