Perform type checking of the event detail in the emit function if the event type matches a Shoelace event

pull/1183/head
Matt Pharoah 2023-02-08 17:01:00 -05:00
rodzic 75bd7784fb
commit ce708fbba8
1 zmienionych plików z 7 dodań i 1 usunięć

Wyświetl plik

@ -1,13 +1,19 @@
import { LitElement } from 'lit';
import { property } from 'lit/decorators.js';
type SlEventInit<T> = T extends keyof GlobalEventHandlersEventMap
? GlobalEventHandlersEventMap[T] extends CustomEvent
? CustomEventInit<GlobalEventHandlersEventMap[T]['detail']>
: 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<T extends string>(name: T, options?: SlEventInit<T>) {
const event = new CustomEvent(name, {
bubbles: true,
cancelable: false,