How to transform type of object with TS so that the values are of same type but wrapped in Array

Can someone pls help with this issue I'm having. I have a type like so:
const myZodType = z.object({ name: z.string(), ...})
type myType = z.TypeOf<typeof myZodType>

--> myType is { name: string, ... }
const myZodType = z.object({ name: z.string(), ...})
type myType = z.TypeOf<typeof myZodType>

--> myType is { name: string, ... }
I'm doing some processing with formidable which then turns an object of myType into this:
// Returned from formidable
const res = {
name: ["some name"],
...
}
// Returned from formidable
const res = {
name: ["some name"],
...
}
I want to extract that type. Which basically means, take the type myType or myZodType as it is and turn it into an object that has the same keys but the types of values are now wrapped in an array. I've done this for now, but don't know how to continue...
type transformObjectPropsToArray<T extends { [key: string]: T[string] }> = Extract<T[string], any>;

--> I want a inferred type like this:
type newType = {
name: Array<{name: string}>,
...
}
type transformObjectPropsToArray<T extends { [key: string]: T[string] }> = Extract<T[string], any>;

--> I want a inferred type like this:
type newType = {
name: Array<{name: string}>,
...
}
I'm kind of confident about this last one, don't know about the any type though...
6 Replies
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
JulieCezar
JulieCezar•2y ago
UPDATE! Found out about the Record utility type which does what I need.
type transformObjectPropsToArray<T extends { [key: string]: unknown }> = Record<
keyof T,
Array<T[string]>
>;
type transformObjectPropsToArray<T extends { [key: string]: unknown }> = Record<
keyof T,
Array<T[string]>
>;
Which then turns my new object like so:
type myNewType = transformObjectPropsToArray<myType>;
type myNewType = transformObjectPropsToArray<myType>;
Into:
type myNewType = {
name: unknown[];
}
type myNewType = {
name: unknown[];
}
Just seen your reply, yes it works, TY!
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
JulieCezar
JulieCezar•2y ago
Just realized it's not quite right... Because it doesn't take each of the properties and puts that type in array. It makes the values be of any type of all the property types e.g.
type formData = {
name: string;
surname: string[];
phone: number;
};
type formData = {
name: string;
surname: string[];
phone: number;
};
becomes
type x = {
name: [string | number | string[]];
surname: [string | number | string[]];
phone: [string | number | string[]];
}
type x = {
name: [string | number | string[]];
surname: [string | number | string[]];
phone: [string | number | string[]];
}
instead of
type x = {
name: [string];
surname: [string[]];
phone: [number];
}
type x = {
name: [string];
surname: [string[]];
phone: [number];
}
Unknown User
Unknown User•2y ago
Message Not Public
Sign In & Join Server To View
JulieCezar
JulieCezar•2y ago
Perfect, tyvm 😄
Want results from more Discord servers?
Add your server