Say I have a type test
:
type test = {
a: number,
b: number,
c: number,
d: number,
}
How would I create a generic type that will extract a subset of test
up to a specified key in the original order of the object, not including said key? For example:
type OrderedExtraction<T extends Object, K extends keyof T> = {.....?}
type subset = OrderedExtraction<test, "c">
> subset = {a: number, b: number}
I'm aware that ECMAScript technically makes no guarantees about the order of object keys (which would make this nonsensical), but unofficially V8 at least does seem to guarantee it.
Normally, one might do this using Omit
, Pick
, or Extract
, but those won't work in this case unless a union of keys up to the specified one can be generated somehow. I suspect recursive types might be the way, but I'm not sure.