strict-event-emitter-types

  • Version 2.0.0
  • Published
  • 10.3 kB
  • No dependencies
  • ISC license

Install

npm i strict-event-emitter-types
yarn add strict-event-emitter-types
pnpm add strict-event-emitter-types

Overview

Strictly and safely type any EventEmitter-like interface on any object

Index

Variables

variable assignmentCompatibilityHack

const assignmentCompatibilityHack: Symbol;

    Interfaces

    interface TypeRecord

    interface TypeRecord<T, U, V> {}

      property ' _emitterType'

      ' _emitterType'?: T;

        property ' _emitType'

        ' _emitType'?: V;

          property ' _eventsType'

          ' _eventsType'?: U;

            Type Aliases

            type EEMethodReturnType

            type EEMethodReturnType<
            T,
            S extends string,
            TValue,
            FValue = void
            > = S extends keyof T ? InnerEEMethodReturnType<T[S], TValue, FValue> : FValue;

              type EmitEventNames

              type EmitEventNames<
              TEmitter extends TypeRecord<any, any, any>,
              TEventRecord extends NoUndefined<TEmitter[' _eventsType']> = NoUndefined<
              TEmitter[' _eventsType']
              >,
              TEmitRecord extends NoUndefined<TEmitter[' _emitType']> = NoUndefined<
              TEmitter[' _emitType']
              >
              > = keyof TEmitRecord;

                type EventNames

                type EventNames<
                TEmitter extends TypeRecord<any, any, any>,
                TEventRecord extends NoUndefined<TEmitter[' _eventsType']> = NoUndefined<
                TEmitter[' _eventsType']
                >,
                TEmitRecord extends NoUndefined<TEmitter[' _emitType']> = NoUndefined<
                TEmitter[' _emitType']
                >
                > = keyof TEmitRecord | keyof TEventRecord;

                  type InnerEEMethodReturnType

                  type InnerEEMethodReturnType<T, TValue, FValue> = T extends (...args: any[]) => any
                  ? ReturnType<T> extends void | undefined
                  ? FValue
                  : TValue
                  : FValue;

                    type MatchingKeys

                    type MatchingKeys<
                    TRecord,
                    TMatch,
                    K extends keyof TRecord = keyof TRecord
                    > = K extends (TRecord[K] extends TMatch ? K : never) ? K : never;

                      type NoUndefined

                      type NoUndefined<T> = T extends undefined ? never : T;

                        type OnEventNames

                        type OnEventNames<
                        TEmitter extends TypeRecord<any, any, any>,
                        TEventRecord extends NoUndefined<TEmitter[' _eventsType']> = NoUndefined<
                        TEmitter[' _eventsType']
                        >,
                        TEmitRecord extends NoUndefined<TEmitter[' _emitType']> = NoUndefined<
                        TEmitter[' _emitType']
                        >
                        > = keyof TEventRecord;

                          type OverriddenKeys

                          type OverriddenKeys = keyof OverriddenMethods<any, any, any>;

                            type OverriddenMethods

                            type OverriddenMethods<TEmitter, TEventRecord, TEmitRecord = TEventRecord> = {
                            on<P extends keyof TEventRecord, T>(
                            this: T,
                            event: P,
                            listener: (...args: ListenerType<TEventRecord[P]>) => void
                            ): EEMethodReturnType<TEmitter, 'on', T>;
                            on(
                            event: typeof assignmentCompatibilityHack,
                            listener: (...args: any[]) => any
                            ): void;
                            addListener<P extends keyof TEventRecord, T>(
                            this: T,
                            event: P,
                            listener: (...args: ListenerType<TEventRecord[P]>) => void
                            ): EEMethodReturnType<TEmitter, 'addListener', T>;
                            addListener(
                            event: typeof assignmentCompatibilityHack,
                            listener: (...args: any[]) => any
                            ): void;
                            addEventListener<P extends keyof TEventRecord, T>(
                            this: T,
                            event: P,
                            listener: (...args: ListenerType<TEventRecord[P]>) => void
                            ): EEMethodReturnType<TEmitter, 'addEventListener', T>;
                            addEventListener(
                            event: typeof assignmentCompatibilityHack,
                            listener: (...args: any[]) => any
                            ): void;
                            removeListener<P extends keyof TEventRecord, T>(
                            this: T,
                            event: P,
                            listener: (...args: any[]) => any
                            ): EEMethodReturnType<TEmitter, 'removeListener', T>;
                            removeListener(
                            event: typeof assignmentCompatibilityHack,
                            listener: (...args: any[]) => any
                            ): void;
                            removeEventListener<P extends keyof TEventRecord, T>(
                            this: T,
                            event: P,
                            listener: (...args: any[]) => any
                            ): EEMethodReturnType<TEmitter, 'removeEventListener', T>;
                            removeEventListener(
                            event: typeof assignmentCompatibilityHack,
                            listener: (...args: any[]) => any
                            ): void;
                            once<P extends keyof TEventRecord, T>(
                            this: T,
                            event: P,
                            listener: (...args: ListenerType<TEventRecord[P]>) => void
                            ): EEMethodReturnType<TEmitter, 'once', T>;
                            once(
                            event: typeof assignmentCompatibilityHack,
                            listener: (...args: any[]) => any
                            ): void;
                            emit<P extends keyof TEmitRecord, T>(
                            this: T,
                            event: P,
                            ...args: ListenerType<TEmitRecord[P]>
                            ): EEMethodReturnType<TEmitter, 'emit', T>;
                            emit(event: typeof assignmentCompatibilityHack, ...args: any[]): void;
                            };

                              type ReturnTypeOfMethod

                              type ReturnTypeOfMethod<T> = T extends (...args: any[]) => any
                              ? ReturnType<T>
                              : void;

                                type ReturnTypeOfMethodIfExists

                                type ReturnTypeOfMethodIfExists<T, S extends string> = S extends keyof T
                                ? ReturnTypeOfMethod<T[S]>
                                : void;

                                  type StrictBroadcast

                                  type StrictBroadcast<
                                  TEmitter extends TypeRecord<any, any, any>,
                                  TEmitRecord extends NoUndefined<TEmitter[' _emitType']> = NoUndefined<
                                  TEmitter[' _emitType']
                                  >,
                                  VK extends VoidKeys<TEmitRecord> = VoidKeys<TEmitRecord>,
                                  NVK extends Exclude<keyof TEmitRecord, VK> = Exclude<keyof TEmitRecord, VK>
                                  > = {
                                  <E extends NVK>(event: E, request: TEmitRecord[E]): any;
                                  <E extends VK>(event: E): any;
                                  };

                                    type StrictEventEmitter

                                    type StrictEventEmitter<
                                    TEmitterType,
                                    TEventRecord,
                                    TEmitRecord = TEventRecord,
                                    UnneededMethods extends Exclude<OverriddenKeys, keyof TEmitterType> = Exclude<
                                    OverriddenKeys,
                                    keyof TEmitterType
                                    >,
                                    NeededMethods extends Exclude<OverriddenKeys, UnneededMethods> = Exclude<
                                    OverriddenKeys,
                                    UnneededMethods
                                    >
                                    > = TypeRecord<TEmitterType, TEventRecord, TEmitRecord> &
                                    Pick<TEmitterType, Exclude<keyof TEmitterType, OverriddenKeys>> &
                                    Pick<OverriddenMethods<TEmitterType, TEventRecord, TEmitRecord>, NeededMethods>;

                                      type VoidKeys

                                      type VoidKeys<Record> = MatchingKeys<Record, void>;

                                        Package Files (1)

                                        Dependencies (0)

                                        No dependencies.

                                        Dev Dependencies (2)

                                        Peer Dependencies (0)

                                        No peer dependencies.

                                        Badge

                                        To add a badge like this onejsDocs.io badgeto your package's README, use the codes available below.

                                        You may also use Shields.io to create a custom badge linking to https://www.jsdocs.io/package/strict-event-emitter-types.

                                        • Markdown
                                          [![jsDocs.io](https://img.shields.io/badge/jsDocs.io-reference-blue)](https://www.jsdocs.io/package/strict-event-emitter-types)
                                        • HTML
                                          <a href="https://www.jsdocs.io/package/strict-event-emitter-types"><img src="https://img.shields.io/badge/jsDocs.io-reference-blue" alt="jsDocs.io"></a>