#Record<string, SomeMetadata> | [] => Record<string, SomeMetadata>

25 messages ยท Page 1 of 1 (latest)

slim oyster
#

Ok so I'm calling a 3rd party API and get some weirdly shaped data like this

{
  "fixedKey1": MyRecord,
  "fixedKey2": MyRecord
}

where

type MyRecord = Record<string, SomeMetadata> | []`

and I'm looking for a way to merge both fixedKey1 and fixedKey2 into an object of type Record<string, SomeMetadata>, or even just an array of objects consisting of some values contained in SomeMetadata. Any ideas?

#

I can't find a way to iterate over the keys of MyRecord because of that empty array

#

I feel like this shouldn't be so hard but i'm hitting a wall

slim oyster
#

Ohhh wait I can probably use Object.entries to convert both into an array

#

Yeah that works, but I kinda want to keep the map/dictionary instead of having to iterate through that array to look something up

#

I tried something like {...obj.fixedKey1, ...obj.fixedKey1 } but end up with Array properties like length

stark geyserBOT
#
ascor8522#0

Preview:```ts
////////// INPUT
declare interface MyAPI {
"a": MyFixedKeyValue,
"b": MyFixedKeyValue,
}

declare type MyFixedKeyValue =
| Record<string, SomeMetadata>
| unknown[];

declare interface SomeMetadata {
e: string;
f: number;
}

////////// OUTPUT
interface FinalResult {
...```

brazen fulcrum
#

@slim oyster This isn't something TS-related really

#

it's not something that complex in JS either, simply manipulating objects and arrays at the same time

#

the few things that make it a bit harder is that you have nested objects, up to a depth of 3

#

also, you didn't really explain what you wanted the final object to look like

#

so if you haven't figure out that part yet, I'd recommend you to think about that first, before trying to transform the object

slim oyster
#

Thanks, I appreciate you taking the time to answer

#

And yeah sorry, i know my question wasn't really comprehensible, it was late and I was running out of steam ๐Ÿ˜…

#

I modified your code a bit to reflect the situation a bit better and also the output I was going for

stark geyserBOT
slim oyster
#

I want to merge the contents of "a", "b" and "c", and logically I would be able to do this in JS, but was having troubles coming up with correct type definitions. The compiler complained about some array properties not being present or something of that sort

#

@brazen fulcrum i'd be very thankful if you could look at this again, i'm really interested in any kind of insight you could provide

brazen fulcrum
#

so you want to ignore the arrays and not merge those, right?

#

and do you always want to extract the r property on the SomeMetadata type?

#

@slim oyster

slim oyster
#

exactly

brazen fulcrum
#

@slim oyster

stark geyserBOT
#
ascor8522#0

Preview:```ts
////////// INPUT
declare interface MyAPI {
"a": MyFixedKeyValue,
"b": MyFixedKeyValue,
"c": MyFixedKeyValue,
}

declare type MyFixedKeyValue =
| Record<string, SomeMetadata>
| [];

declare interface SomeMetadata {
r: string;
s: number;
t: string;
...```

slim oyster
#

Thanks a lot, i'm going to throw that into WebStorm later today and try and understand what you did there ๐Ÿ™‚