I’m trying out stimulus for on of my applications. This one still has a lot of dependency on jquery, so it will be nice to finally be able to remove that from the bundle. The application is written in typescript, since stimulus is also written in typescript this shoudln’t be too difficult.
One of the things I’m having trouble with is declaring this.element. Since this.element is defined as a getter in the parent class, the typescript compiler doesn’t want me to overwrite that declaration.
When one of my controllers depends on a specific HTML Element, I currently always have to cast it to that element.
For example:
export default class extends Controller {
get value(): string {
return (this.element as HTMLInputElement).value;
}
}
export default class extends Controller {
element: HTMLButtonElement;
}
Which ends in:
TS2564: Property 'element' has no initializer and is not definitely assigned in the constructor.
TS2610: 'element' is defined as an accessor in class 'Controller', but is overridden here in 'default' as an instance property.
By changing to:
export default class extends Controller {
declare element: HTMLButtonElement;
}
I still get then this one:
TS2610: 'element' is defined as an accessor in class 'Controller', but is overridden here in 'default' as an instance property.
If I try to use:
export default class extends Controller {
declare get element(): HTMLButtonElement;
}
It ends in this error:
TS1031: 'declare' modifier cannot appear on class elements of this kind.