If the CSSRule.type property is deprecated what can I used instead

Viewed 67

The CSSRule docs in MDN states that the CSSRule.type property is deprecated and is not clear to me what should be used instead if one wants to check the type of CSSRule. Adding some code for reference.

const isStyleOrImportRule = (rule: CSSRule): boolean => {
  // 1 -> CSSRule.STYLE_RULE
  // 3 -> CSSRule.IMPORT_RULE   
  return  [1,3].includes(rule.type)
}
1 Answers

There is a better way to do this and avoiding deprecation issues referencing the constructor function name.

const isStyleOrImportRule = (rule: CSSRule): boolean => {
  return ['CSSStyleRule', 'CSSImportRule'].includes(rule.constructor.name)
}
Related