Assigning css style in type script using setAttribute

I am trying to assign element’s attributes in typescript using setAttribute like this w3school example

Here is my JavaScript:

const p = document.createElement('p');
p.setAttribute('class', 'demoClass');
p.innerText = 'Hello';

and .css:

.demoClass {
  background-color: #00ffff !important;  
}

But this way it doesn’t take the style.
The only solution I’ve found is the following:

p.setAttribute('style', 'background-color: #00ffff !important');

Does anyone know how I can give the style using the class and not directly assigning it? Thank you in advance.