Skip to content

A typescript querySelector that can be used for static client side apps to get elements via querySelector, that you know exist. (Without strict null checks).

Notifications You must be signed in to change notification settings

N8python/safeQuerySelector

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 

Repository files navigation

safeQuerySelector

A typescript querySelector that can be used for static client side apps to get elements via querySelector, that you know exist. (Without strict null checks).

Let's say you have a p element with an id of "test". You want to set its innerHTML to "Hello World". You know it exists, but TypeScript can't be sure, and makes you perform strict null checks, and cluttering pointless if statments in your code. safeQuerySelectorfixes that: Before safeQuerySelector:

const elem = document.querySelector("#test");
if (elem) {
  elem.innerHTML = "Hello World";
}

If you have multiple elements involved in a program, this can become cluttered. But, with safeQuerySelector, your code looks like:

const elem = safeQuerySelector("#test");
elem.innerHTML = "Hello World";

No need to worry about strict null checks! If the query dosen't return anything, you get back a div element that won't do anything, as it has not been added to the DOM.

About

A typescript querySelector that can be used for static client side apps to get elements via querySelector, that you know exist. (Without strict null checks).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published