繁体   English   中英

如何确保在 Typescript 中正确输入了我的 Lit 元素组件道具

[英]How do I make sure that my Lit element component props are properly typed in Typescript

我有一个库,我想在其中为所有不同的框架创建一个按钮。 所有这些组件道具都应该具有来自 Typescript 接口的单一事实来源:

interface BaseButton {
    tiny: boolean;
    color: string;
}

如何确保根据该界面正确键入我的 Lit 元素属性?

@customElement("my-button")
export class MyButton extends LitElement {
    @property({ type: Boolean })
    tiny = true;

    @property({ type: String })
    color = "red";
    ....
}

只需实现接口。

一般示例:

interface MyInterface {
  foo: boolean;
  bar: string;
}

// Invalid
class MyClass implements MyInterface {
  // TS2416
  foo: string;
  bar: boolean;
}

// Valid
class MyClass implements MyInterface {
  foo: boolean;
  bar: string;
}

你的代码:

export class MyButton extends LitElement implements BaseButton {...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM