typescript - 打字稿:得到右手边算术错误
问题描述
当我尝试在 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));
解决方案
因为,Typescript 试图警告你:不要用字符串做一些数学运算?
// computedNode's properties are strings
const computedNode = {
height: '20px',
paddingTop: '8px',
paddingBottom: '8px'
}
const coversContainerHeight = (computedNode.height - (computedNode.paddingTop + computedNode.paddingBottom));
推荐阅读
- pyspark - 将缺少的列添加到 AWS Glue DataFrame
- android - 如何删除导航抽屉?
- python - “NoneType”对象在 Kivy 中没有属性“文本”
- c++ - 在 Botan 上使用 Argon2 生成哈希会导致垃圾数据和 0xC0000005:访问冲突读取位置 0x0000000100000000
- sql - 用于搜索和填充多行的 SQL 服务器查询
- javascript - 为 API 端点构造 json
- cmd - 如何将输出写入文件并在 CMD 窗口上显示相同的输出
- numpy - 使用 numpy 数组输入从 python 方法创建 PySpark UDF,以计算并返回单个浮点值
- html - 如何使用 display flex 使多个元素重叠
- python - Scrapy安装混乱