2021-07-20

CSS variable is not assinable in TypeScript React

css, typescript, react, selfnote

banner

Image by Niek Verlaan from Pixabay

When you use React with TypeScript, you have have seen an error like this.

not assinable react cssproperties

Type '{ "--language-color": string; }' is not assignable to type 'Properties<string | number, string & {}>'. Object literal may only specify known properties, and '"--language-color"' does not exist in type 'Properties<string | number, string & >'. The expected type comes from property 'style' which is declared here on type 'DetailedHTMLProps<HTMLAttributes<HTMLElement>, HTMLElement>'

Cast it to React.CSSProperties.

1<i2  style={{ "--language-color": item.primaryLanguage.color } as React.CSSProperties}3  className={`w-4 h-4 rounded-full bg-[var(--language-color)]`}4/>