BeyondCodeBootcamp / js-with-types-jsdoc-tsc-starter

A simple, but non-trivial example of getting the most from JSDoc + tsserver (Type Linting without TypeScript)
Mozilla Public License 2.0
17 stars 1 forks source link

Docs: How to escape special characters in JSDoc property names #3

Open coolaj86 opened 3 years ago

coolaj86 commented 3 years ago

You'd never put # as an object name for a sane reason... but sometimes you have to deal with CSV. According to the JSDoc docs, it seems like this should do the job:

/**
 * @typedef {Object} EmpCsv
 * @property {string} Emp#
 * @property {string} "First Name"
 */

But I get errors...

Asked at https://stackoverflow.com/questions/68946492/how-can-i-escape-special-characters-names-in-jsdoc-typescript.

coolaj86 commented 3 years ago

There's an alternate syntax that works:

/**
 * @typedef {{
 *   "Emp#": string,
 *   "First Name": string,
 * }} EmpCsv
 */

And the .d.ts counterpart should look like this:

interface EmployeeCsv {
  "Emp#": string,
  "First Name": string,
}
dleetr commented 1 year ago

Is there a way to add descriptions to a member with this alternate style? I cannot get vscode to be happy with any variation I attempt

/**
 * @typedef {{
 *   "Emp#": string, // want to give context to Emp# here
 *   "First Name": string,
 * }} EmpCsv
 */

I tried a couple of workarounds to no avail. Is there any functioning syntax where we can add normal, full-fledged annotation with description to a special character field name?

Edit: The best I've done so far is doing the jankiness below for the special character fields.

/**
 * @typedef FooHelperProps
 * @property {string} fooField - Can still annotate regular fields at least
 *
*/
/**
 * @typedef {FooHelperProps & {":specialField": any[]}} FooProps // Can't annotate ":specialField"
 */