Skip to content

Return narrower Element types #23

Closed as not planned
Closed as not planned
@Caellian

Description

@Caellian

Initial checklist

  • I searched issues and discussions and couldn’t find anything (or linked relevant results below)

Problem

Assuming there exists some type definition for:

/**
 * @typedef {object} HASTImgElement
 * @property {"element"} type
 * @property {"img"} tagName
 * ... etc ...
 */

The h function currently returns Element which doesn't satisfy tagName restriction. This requires manual casting and/or ts-ignore in TS or JS code with (TS backed) type checking. It would be nice if the function returned {tagName: selector} & Element, even though that still doesn't guarantee properties and other requirements to match.

Current solutions

While toying around with it locally, I saw that this is the ~best that can be done given that selectors can be composed tag expressions with classes and what not. So currently, the type is always Element because of that.

Proposed solutions

Soon after, I realized that in (my) case where tag name is a simple lowercase HTML tag, there could exist a type HTMLName which covers all of them and an additional @override for returned h function which specializes return type based on stricter HTMLName selector type to satisfy:

/**
 * @template T
 * @param {T & HTMLName} selector
 * @param {Properties} properties
 * @param {...Child} children
 * @returns {{tagName: T} & Element}
 */

Now, I checked into used hast-util-parse-selector and it seems to return proper types, so it's hastscript that erases this information.

Metadata

Metadata

Assignees

No one assigned

    Labels

    👎 phase/noPost cannot or will not be acted on🙅 no/wontfixThis is not (enough of) an issue for this project

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions