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

Fix: CamelCasedPropertiesDeep changing tupleto array #818

Merged
merged 1 commit into from May 6, 2024
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
43 changes: 38 additions & 5 deletions source/camel-cased-properties-deep.d.ts
@@ -1,4 +1,5 @@
import type {CamelCase, CamelCaseOptions} from './camel-case';
import type {UnknownArray} from './unknown-array';

/**
Convert object properties to camel case recursively.
Expand Down Expand Up @@ -44,11 +45,43 @@ const result: CamelCasedPropertiesDeep<UserWithFriends> = {
@category Template literal
@category Object
*/
export type CamelCasedPropertiesDeep<Value, Options extends CamelCaseOptions = {preserveConsecutiveUppercase: true}> = Value extends Function
export type CamelCasedPropertiesDeep<
Value,
Options extends CamelCaseOptions = {preserveConsecutiveUppercase: true},
> = Value extends Function
? Value
: Value extends Array<infer U>
? Array<CamelCasedPropertiesDeep<U, Options>>
: Value extends UnknownArray
? CamelCasedPropertiesArrayDeep<Value>
: Value extends Set<infer U>
? Set<CamelCasedPropertiesDeep<U, Options>> : {
[K in keyof Value as CamelCase<K, Options>]: CamelCasedPropertiesDeep<Value[K], Options>;
? Set<CamelCasedPropertiesDeep<U, Options>>
: {
[K in keyof Value as CamelCase<K, Options>]: CamelCasedPropertiesDeep<
Value[K],
Options
>;
};

// This is a copy of DelimiterCasedPropertiesArrayDeep (see: delimiter-cased-properties-deep.d.ts).
// These types should be kept in sync.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to add this text to DelimiterCasedPropertiesArrayDeep too, otherwise it could get out of sync with this one.

type CamelCasedPropertiesArrayDeep<Value extends UnknownArray> =
Value extends []
? []
: // Tailing spread array
Value extends [infer U, ...infer V]
? [CamelCasedPropertiesDeep<U>, ...CamelCasedPropertiesDeep<V>]
: Value extends readonly [infer U, ...infer V]
? readonly [CamelCasedPropertiesDeep<U>, ...CamelCasedPropertiesDeep<V>]
: // Leading spread array
Value extends readonly [...infer U, infer V]
? [...CamelCasedPropertiesDeep<U>, CamelCasedPropertiesDeep<V>]
: Value extends readonly [...infer U, infer V]
? readonly [
...CamelCasedPropertiesDeep<U>,
CamelCasedPropertiesDeep<V>,
]
: // Array
Value extends Array<infer U>
? Array<CamelCasedPropertiesDeep<U>>
: Value extends ReadonlyArray<infer U>
? ReadonlyArray<CamelCasedPropertiesDeep<U>>
: never;
2 changes: 2 additions & 0 deletions source/delimiter-cased-properties-deep.d.ts
Expand Up @@ -61,6 +61,8 @@ export type DelimiterCasedPropertiesDeep<
>]: DelimiterCasedPropertiesDeep<Value[K], Delimiter>;
};

// This is a copy of CamelCasedPropertiesArrayDeep (see: camel-cased-properties-deep.d.ts).
// These types should be kept in sync.
type DelimiterCasedPropertiesArrayDeep<Value extends UnknownArray, Delimiter extends string> =
Value extends []
? []
Expand Down
3 changes: 3 additions & 0 deletions test-d/camel-cased-properties-deep.ts
Expand Up @@ -19,6 +19,9 @@ expectType<{fooBAR: number; baz: {fooBAR: number; bARFoo: string}}>(baz);
declare const biz: CamelCasedPropertiesDeep<bazBizDeep, {preserveConsecutiveUppercase: false}>;
expectType<{fooBar: number; baz: {fooBar: number; barFoo: string}}>(biz);

declare const tuple: CamelCasedPropertiesDeep<{tuple: [number, string, {D: string}]}>;
expectType<{tuple: [number, string, {d: string}]}>(tuple);

// Verify example
type User = {
UserId: number;
Expand Down