I don't know if this is really possible, but I have an existing object that maps string keys to React components.
I want to create a type to essentially make a parent component only accept keys of the icon mapping object as a prop:
export const icons: { readonly [key: string]: React.VFC } = Object.freeze({})
type IconProps = {
name: // every key of `icons`
}
export const Icon: React.VFC = ({ name }) => {
return icons[name]({})
}
Again, I don't know if this even is possible, but it'd be super awesome if it was.
Also, if you have suggestion for a different title to make it more searchable and descriptive of what I'm actually trying to accomplish, I'd love to update it.
Object.freeze({})here? You have ensured thaticonshas no properties at all. Assuming you don't wanttype IconProps = {name: never}I think it would useful if you could modify the code so as to constitute a minimal reproducible example .Good luck.keyof. But agree with @jcalz that we need a minimal example in order to help.iconswould be the object that’d map a string key to a react component. for exampleconst icons = Object.freeze({ Add: () => /* svg /* }). the object is frozen such that it’s immutable to consumers