From ce708fbba8fd41f8dd45fff0568657f8001caabc Mon Sep 17 00:00:00 2001 From: Matt Pharoah Date: Wed, 8 Feb 2023 17:01:00 -0500 Subject: [PATCH] Perform type checking of the event detail in the emit function if the event type matches a Shoelace event --- src/internal/shoelace-element.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/internal/shoelace-element.ts b/src/internal/shoelace-element.ts index 2188e9e9..7534098a 100644 --- a/src/internal/shoelace-element.ts +++ b/src/internal/shoelace-element.ts @@ -1,13 +1,19 @@ import { LitElement } from 'lit'; import { property } from 'lit/decorators.js'; +type SlEventInit = T extends keyof GlobalEventHandlersEventMap + ? GlobalEventHandlersEventMap[T] extends CustomEvent + ? CustomEventInit + : CustomEventInit + : CustomEventInit; + export default class ShoelaceElement extends LitElement { // Make localization attributes reactive @property() dir: string; @property() lang: string; /** Emits a custom event with more convenient defaults. */ - emit(name: string, options?: CustomEventInit) { + emit(name: T, options?: SlEventInit) { const event = new CustomEvent(name, { bubbles: true, cancelable: false,