Here is an example of a specific interface:
interface Elements {
divContainer: HTMLDivElement;
inputUpload: HTMLInputElement;
}
My goal is to create a function that can retrieve elements based on their names:
getElement(name: keyof Elements): Elements[name] {
// Code to fetch the specified element.
}
const div = getElement("divContainer"); // This should return HTMLDivElement
const input = getElement("inputElement"); // This should return HTMLInputElement
However, I encountered an issue where I couldn't use the parameter as a type for the return value. I've experimented with Parameters
, but haven't been successful in resolving this problem.