JSDoc: how to define a type that's equal to object's property names- like Typescript's keyof?

Viewed 274

I'm looking for a way to get an object's property names as a type using JSDoc.

Let there be a typedef of an object named Record

/**
 * @typedef {{
      date: string,
      a1: string,
      a2: string,
   }} Record
 */

I want the variable fields to be documented as equal to the object's properties- which means for this example: 'date' | 'a1' | 'a2'

/**
 * @type {*keyof Record??*} in this case, this will be equal to @type {'date' | 'a1' | 'a2'}
 */
let fields = 'a1';

Typescript provides the keyof keyword, which does just that. unfortunately, the system I'm working on does not support typescript :\

1 Answers
/*
* @typedef {{
* date: string,
* a1: string,
* a2: string,
* }} Record
*/

/** @type {Record['date']|Record['a1']|Record['a2']}*/
let fileds = 'a1';

Try this

-New Edit-

If you don't want to list up the types. Maybe you want to try this way.

/** @typdef {string} RecordProperty */

/*
* @typedef {{
* date: RecordProperty,
* a1: RecordProperty,
* a2: RecordProperty,
* }} Record
*/

/** @type {RecordProprty} */
let fileds = 'a1';

Related