develwoutacause’s avatardevelwoutacause’s Twitter Archive—№ 278

        1. document.querySelector("input#name") returns Element, even though it should clearly be HTMLInputElement. Using #TypeScript 4.1 inferred Template Literal Types, I made a parser to more accurately type the result, check it out! #WebDev #JavaScript gist.github.com/dgp1130/ac36e63744cdeffcbc7522387662f722
      1. …in reply to @develwoutacause
        This parses the provided selector, extracts the tag name, and then looks up that tag name to get the associated element. It's able to handle all CSS combinators and operators (let me know if I missed one). I'm not totally convinced this is a *good* idea, but it is a cool one!
    1. …in reply to @develwoutacause
      This does hit the recursion limit for overly-complex queries, but it's good enough for 99% of use cases IMHO. I also discovered that pseudo-elements don't work in querySelector(). This parser also catches that mistake and turns the runtime error into a compile-time error!
  1. …in reply to @develwoutacause
    I'm really interested to see what the community thinks of this of this use case. @buildsghost made a similar JSON parser (github.com/jamiebuilds/json-parser-in-typescript-very-bad-idea-please-dont-use), this is just a (slightly) less-bad use case of the same idea.