JSPM

@keupoz/strict-queryselector

1.1.0
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 52
  • Score
    100M100P100Q55571F
  • License MIT

A simple typeguard wrapper around document.querySelector for TypeScript

Package Exports

  • @keupoz/strict-queryselector

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@keupoz/strict-queryselector) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

strict-queryselector

Usage

import { querySelector } from "@keupoz/strict-queryselector";

// Same examples applies to querySelectorAll
// Gets body element with type of HTMLBodyElement
const body = querySelector("body", HTMLBodyElement);

// Throws TypeError as body element is not instance of HTMLDivElement
const bodyDiv = querySelector("body", HTMLDivElement);

// Throws Error if element doesn't exist
// If it exists, checks if it's instance of specified type
const meaningOfLife = querySelector(".life-meaning", HTMLDivElement);

Wrappers

There are wrapper functions that allows to wrap the type guard around custom element. And they return the same function as regular querySelector.

const bodyQuerySelector = wrapQuerySelector(document.body);