Skip to content

Commit 13ea1a7

Browse files
committed
feat: application of unbranding type to types not applied
1 parent 7605f7e commit 13ea1a7

File tree

3 files changed

+19
-9
lines changed

3 files changed

+19
-9
lines changed

src/types/DeepDateToString.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
1+
import { DeepStrictUnbrand } from './DeepStrictUnbrand';
2+
13
/**
24
* A utility type that recursively converts all `Date` types within a nested object or array to `string`.
35
*
46
* - If `T` is an array of objects, the type processes each element recursively.
57
* - If `T` is a `Date`, it is converted to `string`.
68
* - If `T` is an object, each key is checked recursively for `Date` types or nested objects.
79
*/
8-
export type DeepDateToString<T> =
9-
T extends Array<infer I extends object>
10+
export type DeepDateToString<T extends object> =
11+
DeepStrictUnbrand<T> extends Array<infer I extends object>
1012
? Array<DeepDateToString<I>>
1113
: T extends Date
1214
? string
1315
: {
1416
[K in keyof T]: T[K] extends infer I
1517
? I extends Date
1618
? string
17-
: I extends object
18-
? DeepDateToString<I>
19+
: DeepStrictUnbrand<I> extends object
20+
? DeepDateToString<DeepStrictUnbrand<I>>
1921
: I
2022
: never;
2123
};

src/types/GetStrictObjectLastKeys.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { DeepStrictUnbrand } from './DeepStrictUnbrand';
12
import type { IsAny } from './IsAny';
23
import type { IsUnion } from './IsUnion';
34
import type { ValueType } from './ValueType';
@@ -53,8 +54,8 @@ export type DeepStrictObjectLastKeys<
5354
},
5455
P extends keyof T = keyof T,
5556
> =
56-
T extends Array<infer Element>
57+
DeepStrictUnbrand<T> extends Array<infer Element>
5758
? Element extends object
58-
? `${Joiner['array']}.${DeepStrictObjectLastKeys<Element, Joiner>}`
59+
? `${Joiner['array']}.${DeepStrictObjectLastKeys<DeepStrictUnbrand<Element>, Joiner>}`
5960
: `${Joiner['array']}.${keyof Element extends string ? keyof Element : never}`
60-
: __DeepStrictObjectKeys<T, Joiner, P>;
61+
: __DeepStrictObjectKeys<DeepStrictUnbrand<T>, Joiner, Extract<P, keyof DeepStrictUnbrand<T>>>;

test/DeepDateToString.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1+
import { ok } from 'node:assert';
2+
import test from 'node:test';
13
import typia from 'typia';
24
import { DeepDateToString, Equal } from '../src';
3-
import test from 'node:test';
4-
import { ok } from 'node:assert';
55

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

3838
ok(typia.random<Answer>());
3939
});
40+
41+
test('If the prop type of an object is symbol | (string & {}) | Date', () => {
42+
type Question = DeepDateToString<{ prop: symbol | (string & {}) | Date }>;
43+
type Answer = Equal<Question, { prop: symbol | string }>;
44+
45+
ok(typia.random<Answer>());
46+
});

0 commit comments

Comments
 (0)