feat: generate strict types for addEventListener and removeEventListener for components
Prerequisites
- [X] I have read the Contributing Guidelines.
- [X] I agree to follow the Code of Conduct.
- [X] I have searched for existing issues that already include this feature request, without success.
Describe the Feature Request
This feature would give type safety when using addEventListener or removeEventListener. This would improve the developer experience of TypeScript developers.
This would also make consuming Stencil components in an Angular project with or without using the Angular build target manageable with strictTemplates flag set to true.
Describe the Use Case
This feature would allow developers to use Stencil components in a TypeScript project where using addEventListener and removeEventListener would use proper types and would eliminate the need always to use type assertion, create custom types, or overloads to each component's html element type and define them ourselves in HTMLElementTagNameMap.
Consider the following:
import { Component, Event, EventEmitter, Prop, h } from '@stencil/core';
@Component(/* ... */)
export class MyComponent {
// Add an event `example`:
@Event() example!: EventEmitter<number>;
// ...
}
// HTMLMyComponentElement
const myComponent = document.createElement('my-component');
myComponent.addEventListener('example', (event) => {
// Property 'detail' does not exist on type 'Event'.ts(2339)
console.log(event.detail);
});
This type for the event callback shouldn't be Event but instead MyComponentCustomEvent<number>.
This issue makes working with Stencil in an Angular project very difficult since there are less options and is unclear to influence this behavior in an Angular template:
Describe Preferred Solution
Stencil should provide types to Stencil component's html elements such that using addEventListener and removeEventListener have specific types instead of just Event, which is typically wrong since most events are likely a CustomEvent of some kind.
Since there are no overloads for each component's html element, they all fallback to HTMLElement for addEventListener and removeEventListener:
interface HTMLElement {
// ...
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
If Stencil generated these overloads, there wouldn't be any extra steps needed to use addEventListener and removeEventListener:
/** demo/src/components.d.ts */
// ...
export interface MyComponentCustomEvent<T> extends CustomEvent<T> {
detail: T;
target: HTMLMyComponentElement;
}
declare global {
interface HTMLMyComponentElementEventMap {
example: number;
}
interface HTMLMyComponentElement extends Components.MyComponent, HTMLStencilElement {
addEventListener<K extends keyof HTMLMyComponentElementEventMap>(type: K, listener: (this: HTMLMyComponentElement, ev: MyComponentCustomEvent<HTMLMyComponentElementEventMap[K]>) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof HTMLMyComponentElementEventMap>(type: K, listener: (this: HTMLMyComponentElement, ev: MyComponentCustomEvent<HTMLMyComponentElementEventMap[K]>) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
var HTMLMyComponentElement: {
prototype: HTMLMyComponentElement;
new (): HTMLMyComponentElement;
};
interface HTMLElementTagNameMap {
"my-component": HTMLMyComponentElement;
}
}
This would make dealing with events a lot easier in general:
And especially Angular:
Describe Alternatives
Creating a custom build target to create types that overload addEventListener and removeEventListener for each Stencil component's html element type. Or add types with overloads somewhere in your project when consuming your Stencil components. Or modifying your generated components.d.ts through some other kind of automation.
Example:
export interface MyComponentCustomEvent<T> extends CustomEvent<T> {
detail: T;
target: HTMLMyComponentElement;
}
declare global {
interface HTMLMyComponentElementEventMap {
example: number;
}
interface HTMLMyComponentElement extends Components.MyComponent, HTMLStencilElement {
addEventListener<K extends keyof HTMLMyComponentElementEventMap>(type: K, listener: (this: HTMLMyComponentElement, ev: MyComponentCustomEvent<HTMLMyComponentElementEventMap[K]>) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof HTMLMyComponentElementEventMap>(type: K, listener: (this: HTMLMyComponentElement, ev: MyComponentCustomEvent<HTMLMyComponentElementEventMap[K]>) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
var HTMLMyComponentElement: {
prototype: HTMLMyComponentElement;
new (): HTMLMyComponentElement;
};
interface HTMLElementTagNameMap {
"my-component": HTMLMyComponentElement;
}
}
Another workaround is to use Another workaround to this issue is this custom output target I made: https://github.com/m-thompson-code/event-listener-types-output-target#event-listener-types-output-target to automate creating event listener types.
Related Code
- Generate a stencil component library:
npm init stencil
cd demo
Choose component --> Project name: demo --> Confirm Y
- Add an event named
examplewith typeEventEmitter<number>toMyComponentfound atdemo/src/components/my-component/my-component.tsx:
// Add `Event` to import
import { Component, Event, EventEmitter, Prop, h } from '@stencil/core';
// ...
@Component(/* ... */)
export class MyComponent {
// Add an event `example`:
@Event() example!: EventEmitter<number>;
// ...
}
- Build stencil library:
npm run build
- Add the following to any TypeScript find such as
index.tsfound atdemo/src/index.ts:
// HTMLMyComponentElement
const myComponent = document.createElement('my-component');
// Add event listener
myComponent.addEventListener('example', (event) => {
// Property 'detail' does not exist on type 'Event'.ts(2339)
console.log(event.detail);// <-- Type error
});
When trying to access event.detail, this leads to a type error:
Property 'detail' does not exist on type 'Event'.ts(2339)
Additional Information
This issue relates to https://github.com/ionic-team/stencil-ds-output-targets/issues/219 but is not the same issue. The difference is that this issue is about Stencil as custom elements and consuming them as custom elements with or without Angular and without using the Angular output target.
That being said, a solution would likely resolve the majority of the problems involving Outputs / Events with the Angular output target.
Another workaround to this issue is this custom output target I made: https://github.com/m-thompson-code/event-listener-types-output-target#event-listener-types-output-target
The fix for this issue has been released as a part of today's Stencil v4.6.0 release. As a result, I'm going to close this issue. Should the bug/issue reappear after v4.6.0, please feel free to open a new issue.
Unfortunately the provided solution for Stencil 4.6.0 didn't work for me.
I'm getting following error in my Angular-Project (Version 16.2.0): Event valueChange is not emitted by any applicable directives nor by dx-text-input element. The compilation still works fine but I cannot get any autocompetion for my event.
I'm using following dependencies:
- @stencil/angular-output-target: 0.8.2
- @stencil/core: 4.6.0
The event map within my dist/types is generated properly:
interface HTMLDxTextInputElementEventMap {
"valueChange": string;
"valueComplete": string;
"autocomplete": TextInputAutocompleteController;
"selectAutocomplete": any;
"enterPress": void;
}
/**
* Form element for capturing text input.
* @path /Form/Inputs
* @name Text Input
* @shortname Text
* @icon font
* @stable
* @slot-example info
*/
interface HTMLDxTextInputElement extends Components.DxTextInput, HTMLStencilElement {
addEventListener<K extends keyof HTMLDxTextInputElementEventMap>(type: K, listener: (this: HTMLDxTextInputElement, ev: DxTextInputCustomEvent<HTMLDxTextInputElementEventMap[K]>) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | AddEventListenerOptions): void;
addEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | AddEventListenerOptions): void;
removeEventListener<K extends keyof HTMLDxTextInputElementEventMap>(type: K, listener: (this: HTMLDxTextInputElement, ev: DxTextInputCustomEvent<HTMLDxTextInputElementEventMap[K]>) => any, options?: boolean | EventListenerOptions): void;
removeEventListener<K extends keyof DocumentEventMap>(type: K, listener: (this: Document, ev: DocumentEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener<K extends keyof HTMLElementEventMap>(type: K, listener: (this: HTMLElement, ev: HTMLElementEventMap[K]) => any, options?: boolean | EventListenerOptions): void;
removeEventListener(type: string, listener: EventListenerOrEventListenerObject, options?: boolean | EventListenerOptions): void;
}
var HTMLDxTextInputElement: {
prototype: HTMLDxTextInputElement;
new (): HTMLDxTextInputElement;
};
Angular HTML-file
<dx-text-input (valueChange)="onTextInputValueChanged($event.detail)"></dx-text-input>
Angular Typescript-file
onTextInputValueChanged(detail: string) {
console.log(detail);
}
Adding an Event-Listener through Typescript however works fine:
@FabioGimmillaro can you please open a new issue? The team does not actively monitor closed PRs/Issues