Skip to content

querySelector wrapper #17930

Answered by JonghwanWon
yukulele asked this question in Q&A
Discussion options

You must be logged in to vote

hello, @yukulele

I hope this solution help you.

function qs<K extends keyof HTMLElementTagNameMap>(selectors: K): HTMLElementTagNameMap[K] | null;
function qs<K extends keyof SVGElementTagNameMap>(selectors: K): SVGElementTagNameMap[K] | null;
function qs<E extends Element = Element>(selectors: string): E | null;
function qs(selectors: Parameters<typeof document['querySelector']>[0]) {
  const elm = document.querySelector(selectors);
  if (!elm) throw new Error(`'${selectors}' is not in dom.`);
  return elm;
}

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by yukulele
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants