🪄 A TypeScript-types patch for querySelector()
/ querySelectorAll()
, make them return types you expect them to! 🔮
I work hard for every project, including this one and your support means a lot to me!
Consider buying me a coffee. ☕
Thank you for supporting my efforts! 🙏😊
@igorskyflyer
Here's magic-querySelector
in action.
without-magic.mp4
magic-queryselector
with-magic.mp4
magic-queryselector
Visual Studio Code theme used in the demonstration is Kai 🌊
.
Install it by executing:
npm i -D "@igor.dvlpr/magic-queryselector"
Including the magic-queryselector
into your project depends on the language of it.
Please see the appropriate section for your project:
If you want to use it with TypeScript, you need to copy the following code
import '@igor.dvlpr/magic-queryselector'
and then do one of the following:
[ 1st option ]
Warning
This method requires a valid tsconfig.json
file to be present in the root of your project.
Create a magic.d.ts
file in the root directory of your project and add the snippet you copied:
magic.d.ts
import '@igor.dvlpr/magic-queryselector'
That's it! 🥳 You're all set up.
Tip
TypeScript server sometimes likes to play games, if the patch doesn't work immediately please restart TypeScript server or Visual Studio Code.
[ 2nd option ]
Add the code snippet you copied to the top of your entrypoint/main TypeScript file.
index.ts
import '@igor.dvlpr/magic-queryselector'
Tip
TypeScript server sometimes likes to play games, if the patch doesn't work immediately please restart TypeScript server or Visual Studio Code.
Note
If you want to use it with JavaScript, you don't need to do anything besides installing the package.
Tip
TypeScript server sometimes likes to play games, if the patch doesn't work immediately please restart TypeScript server or Visual Studio Code.
This patch extends the default (return) type inference of TypeScript by inferring the types from the input string
containing selectors/combinators passed to querySelector()
/ querySelectorAll()
.
Note
querySelector()
will return the type listed in the table below, e.g. HTMLDivElement
, while querySelectorAll()
will return NodeListOf<T>
of the same type, e.g. NodeListOf<HTMLDivElement>
.
For brevity this table only shows the types for querySelector()
.
Read more about selector structure and selectors and combinators on MDN
.
The following table shows which selectors/combinators are supported along with the inferred return types for the given examples.
Selector/Combinator | Example | Compatibility | Inference | Before/After |
---|---|---|---|---|
Type + ID | div#app |
✅ | Patched | Element /HTMLDivElement |
Type + Class | a.myLink |
✅ | Patched | Element /HTMLAnchorElement |
Type + Attribute | a[title] |
✅ | Patched | Element /HTMLAnchorElement |
Descendant | div video |
✅ | Patched | Element /HTMLVideoElement |
Child | main > a |
✅ | Patched | Element /HTMLAnchorElement |
Next-sibling | div + span |
✅ | Patched | Element /HTMLSpanElement |
Subsequent-sibling | h1 ~ pre |
✅ | Patched | Element /HTMLPreElement |
Pseudo-class :root | :root |
✅ | Patched | Element /HTMLHtmlElement |
Column (1) | col || td |
✅ | Patched | Element /HTMLTableCellElement |
Universal | * |
— | Native | Element /Element |
Type | li |
— | Native | HTMLLIElement /HTMLLIElement |
ID | #share |
— | Native | Element /Element |
Class | .footer |
— | Native | Element /Element |
Attribute | [title] |
— | Native | Element /Element |
(1) The column combinator is a highly-experimental upcoming combinator "that is placed between two CSS selectors. It matches only those elements matched by the second selector that belong to the column elements matched by the first." (source: MDN )
main.js
const video = document.querySelector('div#app > video') // HTMLVideoElement | null
const audios = document.querySelectorAll('div#app > audio') // NodeListOf<HTMLAudioElement>
if(video) {
video.src = '<some_URL>' // now we can access all <video> properties and methods
}
if(audios.length > 0) {
audios[0].src = '<some_URL>' // 😀😀😀
}
📑 Changelog is available here: CHANGELOG.md.
Licensed under the MIT license which is available here, MIT license.
🕶️ Reads a JSON file into a Map. 🌻
🦀 ExtendableString allows you to create strings on steroids that have custom transformations applied to them, unlike common, plain strings.. 🪀
🥽 Provides ways of parsing UNC paths and checking whether they are valid. 🎱
✒ DúöScríbî allows you to convert letters with diacritics to regular letters. 🤓
🧬 A lightweight JavaScript utility allowing deep copy-by-value of nested objects, arrays and arrays of objects. 🪁
Created by Igor Dimitrijević (@igorskyflyer).