Skip to content

Commit 301480e

Browse files
authored
Merge pull request #40 from kakasoo/kakasoo/rm-merge-dead-code
refactor: remove dead code in DeepStrictMerge.Infer
2 parents edbee31 + d84a16e commit 301480e

2 files changed

Lines changed: 42 additions & 15 deletions

File tree

src/types/DeepStrictMerge.ts

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,7 @@ namespace DeepStrictMerge {
2929
: Target[key] // If `Target` is not an object, take `Target`'s value
3030
: Target[key] // If `key` is only in `Target`, take `Target`'s value
3131
: key extends keyof Source
32-
? key extends keyof Target
33-
? Source[key] extends object
34-
? Source[key] extends Date
35-
? Target[key] // Date is a leaf type, Target wins
36-
: Target[key] extends object
37-
? Target[key] extends Date
38-
? Target[key] // Target is Date leaf, preserve as-is
39-
: Target[key] extends Array<infer TE extends object>
40-
? Source[key] extends Array<infer PE extends object>
41-
? Array<Infer<TE, PE>> // If both are arrays of objects, merge their elements into a new array
42-
: never // If one is an array and the other is not, merging is not possible
43-
: Infer<Target[key], Source[key]> // If both are objects, merge them recursively
44-
: Source[key] // If `Source` is an object but `Target` is not, take `Source`'s value
45-
: Source[key] // If `Source` is not an object, take `Source`'s value
46-
: Source[key] // If `key` is only in `Source`, take `Source`'s value
32+
? Source[key] // If `key` is only in `Source`, take `Source`'s value
4733
: never; // If `key` is in neither `Target` nor `Source`, return `never`
4834
};
4935
}

test/features/DeepStrictMerge.ts

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,3 +118,44 @@ export function test_types_deep_strict_merge_nested_date() {
118118
type Answer = Equal<Question, { a: { createdAt: Date; b: number; updatedAt: Date; c: string } }>;
119119
ok(typia.random<Answer>());
120120
}
121+
122+
/**
123+
* Tests that a Source-only key with a Date value is taken directly from Source.
124+
* This proves the dead-code branch (re-checking `key extends keyof Target`) was unreachable:
125+
* if `key` is only in Source, the result must be `Source[key]` regardless of type.
126+
*/
127+
export function test_types_deep_strict_merge_source_only_date() {
128+
type Question = DeepStrictMerge<{ a: number }, { createdAt: Date }>;
129+
type Answer = Equal<Question, { a: number; createdAt: Date }>;
130+
ok(typia.random<Answer>());
131+
}
132+
133+
/**
134+
* Tests that a Source-only key with a nested object value is taken directly from Source.
135+
* The dead code attempted to recursively merge Source[key] with Target[key],
136+
* but Target[key] doesn't exist for Source-only keys.
137+
*/
138+
export function test_types_deep_strict_merge_source_only_nested_object() {
139+
type Question = DeepStrictMerge<{ a: number }, { b: { c: string; d: boolean } }>;
140+
type Answer = Equal<Question, { a: number; b: { c: string; d: boolean } }>;
141+
ok(typia.random<Answer>());
142+
}
143+
144+
/**
145+
* Tests that a Source-only key with an array-of-objects value is taken directly from Source.
146+
* The dead code attempted to merge array elements, but that's impossible for Source-only keys.
147+
*/
148+
export function test_types_deep_strict_merge_source_only_array_of_objects() {
149+
type Question = DeepStrictMerge<{ a: number }, { items: { id: number; name: string }[] }>;
150+
type Answer = Equal<Question, { a: number; items: { id: number; name: string }[] }>;
151+
ok(typia.random<Answer>());
152+
}
153+
154+
/**
155+
* Tests that a Source-only key with a deeply nested object is taken as-is from Source.
156+
*/
157+
export function test_types_deep_strict_merge_source_only_deeply_nested_object() {
158+
type Question = DeepStrictMerge<{ x: number }, { y: { z: { w: string; v: Date } } }>;
159+
type Answer = Equal<Question, { x: number; y: { z: { w: string; v: Date } } }>;
160+
ok(typia.random<Answer>());
161+
}

0 commit comments

Comments
 (0)