I have an item that looks like this:
const obj = [
{ link: "/home", title: "Home1" },
{ link: "/about", title: "About2" },
{ link: "/contact", title: "Contact1" }
] as const
and I want to produce items in the following format:
const menu: MenuItems = {
"/home": [{ link: "/home", title: "Home2" }] // can also include Home1, but not Contact1
}
Is it achievable to create a type for this? Here's my approach to clarify my intent:
type Obj = typeof obj;
type Item = Obj[number]
type MenuItems = {
[K in Item["link"]]: Array<Item extends {link: K} ? Item : never>;
}
codesandbox: https://codesandbox.io/s/56564