5 ms·
> You could fix it by eg. `$state(undefined as unknown as HTMLElement)` but that's dumb I think the generic is what you are looking for class Wrapper {
by beremaki 2y ago
> You could fix it by eg. `$state(undefined as unknown as HTMLElement)` but that's dumb
I think the generic is what you are looking for
class Wrapper {
dom = $state<HTMLElement>();
constructor(el: HTMLElement) {
this.dom = el;
}
}
the dom property will still be HTMLElement | undefined, if the 'undefined' bothers you have to add an exclamation mark and write "$state<HTMLElement>()!"
- Etheryte 2y agoThat's still a manual type assertion though, and if a regular usage pattern demands one, then the library is doing it wrong. Regardless of how you annotate it, every manual override reduces the effectiveness of the type system.