作者:树阿两 | 来源:互联网 | 2022-12-06 18:01
我想这样定义jsx:
并且我在CSS中使用--length,也有一些单元格具有--count,它们使用css伪选择器(使用Counter 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变量(自定义属性),或者是否可以对样式对象进行强制?
1> 小智..:
像这样:
render(){
var style = { "--my-css-var": 10 } as React.CSSProperties;
return ...
}
我认为这是我在接受答案的示例之后得出的结论,但是直接答案总是更好。
2> Matt McCutch..:
如果转到的定义CSSProperties
,则会看到:
export interface CSSProperties extends CSS.Properties {
/**
* The index signature was removed to enable closed typing for style
* using CSSType. You're able to use type assertion or module augmentation
* to add properties or an index signature of your own.
*
* For examples and more information, visit:
* https://github.com/frenic/csstype#what-should-i-do-when-i-get-type-errors
*/
}
该链接提供了如何通过增加Properties
in 的定义csstype
或将属性名称强制转换为来解决类型错误的示例any
。