【发布时间】:2018-07-05 03:36:45
【问题描述】:
test.tsx
<img onerror="this.style.display='none'" height="178" cmd="start" />
产量
error TS2339: Property 'onerror' does not exist on type 'HTMLAttributes'.
所以我在 JSX 部分上方的 test.tsx 中添加:
namespace JSX {
interface HTMLAttributes {
onerror?: any; // 1. attempt: add the missing onerror attribute
}
interface IntrinsicElements {
img: any // 2. attempt: generally override the type of img, allowing anything
}
}
但是没有效果。嗯?
如何在本地向我要使用的 JSX 代码添加属性?
我知道我可以粗暴地破解导入的类型文件,但我想知道是否有本地方法。
编辑: 除了 onerror 属性(即 preact.d.ts 中“错误地”缺失)之外,我通常想知道如何将临时属性添加到内在元素甚至我自己的元素。奇怪的是,打字稿从不抱怨“data-*”属性,我也可能会切换(无论如何都想成为一个不错的 html5 开发人员)。但是关于扩展接口 HTMLAttributes 的问题还是悬而未决。
【问题讨论】:
-
顺便说一句,如果你不给某人加标签或评论他的回答,那么他将不会收到通知。
标签: typescript jsx preact tsx