如何使用 Lit 在 Typescript 中扩展 Shoelace Web 组件

发布于 2025-01-16 05:13:08 字数 906 浏览 3 评论 0原文

在 Lit 中扩展 组件后,我没有收到错误属性的 Typescript 错误正在通过。例如,在下面的代码片段中,当我使用错误的属性调用 时,打字稿会显示错误,但如果我调用 属性错误的打字稿不会显示任何错误。

import SlButton from '@shoelace-style/shoelace/dist/components/button/button.js';
import {html} from 'lit';
import {customElement} from 'lit/decorators.js';

@customElement('my-button')
export default class MyButton extends SlButton {
  constructor() {
    super();
  }
}

export const Demo = () =>
  html`
    <my-button size="not-exists">Click here!</my-button> // shows no error on "size"
    <sl-button size="not-exists">Click here!</sl-button> // shows error on "size"
  `;

declare global {
  interface HTMLElementTagNameMap {
    'my-button': MyButton;
  }
}

After extending <sl-button> component in Lit, I am not getting Typescript errors for the wrong attributes being passed. For example, in the code snippet below, when I call <sl-button> with the wrong attribute typescript shows errors, but if I call <my-button> with the wrong attribute typescript doesn't show any error.

import SlButton from '@shoelace-style/shoelace/dist/components/button/button.js';
import {html} from 'lit';
import {customElement} from 'lit/decorators.js';

@customElement('my-button')
export default class MyButton extends SlButton {
  constructor() {
    super();
  }
}

export const Demo = () =>
  html`
    <my-button size="not-exists">Click here!</my-button> // shows no error on "size"
    <sl-button size="not-exists">Click here!</sl-button> // shows error on "size"
  `;

declare global {
  interface HTMLElementTagNameMap {
    'my-button': MyButton;
  }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱给你人给你 2025-01-23 05:13:08

这似乎按预期工作。我相信您正在寻找的不是 TypeScript 错误,因为 TS 不关心 HTML 属性,而是编辑器的代码完成。

我怀疑您正在使用 VS Code 以及以下提供的自定义数据鞋带。这就是告诉 VS Code 有关已注册 Shoelace 元素的信息,如果您打开 dist/vscode.html-custom-data.json 您将看到每个标签和可用属性列表。

如果您要扩展和重新标记 Shoelace 元素,则需要向 VS Code 提供您自己的自定义数据。


有几点需要注意:

首先,当您导入 components/button/button.js 时,您仍然在注册 Shoelace 按钮。事实上,您的代码正在注册 因为注册是在导入时发生的。

其次,Shoelace 组件并不是为了重命名而构建的。许多东西依赖于标签名称来工作,例如 期望子元素为 < ;sl-tab-group> 需要 子级。重命名它们会以意想不到的方式破坏事情。

This appears to be working as expected. I believe what you're looking for isn't a TypeScript error, as TS doesn't care about HTML attributes, but code completion for your editor.

I suspect you're using VS Code with the custom data provided by Shoelace. This is what tells VS Code about registered Shoelace elements, and if you open up dist/vscode.html-custom-data.json you'll see each tag and a list of available attributes.

If you're extending and re-tagging Shoelace elements, you'll need to provide your own custom data to VS Code.


A couple notes:

First, when you import components/button/button.js, you're still registering a Shoelace button. In fact, your code is registering both <sl-button> and <my-button> because registration happens on import.

Second, Shoelace components aren't built to be renamed. Many rely on tag names for things to work, such as <sl-menu> which expects children to be <sl-menu-item> and <sl-tab-group> which expects <sl-tab> and <sl-tab-panel> children. Renaming them will break things in unexpected ways.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文