Skip to content

Commit

Permalink
Fix TypeScript types for passing in multiple objects (#122)
Browse files Browse the repository at this point in the history
Co-authored-by: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
yutak23 and sindresorhus committed Jan 13, 2024
1 parent 7887a0d commit c6ef8fe
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 27 deletions.
10 changes: 5 additions & 5 deletions index.d.ts
Expand Up @@ -39,17 +39,17 @@ type AppendPath<S extends string, Last extends string> = S extends ''
Convert keys of an object to camelcase strings.
*/
export type CamelCaseKeys<
T extends ObjectUnion | readonly any[],
T extends ObjectUnion | ReadonlyArray<Record<string, unknown>>,
Deep extends boolean = false,
IsPascalCase extends boolean = false,
PreserveConsecutiveUppercase extends boolean = false,
Exclude extends readonly unknown[] = EmptyTuple,
StopPaths extends readonly string[] = EmptyTuple,
Path extends string = '',
> = T extends readonly any[]
> = T extends ReadonlyArray<Record<string, unknown>>
// Handle arrays or tuples.
? {
[P in keyof T]: T[P] extends Record<string, unknown> | readonly any[]
[P in keyof T]: T[P] extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
? CamelCaseKeys<
T[P],
Deep,
Expand All @@ -72,7 +72,7 @@ export type CamelCaseKeys<
]
? T[P]
: [Deep] extends [true]
? T[P] extends ObjectUnion | readonly any[]
? T[P] extends ObjectUnion | ReadonlyArray<Record<string, unknown>>
? CamelCaseKeys<
T[P],
Deep,
Expand Down Expand Up @@ -231,7 +231,7 @@ camelcaseKeys(commandLineArguments);
```
*/
export default function camelcaseKeys<
T extends Record<string, unknown> | readonly any[],
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>,
OptionsType extends Options = Options,
>(
input: T,
Expand Down
22 changes: 2 additions & 20 deletions index.test-d.ts
Expand Up @@ -12,10 +12,6 @@ expectType<Array<{fooBar: boolean}>>(camelFooBarArray);

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

expectType<string[]>(camelcaseKeys(['name 1', 'name 2']));

expectType<string[]>(camelcaseKeys(['name 1', 'name 2'], {deep: true}));

expectType<readonly [{readonly fooBar: true}, {readonly fooBaz: true}]>(
camelcaseKeys([{'foo-bar': true}, {'foo-baz': true}] as const),
);
Expand Down Expand Up @@ -440,31 +436,17 @@ expectType<{
}),
);

expectType<[
() => 'foo',
{foo: string},
Promise<unknown>,
]>(
camelcaseKeys([
() => 'foo',
{foo: 'bar'},
new Promise(resolve => {
resolve(true);
}),
]),
);

// Test for function with inferred type
// eslint-disable-next-line @typescript-eslint/comma-dangle
function camelcaseKeysDeep<
T extends Record<string, unknown> | readonly unknown[]
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
>(response: T): CamelCaseKeys<T, true> {
return camelcaseKeys(response, {deep: true});
}

// eslint-disable-next-line @typescript-eslint/comma-dangle
function camelcaseKeysPascalCase<
T extends Record<string, unknown> | readonly unknown[]
T extends Record<string, unknown> | ReadonlyArray<Record<string, unknown>>
>(response: T): CamelCaseKeys<T, false, true> {
return camelcaseKeys(response, {pascalCase: true});
}
Expand Down
4 changes: 2 additions & 2 deletions readme.md
Expand Up @@ -39,9 +39,9 @@ camelcaseKeys(commandLineArguments);

#### input

Type: `object | object[]`
Type: `Record<string, unknown> | ReadonlyArray<Record<string, unknown>>`

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

#### options

Expand Down

0 comments on commit c6ef8fe

Please sign in to comment.