#Convert array of objects to Record with keys being values of keys from passed objects

20 messages · Page 1 of 1 (latest)

deft minnow
#

Hello, I'm trying to write a function that would convert an array of objects, like [{one: 'eno'}, {two: 'owt'}] to {eno: {...}, owt: {...}}.

So far I've come up with this snippet but I'm not sure where to go next. I need makeSomethingFromObjects function to receive an array of objects with specific type and only return object with keys of the objects it received. I hope I'm making sense, basically if function can receive an array of 3 different objects and only 2 objects are passed, the return value should also contain (and hint) for only the 2 objects received.

Also it would be nice if I could make sure passed array cannot have duplicates of those objects, so if {one: 'eno'} is in array it cannot be passed again. Is that somehow possible?

full lotusBOT
#
asasinmode#0058

Preview:ts ... type ReturnValue = { [K in T extends ReadonlyArray< infer U extends PassedObject > ? U["name"] : never]: TransformedObject } ...

floral hinge
#

@deft minnow Pretty sure there isn't a way to prevent duplicates in an array. This whole approach is going to be tricky and a bit unsafe because an array type says what an array can hold, not what it must hold.

deft minnow
#

Thanks, thought so

floral hinge
#

e.g. you'd probably have an issue with something like:

const arr: Array<{x: string} | {y: number}> = []; // valid, arrays can be empty
const obj = makeSomethingFromObjects(arr); // TS might assume this is `{ x: string, y: number }`, but it isn't.
#

I guess theoretically if you wrap your output in Partial (assume all properties are optional) that would be safe against that issue. And maybe there's some trick with tuples to avoid the issue.

#

This might not be a dealbreaker, I guess, could still be a useful API even if unsafe, but I guess I would consider if there's some other approach here that might work better.

deft minnow
#

It's alright, that was just a nice to have. Thanks for answering

#

Would you happen to know how I can fix my snippet? Atm it doesn't like how I set my return object's keys

floral hinge
#

I haven't looked too carefully, but it's very likely you're going to need unsafe casting to make this compile.

#

Looks like type ReturnValue = Record<T[number]["name"], number>; would work for the return value though

deft minnow
#

Unfortunately it seems to be returning the same error ('one' does not exist on type)

full lotusBOT
#
Retsam19#2505

Preview:ts ... type ReturnValue = Record<T[number]["name"], number> ...

floral hinge
#

^ I'm seeing the usage work correctly

deft minnow
#

What about the squigglie in the for loop (line 16)

floral hinge
#

That's what I mean by 'you're going to need unsafe casting to make this compile'

deft minnow
#

Oh alright, I see now. Tyvm for help

floral hinge
#

e.g.

transformedObjects[thing.name as never] = {anotherProperty: `${thing.someProperty}`} as never
deft minnow
#

If it's unavoidable then 🤷