Strangly behaviour that merged types #377
Answered
by
RebeccaStevens
SettingDust
asked this question in
Q&A
-
/**
* type a = unknown
*/
type a = DeepMergeHKT<Array<{ a: 1 } | { a: 'c', b: 0 }>, DeepMergeMergeFunctionsDefaultURIs, DeepMergeBuiltInMetaData>
/**
* type b = { a: 1 | "c"; b: 0; }
*/
type b = DeepMerged<{ a: 1 } | { a: 'c', b: 0 }> export declare type DeepMerged<T, Ignored = never> = [T] extends [UnknownArrayOrTuple]
? {
[K in KeysOfUnion<T>]: DeepMerged<T[K], Ignored>
}
: [T] extends [Ignored | DefaultIgnore]
? T
: [T] extends [object]
? {
[K in KeysOfUnion<T>]: DeepMerged<GetValue<T, K>, Ignored>
}
: T
|
Beta Was this translation helpful? Give feedback.
Answered by
RebeccaStevens
Jun 15, 2023
Replies: 1 comment 2 replies
-
Sorry for the late reply. I'm not sure I quite follow you. Could you give me a more full example? What's is your expected output type? |
Beta Was this translation helpful? Give feedback.
2 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ahh, I think I know what you're referring to now.
So currently
DeepMergeHKT
needs a tuple in order to work out the output type. This tuple represents the order in which the parameters are passed in.DeepMergeHKT<[{ a: 1 }, { a: "c"; b: 0 }], DeepMergeMergeFunctionsDefaultURIs, DeepMergeBuiltInMetaData>
will evaluate to{a: "c"; b: 0;}
, but if you swap the order of the elements, you'll get{a: 1; b: 0;}
.When an array is passed, it make sense that
{ a: 1 | "c"; b: 0; }
should be the output type rather thanunknown
. I'll look into whether or not this would be an easy change to make to the current implementation.