I have the following code snippet:
type Color = string;
interface Props {
color: Color;
text: string;
}
function Badge(props: Props) {
return `<div style="color:${props.color}">${props.text}</div>`;
}
var badge = Badge({
color: '#F00',
text: 'Danger'
});
console.log(badge);
Playground
I am trying to get a build error if the color is not valid, for example:
var badge = Badge({
color: 'rgba(100, 100, 100)',
text: 'Danger'
});
Is there a way to define Color
so that it only allows strings matching one of the following patterns?
#FFF
#FFFFFF
rgb(5, 5, 5)
rgba(5, 5, 5, 1)
hsa(5, 5, 5)
I understand that there are such colors as red
and white
, but this can make the answer difficult if I Color
can accept them.
source
share