Home > OS >  Typescript: getting right-hand side arithmetic error
Typescript: getting right-hand side arithmetic error

Time:10-04

I am getting an error stating the following when I attempt to do simple arithmetic in TS:

The right-hand side of an arithmetic operation must be of type 'any', 'number', 'bigint' or an enum type.

I am not sure what I am doing incorrect here as it seems to be basic arithmetic. I'd assume it's the correct type since I set it to CSSStyleDecleration:

  const computedNode = window.getComputedStyle(coversContainer) as CSSStyleDeclaration;
  const coversContainerHeight = (computedNode.height - (computedNode.paddingTop   computedNode.paddingBottom));

CodePudding user response:

Because, Typescript try to warn you: Do not do some math with strings?

  // computedNode's properties are strings
  const computedNode = {
    height: '20px',
    paddingTop: '8px', 
    paddingBottom: '8px'
  }
  
  const coversContainerHeight = (computedNode.height - (computedNode.paddingTop   computedNode.paddingBottom));
  • Related