I have two sets of string values that I want to map from one to the other as a constant object. I want to generate two types from that mapping: one for keys and one for val
I know that it may not be related, but for my usecase, I reached this question because I wanted to create a type based on an object or array. So I just thought it may be useful for someone with the same usecase reaching this question to use enums: You can simply define an enum like this:
enum Arrow {
Up,
Down,
Left,
Right
}
You can read more about them here and here.
You can now use this enum as a type:
type Props = {
arrow: Arrow
}
const Component = (props: Props) => {
switch(props.arrow) {
case Arrow.Up:
// go-up
case Arrow.Down:
// go-down
...
}
}
and you can use it in your components: