首页 > 解决方案 > 打字稿:得到右手边算术错误

问题描述

当我尝试在 TS 中进行简单算术运算时,我收到一条错误消息:

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

我不确定我在这里做错了什么,因为它似乎是基本的算术。我认为它是正确的类型,因为我将它设置为 CSSStyleDecleration:

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

标签: typescriptvue.js

解决方案


因为,Typescript 试图警告你:不要用字符串做一些数学运算?

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

推荐阅读