Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Typescript - create type of all available css properties

I'm using React and Styled Components and I have a function that accepts a string of a css attribute. I want to type it to validate that the string is a valid css attribute (color, background-color, etc...)

function doSomething(cssPropertyName: SomeType) {
}

doSomething('border-color'); // No error
doSomething('non-existing-css-property'); // Should give an error

How can I achieve this?
Thanks!

like image 301
Uri Klar Avatar asked Nov 02 '25 08:11

Uri Klar


1 Answers

With: https://github.com/frenic/csstype I think you can simply use:

function doSomething(prop: keyof CSSProperties) {
}

doSomething('borderColor'); // No error
doSomething('non-existing-css-property'); // Should give an error
like image 165
Yoshi Avatar answered Nov 03 '25 23:11

Yoshi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!