WebReflection / hyperHTML-Element

An extensible class to define hyperHTML based Custom Elements.
ISC License
202 stars 25 forks source link

Cannot define `get defaultState` with TypeScript #77

Closed abaksha-sc closed 2 years ago

abaksha-sc commented 2 years ago

Getter for defaultState cannot be defined with TypeScript. Example:

class MyComponent extends HyperHTMLElement {
    get defaultState() {
        return { test: 123 };
    }
}

Following error appears:

'defaultState' is defined as a property in class 'HyperHTMLElement<{}>', but is overridden here in 'MyComponent' as an accessor.

I've created a minimal code here:

https://www.typescriptlang.org/play?#code/CYUwxgNghgTiAEkoGdnwBIE8AOIboBUBZAGQFEIQBbEAOwBcAeA+AXngG8BfAPk4Ch4Q5PSj0AlmHhwowAPa0ImeHIBGyPADcQwAIL16McaoCu9EMgBc8EUdoBzANoBdANyChYBbZNh6cmAAKAEp3IUQZc2AQ6005cWAwoTFDYzMQAGEACygHHQyoCAhVKDAAa0DaKBprW3EHABp4bDhNWtTGxBMYGHa7e2DY+MSPeBzaYEoybQZAkDb4abp6Qfg4hKSbUXNrAk2ZeUVlUAAzKBMIegBlbZBd-bpQINX1kfCNa9vAkTE7+AAFWASQrMPgAH3ggUC9Cy4is8BhcKaPx28AIwTYfEBMGBEFBwRew02KMk8FO9RAlWqfzqnTk2Ak3gA-NYKNRlgAREAneriRm0ADyDPE3kJG34XH4-CQqDRFno8BAAA9zBM0FhcPhiORKDQGAJwvB7CAFadzpcbr8QgbDeE4PRurROAj5dYAIwAJgAzPAuJtfRKgA

This is because defaultState is defined as just readonly property when it should be described as getter:

declare class HyperHTMLElement<T = {}> {
   ...
   readonly defaultState: T;
}

The solution is to change definition of defaultState to something like this:

declare class HyperHTMLElement<T = {}> {
   ...
   get defaultState(): T {}
}
abaksha-sc commented 2 years ago

Fixed by PR: https://github.com/WebReflection/hyperHTML-Element/pull/78