1
votes

How can I document the property of an object in JSDoc / VSCode when it contains a period?

For example:

/**
 * @type {PersonObject}
 */
let personObject = {
   "first.last": "John Doe", // optional
   "age": 31
}

/**
 * @typedef {Object} PersonObject
 * @property {String} [first.last]
 * @property {Number} age
 */

{ "message": "Type '{\"first.last\": string; age: number; }' is not assignable to type 'PersonObject'.\n Object literal may only specify known properties, and '\"first.last\"' does not exist in type 'PersonObject'.", "source": "ts", }

1

1 Answers

1
votes

Assuming you're using Closure Compiler, or at least a JSDoc flavor that its compatible with:

I suggest using the new @typedef syntax it seems to work in this example.

/**
 * @typedef  {{
 *            first.last:string,
 *            age:number,
 *          }}
 */
var PersonObject;

/**
 * @type {PersonObject}
 */
let personObject = {
   "first.last": "John Doe", // optional
   "age": 31
}

Note that when you change 'first.last' in either place, Closure will emit a warning:

JSC_TYPE_MISMATCH: initializing variable
found   : {age: number, firstzzzzz: string}
required: {age: number, first.last: string}
missing : [first.last]
mismatch: [] at line 14 character 19
let personObject = {