Description
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.