I'm trying to declare types in TSX. What I would like to have is that if user adds one prop to a component, they can't put the other prop. This works, but I'm using these props in mostly all my components, so I'd like not to repeat this code.
I tried to extend one of my component interface with this type, but I get this error:
An interface can only extend an object type or intersection of object types with statically known members.
How could I do?
This is my code:
interface PossibleColors1 {
color?: AllColorsTypes // List of colors
customColor?: never
}
interface PossibleColors2 {
color?: never
customColor?: CustomColorTypes // Any other color
}
type PossibleColorsTypes = PossibleColors1 | PossibleColors2
interface Props extends PossibleColorsTypes { // This is the line not working
}
Thanks for your answers!
CodePudding user response:
Intersect PossibleColorsTypes
with another type instead of extending it with an interface:
interface PossibleColors1 {
color?: AllColorsTypes // List of colors
customColor?: never
}
interface PossibleColors2 {
color?: never
customColor?: CustomColorTypes // Any other color
}
type PossibleColorsTypes = PossibleColors1 | PossibleColors2
type Props = PossibleColorsTypes & { myNewProperty: "yay" };