如何使用 Lit 在 Typescript 中扩展 Shoelace Web 组件
在 Lit 中扩展
时,打字稿会显示错误,但如果我调用
属性错误的打字稿不会显示任何错误。
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎按预期工作。我相信您正在寻找的不是 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.