I am working on a function that takes an element from the page and adds CSS styles to its attribute. The argument passed to this function should ideally be an object with keys like height
, minWidth
, flexDirection
, and so on.
function addStyle (el: HTMLElement, style: Style): void {
for (const property in style) {
el.style[property] = style[property]
}
}
My issue lies in defining the type for this object. It's not practical to manually list every CSS property. I believe TypeScript should have a way to handle this automatically, but I'm unsure how to do it. This is my best attempt:
type Style = Partial<CSSStyleDeclaration>
...yet I encounter the error "
Type 'string | undefined' is not assignable to type 'string'
": TypeScript playground
This particular error can be addressed, but I'm concerned that it may indicate a mistake or confusion on my part.
Is there a standard method to define the type for an object containing CSS properties?