我想这样定义jsx:

<table style={{'--length': array.lenght}}>
   <tbody>
      <tr>{array}</tr>
   </tbody>
</table>

我在CSS中使用了--length,我还有一个单元格,有--count,它使用CSS伪 Select 器(使用计数器hack)显示count.

但是typescript抛出了错误:

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变量(自定义属性),或者是否有方法强制样式对象使用任何样式属性?

推荐答案

如果你go definition of CSSProperties,你会看到:

export interface CSSProperties extends CSS.Properties<string | number> {
    /**
     * 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
     */
}

该链接给出了如何通过在csstype中增加Properties的定义或将属性名强制转换为any来解决类型错误的示例.

Typescript相关问答推荐

如何修复VueJS中的val类型不能赋给函数

ReturnType此索引方法与点表示法之间的差异

如何访问Content UI的DatePicker/中的sx props of year picker?'<>

对数组或REST参数中的多个函数的S参数进行类型判断

具有泛型类型和缺省值的键

TypeScrip原始字符串没有方法

限制返回联合的TS函数的返回类型

MatTool提示在角垫工作台中不起作用

Fp-ts使用任务的最佳方法包装选项

类型脚本-在调用期间解析函数参数类型

如何在Angular /字体中访问API响应的子元素?

将类型脚本函数返回类型提取到VSCode中的接口

两个名称不同的相同打字界面-如何使其干燥/避免重复?

如何调整项目数组,但允许权重影响顺序

如何将对象的字符串文字属性用作同一对象中的键类型

Next 13 + React 18 createContext 类型的构建问题

如果父级被删除或删除,如何自动删除子级?

React 中如何比较两个对象数组并获取不同的值?

将 Angular 从 14 升级到 16 后出现环境变量注入问题

如何从 Select 元素中删除选项