0

Salesforce and JavaScript community!

I'm looking for the best way to type the result of a querySelectorAll call that targets custom LWC elements. My query is specifically aimed at selecting lightning-input and lightning-input-field components within a form, like so:

const input = form.querySelector("lightning-input");

The only workable option I've found so far is to make my own LightningInput.d.ts type to capture the custom params and actions uniquely available to a LWC.

Questions:

  1. Is there a way to use JSDoc to more accurately type the result of querySelectorAll when targeting specific Salesforce LWC components without resorting to custom type definitions?
  2. If custom types are the only solution, what would be the best practice for defining these types to include the specific properties or methods of the LWC components mentioned?
4
  • 1
    JSDoc certainly allows comment-only typedefs that you could use of this.
    – Phil W
    Commented Mar 15 at 14:14
  • I'm more looking for a type that Salesforce provides OOB, which I'm assuming exists somewhere...? Similar to the types they provide for ObjectInfoRepresentation (i.e. the return type of the uiApi's getObjectInfo function) Commented Mar 15 at 17:04
  • 1
    Yeah, I've not seen one, but the docs would let you create it for yourself reasonably straight-forwardly. You just have to decide which JS file to add it to...
    – Phil W
    Commented Mar 15 at 17:09
  • Great point. Mind adding an answer so I can ✅? Commented Mar 15 at 23:07

1 Answer 1

1

I am not aware of any OOTB definitions for this, though it is straight forward to use the standard documentation as a source of information, such as lightning-input's specification, to create your own JSDoc comment block that defines a typedef you can then use in the JSDoc for your own functions.

All you have to do is decide what file you should add that comment block to...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .