Skip to content

Commit

Permalink
feat: application of unbranding type to types not applied
Browse files Browse the repository at this point in the history
  • Loading branch information
kakasoo committed Jan 20, 2025
1 parent 7605f7e commit 13ea1a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 9 deletions.
10 changes: 6 additions & 4 deletions src/types/DeepDateToString.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { DeepStrictUnbrand } from './DeepStrictUnbrand';

/**
* A utility type that recursively converts all `Date` types within a nested object or array to `string`.
*
* - If `T` is an array of objects, the type processes each element recursively.
* - If `T` is a `Date`, it is converted to `string`.
* - If `T` is an object, each key is checked recursively for `Date` types or nested objects.
*/
export type DeepDateToString<T> =
T extends Array<infer I extends object>
export type DeepDateToString<T extends object> =
DeepStrictUnbrand<T> extends Array<infer I extends object>
? Array<DeepDateToString<I>>
: T extends Date
? string
: {
[K in keyof T]: T[K] extends infer I
? I extends Date
? string
: I extends object
? DeepDateToString<I>
: DeepStrictUnbrand<I> extends object
? DeepDateToString<DeepStrictUnbrand<I>>
: I
: never;
};
7 changes: 4 additions & 3 deletions src/types/GetStrictObjectLastKeys.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DeepStrictUnbrand } from './DeepStrictUnbrand';
import type { IsAny } from './IsAny';
import type { IsUnion } from './IsUnion';
import type { ValueType } from './ValueType';
Expand Down Expand Up @@ -53,8 +54,8 @@ export type DeepStrictObjectLastKeys<
},
P extends keyof T = keyof T,
> =
T extends Array<infer Element>
DeepStrictUnbrand<T> extends Array<infer Element>
? Element extends object
? `${Joiner['array']}.${DeepStrictObjectLastKeys<Element, Joiner>}`
? `${Joiner['array']}.${DeepStrictObjectLastKeys<DeepStrictUnbrand<Element>, Joiner>}`
: `${Joiner['array']}.${keyof Element extends string ? keyof Element : never}`
: __DeepStrictObjectKeys<T, Joiner, P>;
: __DeepStrictObjectKeys<DeepStrictUnbrand<T>, Joiner, Extract<P, keyof DeepStrictUnbrand<T>>>;
11 changes: 9 additions & 2 deletions test/DeepDateToString.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ok } from 'node:assert';
import test from 'node:test';
import typia from 'typia';
import { DeepDateToString, Equal } from '../src';
import test from 'node:test';
import { ok } from 'node:assert';

test('If the prop type of an object is string | Date', () => {
type Question = DeepDateToString<{ prop: string | Date }>;
Expand Down Expand Up @@ -37,3 +37,10 @@ test('If the prop type of an object is symbol | Date', () => {

ok(typia.random<Answer>());
});

test('If the prop type of an object is symbol | (string & {}) | Date', () => {
type Question = DeepDateToString<{ prop: symbol | (string & {}) | Date }>;
type Answer = Equal<Question, { prop: symbol | string }>;

ok(typia.random<Answer>());
});

0 comments on commit 13ea1a7

Please sign in to comment.