Skip to content

Commit

Permalink
DelimiterCasedPropertiesDeep: Fix handing of string (#768)
Browse files Browse the repository at this point in the history
  • Loading branch information
Emiyaaaaa committed Nov 21, 2023
1 parent 855fb64 commit e26c779
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 24 deletions.
20 changes: 9 additions & 11 deletions source/delimiter-cased-properties-deep.d.ts
Expand Up @@ -50,17 +50,15 @@ export type DelimiterCasedPropertiesDeep<
Delimiter extends string,
> = Value extends Function | Date | RegExp
? Value
: Value extends string
? DelimiterCase<Value, Delimiter>
: Value extends UnknownArray
? DelimiterCasedPropertiesArrayDeep<Value, Delimiter>
: Value extends Set<infer U>
? Set<DelimiterCasedPropertiesDeep<U, Delimiter>> : {
[K in keyof Value as DelimiterCase<
K,
Delimiter
>]: DelimiterCasedPropertiesDeep<Value[K], Delimiter>;
};
: Value extends UnknownArray
? DelimiterCasedPropertiesArrayDeep<Value, Delimiter>
: Value extends Set<infer U>
? Set<DelimiterCasedPropertiesDeep<U, Delimiter>> : {
[K in keyof Value as DelimiterCase<
K,
Delimiter
>]: DelimiterCasedPropertiesDeep<Value[K], Delimiter>;
};

type DelimiterCasedPropertiesArrayDeep<Value extends UnknownArray, Delimiter extends string> =
Value extends []
Expand Down
36 changes: 23 additions & 13 deletions test-d/delimiter-cased-properties-deep.ts
Expand Up @@ -53,20 +53,30 @@ expectType<{readonly 'user-id'?: number}>(key);

/** Test Array */
// Test for tuple
declare const tuple: DelimiterCasedPropertiesDeep<['userId', 'userName'], '-'>;
expectType<['user-id', 'user-name']>(tuple);
// Test for readonly
declare const readonlyTuple: DelimiterCasedPropertiesDeep<readonly ['userId', 'userName'], '-'>;
expectType<readonly ['user-id', 'user-name']>(readonlyTuple);
declare const tuple: DelimiterCasedPropertiesDeep<[User], '-'>;
expectType<[{'user-id': number;'user-name': string;date: Date;'reg-exp': RegExp}]>(tuple);
declare const tuple2: DelimiterCasedPropertiesDeep<['UserId', 'UserAge', string], '-'>;
expectType<['UserId', 'UserAge', string]>(tuple2);
// Test for readonly tuple
declare const readonlyTuple: DelimiterCasedPropertiesDeep<readonly [{userId: string}, {userName: number}], '-'>;
expectType<readonly [{'user-id': string}, {'user-name': number}]>(readonlyTuple);
// Test for array
declare const array: DelimiterCasedPropertiesDeep<Array<'userId'>, '-'>;
expectType<Array<'user-id'>>(array);
declare const array: DelimiterCasedPropertiesDeep<User[], '-'>;
expectType<Array<{'user-id': number;'user-name': string;date: Date;'reg-exp': RegExp}>>(array);
// Test for readonly array
declare const readonlyArray: DelimiterCasedPropertiesDeep<ReadonlyArray<'userId'>, '-'>;
expectType<ReadonlyArray<'user-id'>>(readonlyArray);
declare const readonlyArray: DelimiterCasedPropertiesDeep<ReadonlyArray<{userId: string}>, '-'>;
expectType<ReadonlyArray<{'user-id': string}>>(readonlyArray);
// Test for tailing spread array
declare const tailingSpreadArray: DelimiterCasedPropertiesDeep<['userId', 'userName', ...Array<'userAge'>], '-'>;
expectType<['user-id', 'user-name', ...Array<'user-age'>]>(tailingSpreadArray);
declare const tailingSpreadArray: DelimiterCasedPropertiesDeep<[{userId: string}, {userName: number}, ...Array<{userAge: number}>], '-'>;
expectType<[{'user-id': string}, {'user-name': number}, ...Array<{'user-age': number}>]>(tailingSpreadArray);
// Test for leading spread array
declare const leadingSpreadArray: DelimiterCasedPropertiesDeep<[...Array<'userId'>, 'userName', 'userAge'], '-'>;
expectType<[...Array<'user-id'>, 'user-name', 'user-age']>(leadingSpreadArray);
declare const leadingSpreadArray: DelimiterCasedPropertiesDeep<[...Array<{userId: string}>, {userName: number}, {userAge: number}], '-'>;
expectType<[...Array<{'user-id': string}>, {'user-name': number}, {'user-age': number}]>(leadingSpreadArray);
// Test for enum
enum UserType {
AdminUser = 'adminUser',
NormalUser = 'normalUser',
}
declare const enumTest: DelimiterCasedPropertiesDeep<{userType: UserType}, '-'>;
expectType<{['user-type']: UserType}>(enumTest);
enumTest['user-type'] = UserType.AdminUser;

0 comments on commit e26c779

Please sign in to comment.