Skip to content

How to use with JS with Types (JSDoc + TSC)? #42

Description

@coolaj86

What's the syntax to have this import in a normal JavaScript file and have it work with JSDoc (tsc)?

What I've tried

/** @import('typed-query-selector/strict') */ // doesn't seem to update the types
/** @import type {} from 'typed-query-selector/strict' */  // E: 'from' expected.

Problem

let $input = $("input");
if (!$input) {
  throw new Error("no input selected");
}
f.value = "foo"; // E: Property 'value' does not exist on type 'Element'.

Sample Code

Here's how I'm trying to use it, in full:

/** @import('typed-query-selector/strict') */

/**
 * Select first matching element, just like console $
 * @param {String} cssSelector
 * @param {ParentNode} [$parent=document]
 */
function $(cssSelector, $parent = document) {
  let $child = $parent.querySelector(cssSelector);
  return $child;
}

/**
 * Select all matching child elements as a JS Array, just like console $$
 * @param {String} cssSelector
 * @param {ParentNode} [$parent=document]
 */
function $$(cssSelector, $parent = document) {
  let $children = $parent.querySelectorAll(cssSelector);
  let children = Array.from($children);
  return children;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions