【发布时间】:2019-01-30 23:18:51
【问题描述】:
我想这样定义 jsx:
<table style={{'--length': array.lenght}}>
<tbody>
<tr>{array}</tr>
</tbody>
</table>
我在 CSS 中使用 --length,我也有单元格有 --count 使用 CSS 伪选择器(使用计数器 hack)显示计数。
但是打字稿会抛出错误:
TS2326: Types of property 'style' are incompatible.
Type '{ '--length': number; }' is not assignable to type 'CSSProperties'.
Object literal may only specify known properties, and ''--length'' does not exist in type 'CSSProperties'.
是否可以更改样式属性的类型以接受 CSS 变量(自定义属性),或者有没有办法强制任何样式对象?
【问题讨论】:
-
为什么要css变量?因为您不能在样式标签中使用 CSS 变量。它只接受 CSS 属性。你可以创建一个像
const length = array.length这样的变量并在 css 中使用length? -
@SGhaleb 我已经看到了这一点,在我的代码中,css 变量有效,它们出现在 DOM 中并且应用了 css,但是在构建应用程序时它们在 webpack 中给出错误(看起来像错误但编译通过) ,所以这是打字稿打字的问题而不是反应。
-
@KyawSiesein 他们对 js 变量的问题是你不能在
::before和::after中使用它们。
标签: reactjs typescript jsx tsx