在 TypeScript 中覆盖 Lit-Element 子类的属性类型

问题描述 投票:0回答:1

当对 lit-element 类进行子类化以添加更多类型时,是否应该重写

@property
装饰器还是仅重写类型和初始值设定项?换句话说,假设我有这个代码:

interface AB {
   a: number,
   b: string,
}

@customElement('my-parent')
class MyParent extends LitElement {
    @property({type: Array}) stuff: readonly any[] = [];
}

以下哪项是正确的子类化方式:

@customElement('my-child')
class MyChild extends MyParent {
    override @property({type: Array}) stuff: readonly Readonly<AB>[] = [];
}

@customElement('my-child')
class MyChild extends MyParent {
    override stuff: readonly Readonly<AB>[] = [];
}

两者似乎都在我的代码库中工作,所以我不确定要标准化到哪一个。

typescript ecmascript-6 reactive-programming subclass lit-element
1个回答
0
投票

奇怪的是,你在只读的东西上标记

@property
,因为
@property
的目的是让它反应性地触发设置更新,这只读意味着你不会这样做。

无论如何,在没有

@property
的情况下覆盖子类中的类属性将删除反应性。

© www.soinside.com 2019 - 2024. All rights reserved.