How to determine the type of element returned by querySelector in TypeScript?

defines a tool function that is expected to be used to select different elements. It infers that the return type is Element, but this is a base class, and the methods and properties of many specific classes cannot be prompted automatically.

export const $ = (selector, scope = document): HTMLElement | HTMLInputElement => scope.querySelector(selector);

excuse me, how should I define this return value type so that I can indicate the specific properties and methods it has according to different elements? Thank you < 3

May.07,2021

this can only be prompted manually by you, ts

  

you can use
const input = document.querySelector < HTMLInputElement > ('input')

Menu