Skip to content

refactor: align getElement/getElements generic signature with ref builders#8

Merged
psd-coder merged 1 commit into
mainfrom
update-element-selectors-signature
Apr 10, 2026
Merged

refactor: align getElement/getElements generic signature with ref builders#8
psd-coder merged 1 commit into
mainfrom
update-element-selectors-signature

Conversation

@psd-coder

Copy link
Copy Markdown
Owner

Overview

Reworks the generic type parameters of ctx.getElement and ctx.getElements to match the overload pattern already used by the one/many ref builders.

Problem Statement

The previous signature used a single generic E extends keyof HTMLElementTagNameMap, which forced the return type through HTMLElementTagNameMap. This made it impossible to type the result as a custom element type (e.g. SVGCircleElement or a custom HTMLElement subtype) when querying by CSS selector, and created an inconsistency with the ref builder API that already supported this via one<CustomEl>(".selector").

Solution Approach

  • Replaced the single E extends keyof HTMLElementTagNameMap generic with three overload tiers for both getElement and getElements (including their scoped-root variants):
    • Tag name literal → auto-infers from HTMLElementTagNameMap (e.g. getElement("button")HTMLButtonElement)
    • <E extends Element> explicit generic → returns E (e.g. getElement<SVGCircleElement>(".circle"))
    • Plain string selector fallback → returns Element
  • Updated call sites in tests that passed custom element tag names to bind(), now requiring an explicit <HTMLElement> generic since those tags are not in HTMLElementTagNameMap

Breaking Changes

CSS selector queries that previously returned HTMLElementTagNameMap[E] (always an HTMLElement subtype) now return Element when no tag name or explicit generic is provided. Code passing the result directly to APIs expecting HTMLElement (such as ctx.bind()) will need an explicit generic: ctx.getElement<HTMLElement>(".selector").

Testing

Type-level coverage added: a new test case verifies that getElement<CustomEl> and getElements<CustomEl> (both plain and scoped-root variants) correctly resolve to CustomEl and CustomEl[] respectively.

Switch from `E extends keyof HTMLElementTagNameMap` to separate overloads: tag name for auto-inference, `E extends Element` for explicit typing, and a plain fallback — matching the ref builder API.
@github-actions

Copy link
Copy Markdown

size-limit report 📦

Path Size
.size-check/index.mjs 2.35 KB (0%)
.size-check/render.mjs 417 B (0%)
.size-check/context.mjs 402 B (0%)

@psd-coder psd-coder merged commit ed68403 into main Apr 10, 2026
5 checks passed
@psd-coder psd-coder deleted the update-element-selectors-signature branch April 10, 2026 01:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant