Here's how I define and use my type in my react app:
FontSize.ts
const fontSize = {
xs: 'xs',
sm: 'sm',
base: 'base',
lg: 'lg',
xl: 'xl'
}
export default Object.freeze(fontSize);
And this is my simple Text component:
Text.tsx
import { FontSize } from '@bl/foundation';
interface TextProps {
size?: keyof typeof FontSize,
children: React.ReactNode;
}
const Text:React.FC<TextProps> = ({ size = FontSize.sm, children }) => {
const classNames = `font-size-${size}`;
return <p className={classNames}>
{children}
</p>
}
when I write my component:
<Text size={FontSize.lg} > Some text here. </Text>
It show me in the size property this error:
Type 'string' is not assignable to type '"xs" | "sm" | "lg" | "xl" | "base" | undefined'.