Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve type definition. #92

Merged
merged 4 commits into from Jun 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 23 additions & 17 deletions index.d.ts
Expand Up @@ -45,13 +45,16 @@ export type CamelCaseKeys<
> = T extends readonly any[]
// Handle arrays or tuples.
? {
[P in keyof T]: CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
Exclude,
StopPaths
>;
// eslint-disable-next-line @typescript-eslint/ban-types
[P in keyof T]: {} extends CamelCaseKeys<T[P]>
? T[P]
: CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
Exclude,
StopPaths
>;
}
: T extends Record<string, any>
// Handle objects.
Expand All @@ -64,16 +67,19 @@ export type CamelCaseKeys<
true,
]
? T[P]
: [Deep] extends [true]
? CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
Exclude,
StopPaths,
AppendPath<Path, P & string>
>
: T[P];
// eslint-disable-next-line @typescript-eslint/ban-types
: {} extends CamelCaseKeys<T[P]>
? T[P]
: [Deep] extends [true]
? CamelCaseKeys<
T[P],
Deep,
IsPascalCase,
Exclude,
StopPaths,
AppendPath<Path, P & string>
>
: T[P];
}
// Return anything else as-is.
: T;
Expand Down
30 changes: 29 additions & 1 deletion index.test-d.ts
@@ -1,4 +1,4 @@
/* eslint-disable @typescript-eslint/naming-convention */
/* eslint-disable @typescript-eslint/naming-convention, @typescript-eslint/indent */
import {expectType, expectAssignable, expectNotType} from 'tsd';
import camelcaseKeys, {type CamelCaseKeys} from './index.js';

Expand Down Expand Up @@ -350,3 +350,31 @@ expectNotType<InvalidConvertedExcludeObjectDataType>(
exclude,
}),
);

expectType<{
funcFoo: () => 'foo';
recordBar: {foo: string};
promiseBaz: Promise<unknown>;
}>(
camelcaseKeys({
func_foo: () => 'foo',
record_bar: {foo: 'bar'},
promise_baz: new Promise(resolve => {
resolve(true);
}),
}),
);

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