Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Custom HTMLElement type for event targets #3291

Closed
3 tasks done
sean-perkins opened this issue Mar 18, 2022 · 1 comment
Closed
3 tasks done

feat: Custom HTMLElement type for event targets #3291

sean-perkins opened this issue Mar 18, 2022 · 1 comment
Labels
Resolution: Refine This PR is marked for Jira refinement. We're not working on it - we're talking it through.

Comments

@sean-perkins
Copy link
Contributor

sean-perkins commented Mar 18, 2022

Prerequisites

Describe the Feature Request

Export additional type data on the generated component interfaces, that allows developers to have typings for the event target with custom events.

i.e.:

"onIonInfinite"?: (event: CustomEvent<void> & { target: HTMLIonInfiniteScrollElement }) => void;

Usage:

<ion-infinite onIonInfinite={ev => ev.target.complete()} />

The ev.target would allow accessing all the public methods available on the host element.

Additionally it would be valuable to export the custom event type as an interface for each event, so that development eco-systems like Angular can access the reusable type:

handleInfiniteScroll(ev: InfiniteScrollEvent<void>) {
  ev.target.complete();
}

Describe the Use Case

When the event is emitted, it already contains information around the target, but does not include any type information in the generated types. This is problematic because developers have to re-cast or use any for event handlers.

Ideally, developers would like to directly access the correct typings the event target, without having to recast:

<ion-infinite onIonInfinite={ev => ev.target.complete()} />

Describe Preferred Solution

There are two changes that are necessary to support this (from the generated output).

  1. Each custom event will need a type that includes a reference to the HTMLElement, such as:
"onIonInfinite"?: (event: CustomEvent<void> & { target: HTMLIonInfiniteScrollElement }) => void;
  1. Ideally this event type is not inlined and rather a generated public type that is consumed for this type, but available to implementers:
"onIonInfinite"?: InfiniteScrollEvent<void>;
export type InfiniteScrollEvent = <T=any>(event: CustomEvent<T> & { target: HTMLIonInfiniteScrollElement }) => void;

Describe Alternatives

No response

Related Code

I have the first case outlined above handled with this:

export const generateEventTypes = (cmpEvents: d.ComponentCompilerEvent[], cmpHtmlElementInterface: string): d.TypeInfo => {
  return cmpEvents.map((cmpEvent) => {
    const name = `on${toTitleCase(cmpEvent.name)}`;
    const targetType = `{ target: ${cmpHtmlElementInterface} }`;
    const type = cmpEvent.complexType.original
      ? `(event: CustomEvent<${cmpEvent.complexType.original}> & ${targetType}) => void`
      : `CustomEvent & ${targetType}`;
    return {
      name,
      type,
      optional: false,
      required: false,
      internal: cmpEvent.internal,
      jsdoc: getTextDocs(cmpEvent.docs),
    };
  });
};

I will investigate the work effort of the second item.

Additional Information

Related issues:

Framework:

Stencil output targets:

@ionitron-bot ionitron-bot bot added the triage label Mar 18, 2022
@rwaskiewicz rwaskiewicz added Feature: TS Typings Resolution: Needs Investigation This PR or Issue should be investigated from the Stencil team and removed triage labels Mar 18, 2022
@rwaskiewicz rwaskiewicz added Resolution: Refine This PR is marked for Jira refinement. We're not working on it - we're talking it through. and removed Resolution: Needs Investigation This PR or Issue should be investigated from the Stencil team labels May 9, 2022
@rwaskiewicz
Copy link
Member

I'm going to close this issue, as the related-PR (#3296) is a part of today's Stencil v2.16.0 release. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Refine This PR is marked for Jira refinement. We're not working on it - we're talking it through.
Projects
None yet
Development

No branches or pull requests

2 participants