npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

event-listener-types-output-target

v1.3.0

Published

Event listener types output target for @stencil/core components.

Downloads

16

Readme

event-listener-types-output-target

Stencil output target to generate event listener types

Summary

Stencil components currently don't generate event listener types which can make consuming Stencil components difficult when wanting to add an event listener in a TypeScript project:

const myComponent = document.createElement('my-component');

myComponent.addEventListener('my-event', event => {
  // TypeError: Property 'detail' does not exist on type 'Event'
  event.detail;
});

This target output generates additional types to make events type safe:

const myComponent = document.createElement('my-component');

myComponent.addEventListener('my-event', event => {
  // event is now of type MyComponentCustomEvent, and detail will
  // have the proper type
  event.detail;
});

This will also allow components to have proper Output binding types in Angular projects:

@Component({
  selector: 'app-root',
  template: `<my-component (my-event)="handleMyEvent($event)" />`,
})
export class AppComponent {
  handleMyEvent(event: MyComponentCustomEvent<string>) {
    // event is properly typed now and can be consumed without
    // needing to type `event` as Event or any then use type assertion
    // to its proper type
    event.detail;
  }
}

How to use

To export event listener types:

  1. Install event-listener-types-output-target
  2. Update Stencil config to include eventListenerTypesOutputTarget
  3. Add component-event-listeners.d.ts to index file

Install event-listener-types-output-target

npm i -D event-listener-types-output-target

Update Stencil config

By default your Stencil config should be located at stencil.config.ts. Update your Stencil config to use this output target:

import { Config } from '@stencil/core';
import { eventListenerTypesOutputTarget } from 'event-listener-types-output-target';

export const config: Config = {
  // ...
  outputTargets: [
    // ...
    eventListenerTypesOutputTarget(),
  ],
};

Now when you build, component-event-listeners.d.ts will be generated next to your components.d.ts file found at src/component-event-listeners.d.ts.

This is where components.d.ts is generated by default, but you can override where the component-event-listeners.d.ts files are generated by setting outputPaths:

import { Config } from '@stencil/core';
import { eventListenerTypesOutputTarget } from 'event-listener-types-output-target';

export const config: Config = {
  // ...
  outputTargets: [
    // ...
    eventListenerTypesOutputTarget({
      outputPaths: ['some/custom/path/component-event-listeners.d.ts'],
    }),
  ],
};

Add types to index.ts

To include component-event-listeners.d.ts in your dist, you can add the generated types to your index.ts found at src/index.ts:

export * from './components';
export * from './component-event-listeners'; // <-- add this line
// ...

You'll get a type error if component-event-listeners.d.ts doesn't already exist, so be sure to generate one or add a placeholder like this one:

/** src/component-event-listeners */
// Doesn't matter what you export, but this file will need to export/import something something
export type Placeholder = 'This should be replaced automatically';

Output target options

For more custom behavior such as changing the outputPaths to customize where the generated types files are saved, there are two other options:

  1. outputPaths - Paths where generated event listener types will be stored. Path should include filename. Default if not defined is:
    ['src/component-event-listeners.d.ts'];
  2. importPath - The generated event listener types depend on types from components.d.ts. Setting this will define the path where those types will be imported from. Default if not defined is:
    './components';
  3. excludeComponents - List of components to not generate event listener types. Values should be the tag name of the component.

Development

For development, code changes are lint/formatted on commit. New features should come with new tests.

npm i # Install dependencies
npm run test # Run unit tests
npm run test.watch # Run unit tests in watch mode