I have a function that generates a node and returns it. Here is an example implementation:
addElement: function ({
parentNode,
elementName,
tagName,
}) {
// Creates and appends the new node.
parentNode[elementName] = document.createElement(tagName);
parentNode.appendChild(parentNode[elementName]);
return parentNode[elementName];
}
In this function, parentNode
refers to another element, elementName
is a string (e.g., container
), and tagName
specifies the type of element being created, also in string form (e.g., div
).
The issue I am facing is uncertainty regarding the exact type that will be returned. The return type could be an extension of Element
, which might be HTMLDivElement
or HTMLAreaElement
. Similarly, determining the type of parentNode
presents a challenge as well.
How can I resolve this in TypeScript?