Skip to content

[Proposal] - Add Generic Element Type Support to Hook Interface #3956

@IdoLeshkowitz

Description

@IdoLeshkowitz

Proposal - Add Generic Element Type Support to Hook Interface

Summary

The current Hook interface in Phoenix LiveView's TypeScript definitions always types the el property as HTMLElement, which prevents developers from leveraging TypeScript's type system when working with specific HTML element types in hooks.

Use Cases

Many LiveView hooks are designed to work with specific types of HTML elements and need access to element-specific properties and methods:

Form Hooks

const FormValidationHook = {
  mounted() {
    // Need access to form-specific properties like .elements, .method, .action
    // Currently requires casting: (this.el as HTMLFormElement).elements
    this.el.addEventListener('submit', this.validateForm);
  }
};

Current Limitation

The HookInterface is defined with a generic HTMLElement type:

  export interface HookInterface {
    el: HTMLElement; // Always HTMLElement - no element-specific typing
    // ...
  }

Desired Outcome

Hooks should be able to specify what type of HTML element they expect to work with.

This enhancement would significantly improve the TypeScript developer experience. I will be happy to make this change, just wanna make sure that you guys are fine with it.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions