Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/igorskyflyer/npm-magic-queryselector

๐Ÿช„ A TypeScript-types patch for querySelector/querySelectorAll, make them return types you expect them to! ๐Ÿ”ฎ
https://github.com/igorskyflyer/npm-magic-queryselector

back-end biome combinator css dom dom-manipulation html igorskyflyer javascript js magic node query queryselector queryselectorall selector ts typescript

Last synced: about 2 hours ago
JSON representation

๐Ÿช„ A TypeScript-types patch for querySelector/querySelectorAll, make them return types you expect them to! ๐Ÿ”ฎ

Awesome Lists containing this project

README

        

querySelector( ๐Ÿช„ )



๐Ÿช„ A TypeScript-types patch for querySelector() / querySelectorAll(), make them return types you expect them to! ๐Ÿ”ฎ








๐Ÿ’– Support further development


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! ๐Ÿ™๐Ÿ˜Š





Donate to igorskyflyer




@igorskyflyer










## ๐Ÿ“ƒ Table of contents

- [Demonstration](#-demonstration)
- [Usage](#-usage)
- [TypeScript](#typescript)
- [Create a d.ts file](#create-a-dts-file-recommended)
- [Add to the entrypoint](#add-to-the-entrypoint)
- [JavaScript](#javascript)
- [Implementation](#-implementation)
- [API status table](#implementation-table)
- [Examples](#-examples)
- [Changelog](#-changelog)
- [License](#-license)
- [Related](#-related)
- [Author](#-author)

---




## ๐ŸŽฌ Demonstration

Here's `magic-querySelector` in action.


https://github.com/user-attachments/assets/eb0b6695-be60-4a6e-b935-5996b40c5d78


Without magic-queryselector




https://github.com/user-attachments/assets/2251724d-98d7-4deb-8a82-8b4f0a6a6e31


With magic-queryselector




Visual Studio Code theme used in the demonstration is Kai ๐ŸŒŠ An external link.

---

## ๐Ÿ•ต๐Ÿผ Usage

Install it by executing:

```shell
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:

- [TypeScript](#typescript)
- [JavaScript](#javascript)

---

### TypeScript

If you want to use it with TypeScript, you need to copy the following code

```ts
import '@igor.dvlpr/magic-queryselector'
```

and then do one of the following:


**\[ 1st option ]**
#### Create a `d.ts` file (*recommended*)


> [!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`
```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 to the entrypoint

Add the code snippet you copied to the top of your entrypoint/main TypeScript file.


`index.ts`
```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.
>

---

### JavaScript


> [!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.
>

---

## ๐Ÿค– Implementation

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` of the same type, e.g. `NodeListOf`.
>
> For brevity this table only shows the types for `querySelector()`.
>
> Read more about [selector structure](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors/Selector_structure) ![External link](https://raw.githubusercontent.com/igorskyflyer/igorskyflyer/main/assets/external.svg) and [selectors and combinators](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_selectors/Selectors_and_combinators) ![External link](https://raw.githubusercontent.com/igorskyflyer/igorskyflyer/main/assets/external.svg) on `MDN`.
>


The following table shows which selectors/combinators are supported along with the inferred return types for the given examples.


##### Implementation table
|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` |

Table 1. implementation table




***(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](https://developer.mozilla.org/en-US/docs/Web/CSS/Column_combinator) An external link)


---

## โœจ Examples

`main.js`
```js
const video = document.querySelector('div#app > video') // HTMLVideoElement | null
const audios = document.querySelectorAll('div#app > audio') // NodeListOf

if(video) {
video.src = '' // now we can access all properties and methods
}

if(audios.length > 0) {
audios[0].src = '' // ๐Ÿ˜€๐Ÿ˜€๐Ÿ˜€
}
```

---

## ๐Ÿ“ Changelog

๐Ÿ“‘ Changelog is available here: [CHANGELOG.md](https://github.com/igorskyflyer/npm-magic-queryselector/blob/main/CHANGELOG.md).

---

## ๐Ÿชช License

Licensed under the MIT license which is available here, [MIT license](https://github.com/igorskyflyer/npm-magic-queryselector/blob/main/LICENSE).

---

## ๐Ÿงฌ Related

[@igor.dvlpr/jmap](https://www.npmjs.com/package/@igor.dvlpr/jmap)

> _๐Ÿ•ถ๏ธ Reads a JSON file into a Map. ๐ŸŒป_


[@igor.dvlpr/extendable-string](https://www.npmjs.com/package/@igor.dvlpr/extendable-string)

> _๐Ÿฆ€ ExtendableString allows you to create strings on steroids that have custom transformations applied to them, unlike common, plain strings.. ๐Ÿช€_


[@igor.dvlpr/unc-path](https://www.npmjs.com/package/@igor.dvlpr/unc-path)

> _๐Ÿฅฝ Provides ways of parsing UNC paths and checking whether they are valid. ๐ŸŽฑ_


[@igor.dvlpr/duoscribi](https://www.npmjs.com/package/@igor.dvlpr/duoscribi)

> _โœ’ DรบรถScrรญbรฎ allows you to convert letters with diacritics to regular letters. ๐Ÿค“_


[@igor.dvlpr/node-clone-js](https://www.npmjs.com/package/@igor.dvlpr/node-clone-js)

> _๐Ÿงฌ A lightweight JavaScript utility allowing deep copy-by-value of nested objects, arrays and arrays of objects. ๐Ÿช_

---

### ๐Ÿ‘จ๐Ÿปโ€๐Ÿ’ป Author
Created by **Igor Dimitrijeviฤ‡** ([*@igorskyflyer*](https://github.com/igorskyflyer/)).