Skip to content

Commit

Permalink
Improve type definition (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
Masa-Shin committed Jun 22, 2022
1 parent e783aeb commit 264cc70
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
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);
}),
]),
);

0 comments on commit 264cc70

Please sign in to comment.