So, I have the following array:
const users = [
{ fname: "Samuel", lname: "Mathew", username: "sammt" },
{ fname: "Carlos", lname: "King", username: "kingg" },
] as const
And I want to extract the following type out of it:
"Samuel Mathew" | "Carlos King"
I tried the following but I don't understand why this doesn't work:
type TUserFullnames = {
[k in number]: `${(typeof users)[k]["fname"]} ${(typeof users)[k]["lname"]}`;
}[number];
It gives me an union of all possible fname
lname
combinations, which is something I wasn't expecting.
Union of all possible types is understandable if I do this:
type TUserFullnames = `${(typeof users)[number]["fname"]} ${(typeof users)[number]["lname"]}`
But not sure why it yields the same result when mapping over number
.
It works perfectly with an object though:
const users = {
"sammt": { fname: "Samuel", lname: "Mathew" },
"kingg": { fname: "Richie", lname: "Rich" },
} as const
type TUserFullnames = {
[k in keyof typeof users]: `${(typeof users)[k]["fname"]} ${(typeof users)[k]["lname"]}`;
}[keyof typeof users];
Can someone help me this??