我在用着自定义元素 https://developer.mozilla.org/en-US/docs/Web/Web_Components/Using_custom_elements又称为网络组件Preact https://preactjs.com/。问题是 Typescript 抱怨元素没有被定义在JSX.IntrinsicElements
- 在这种情况下check-box
元素:
<div className={styles.option}>
<check-box checked={this.prefersDarkTheme} ref={this.svgOptions.darkTheme}/>
<p>Dark theme</p>
</div>
错误信息(省略路径):
ERROR in MyComponent.tsx
[tsl] ERROR in MyComponent.tsx(50,29)
TS2339: Property 'check-box' does not exist on type 'JSX.IntrinsicElements'.
我遇到了以下可能的解决方案,不幸的是不起作用:
-
https://stackoverflow.com/a/57449556/7664765 https://stackoverflow.com/a/57449556/7664765 - 这是一个与问题无关的答案,但它涵盖了我的问题
我尝试将以下内容添加到我的typings.d.ts
file:
import * as Preact from 'preact';
declare global {
namespace JSX {
interface IntrinsicElements {
'check-box': any; // The 'any' just for testing purposes
}
}
}
我的 IDE 将导入部分灰显并IntrinsicElements
这意味着它没有被使用(?!)并且它无论如何也不起作用。我仍然收到相同的错误消息。
-
https://stackoverflow.com/a/55424778/7664765 https://stackoverflow.com/a/55424778/7664765 - 同样对于react,我尝试将其“转换”为preact,并且得到了与1相同的结果。
我什至找到了一个file https://github.com/GoogleChromeLabs/squoosh/blob/master/src/custom-els/RangeInput/missing-types.d.ts由 google 维护squoosh https://github.com/GoogleChromeLabs/squoosh他们在项目中执行了以下操作来“填充”支持:
与组件 a 位于同一文件夹中missing-types.d.ts
文件包含以下内容,基本上与我的设置相同,但带有index.ts
文件而不是check-bock.ts
他们正在使用旧的 TS 版本v3.5.3
:
declare namespace JSX {
interface IntrinsicElements {
'range-input': HTMLAttributes;
}
}
我假设他们的构建没有失败,那么它是如何工作的以及如何正确定义自定义元素以在 preact/react 组件中使用它们?
我目前正在使用[email protected] /cdn-cgi/l/email-protection
and [email protected] /cdn-cgi/l/email-protection
.