Skip to content

Commit c6ef8fe

Browse files
Fix TypeScript types for passing in multiple objects (#122)
Co-authored-by: Sindre Sorhus <[email protected]>
1 parent 7887a0d commit c6ef8fe

File tree

3 files changed

+9
-27
lines changed

3 files changed

+9
-27
lines changed

index.d.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ type AppendPath<S extends string, Last extends string> = S extends ''
3939
Convert keys of an object to camelcase strings.
4040
*/
4141
export type CamelCaseKeys<
42-
T extends ObjectUnion | readonly any[],
42+
T extends ObjectUnion | ReadonlyArray<Record<string, unknown>>,
4343
Deep extends boolean = false,
4444
IsPascalCase extends boolean = false,
4545
PreserveConsecutiveUppercase extends boolean = false,
4646
Exclude extends readonly unknown[] = EmptyTuple,
4747
StopPaths extends readonly string[] = EmptyTuple,
4848
Path extends string = '',
49-
> = T extends readonly any[]
49+
> = T extends ReadonlyArray<Record<string, unknown>>
5050
// Handle arrays or tuples.
5151
? {
52-
[P in keyof T]: T[P] extends Record<string, unknown> | readonly any[]
52+
[P in keyof T]: T[P] extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
5353
? CamelCaseKeys<
5454
T[P],
5555
Deep,
@@ -72,7 +72,7 @@ export type CamelCaseKeys<
7272
]
7373
? T[P]
7474
: [Deep] extends [true]
75-
? T[P] extends ObjectUnion | readonly any[]
75+
? T[P] extends ObjectUnion | ReadonlyArray<Record<string, unknown>>
7676
? CamelCaseKeys<
7777
T[P],
7878
Deep,
@@ -231,7 +231,7 @@ camelcaseKeys(commandLineArguments);
231231
```
232232
*/
233233
export default function camelcaseKeys<
234-
T extends Record<string, unknown> | readonly any[],
234+
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>,
235235
OptionsType extends Options = Options,
236236
>(
237237
input: T,

index.test-d.ts

+2-20
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ expectType<Array<{fooBar: boolean}>>(camelFooBarArray);
1212

1313
expectType<Array<{fooBar: boolean}>>(camelcaseKeys([{'foo-bar': true}]));
1414

15-
expectType<string[]>(camelcaseKeys(['name 1', 'name 2']));
16-
17-
expectType<string[]>(camelcaseKeys(['name 1', 'name 2'], {deep: true}));
18-
1915
expectType<readonly [{readonly fooBar: true}, {readonly fooBaz: true}]>(
2016
camelcaseKeys([{'foo-bar': true}, {'foo-baz': true}] as const),
2117
);
@@ -440,31 +436,17 @@ expectType<{
440436
}),
441437
);
442438

443-
expectType<[
444-
() => 'foo',
445-
{foo: string},
446-
Promise<unknown>,
447-
]>(
448-
camelcaseKeys([
449-
() => 'foo',
450-
{foo: 'bar'},
451-
new Promise(resolve => {
452-
resolve(true);
453-
}),
454-
]),
455-
);
456-
457439
// Test for function with inferred type
458440
// eslint-disable-next-line @typescript-eslint/comma-dangle
459441
function camelcaseKeysDeep<
460-
T extends Record<string, unknown> | readonly unknown[]
442+
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
461443
>(response: T): CamelCaseKeys<T, true> {
462444
return camelcaseKeys(response, {deep: true});
463445
}
464446

465447
// eslint-disable-next-line @typescript-eslint/comma-dangle
466448
function camelcaseKeysPascalCase<
467-
T extends Record<string, unknown> | readonly unknown[]
449+
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
468450
>(response: T): CamelCaseKeys<T, false, true> {
469451
return camelcaseKeys(response, {pascalCase: true});
470452
}

readme.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ camelcaseKeys(commandLineArguments);
3939

4040
#### input
4141

42-
Type: `object | object[]`
42+
Type: `Record<string, unknown> | ReadonlyArray<Record<string, unknown>>`
4343

44-
An object or array of objects to camel-case.
44+
A plain object or array of plain objects to camel-case.
4545

4646
#### options
4747

0 commit comments

Comments
 (0)