stencil icon indicating copy to clipboard operation
stencil copied to clipboard

bug: Event payload with self-reference causes type generation error

Open Mitiryl opened this issue 11 months ago • 1 comments

Prerequisites

Stencil Version

4.29.2

Current Behavior

I've encountered an issue with Stencil's type generation when a component emits an event and passes itself as the payload. The generated TypeScript types contain an error that breaks type checking. The generated .d.ts file includes an unresolved type reference to the component, causing a TypeScript error like:

[ ERROR ]  TypeScript: src/components.d.ts:24:18
           Cannot find name 'MyComponent'.

     L23:  interface HTMLMyComponentElementEventMap {
     L24:      "myEvent": MyComponent;
     L25:  }

Expected Behavior

Type generation should work correctly even when the event payload is a reference to the component instance itself.

System Info

System: node 20.17.0
Stencil: 4.29.2 🔋
TypeScript: 5.5.4

Steps to Reproduce

  1. Create a Stencil component like this:
import { Component, Event, EventEmitter, h } from '@stencil/core';

@Component({
  tag: 'my-component',
  shadow: true,
})
export class MyComponent {
  
  @Event() myEvent: EventEmitter<MyComponent>;

  render() {
    return <div>Hello</div>;
  }
}

  1. Build the project

Code Reproduction URL

https://github.com/Mitiryl/stencil-types

Additional Information

No response

Mitiryl avatar Apr 09 '25 12:04 Mitiryl

Thank you for reporting and providing a reproduction case, any contributions that may resolve this bug are much appreciated.

christian-bromann avatar Apr 10 '25 15:04 christian-bromann

hey @Mitiryl,

Can you give me some idea around what you were wanting to achieve here?

@Component({
  tag: 'my-component',
  shadow: true,
})
export class MyComponent {
  
  @Event() myEvent: EventEmitter<MyComponent>;

  render() {
    return <div>Hello</div>;
  }
}

Practically speaking, What's the desired payload here(?) this.myEvent.emit( ...what?...) MyComponent in your example is a class ctor ... is that correct? (if so why?)

A more common requirement, is to make the calling element part of the event payload .... something like:

@Element() host: HTMLMyComponent;
@Event() myEvent:  EventEmitter<HTMLMyComponent>
...
this.myEvent.emit(this.host);

Is that what you wanted? That'd probably be more accurate than the class ctor.

johnjenkins avatar Oct 22 '25 08:10 johnjenkins