Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic keys in jsdoc typedef

Is it possible to have dynamic keys (prop names) in a jsdoc typedef? I'm imagining this would look something like the example below (which does not work).

@typedef {Object} Foo
@property {string} bar
@property {*} *

Passing properties not listed in the typedef e.g. {baz: 0} makes typescript upset with something like,

Argument of type '{ bar: string; baz: number; }' is not assignable to parameter of type 'Foo'. Object literal may only specify known properties, and 'baz' does not exist in type 'Foo'


Using the method proposed by @jcalz Object.<string, *> seems closer to the ideal output but resulted in a strange output
@typedef {Object} Foo
@property {number} bar
@property {Object.<string, *>}

output:

type Foo = {
    bar: number;
    (Missing): {
        [x: string]: any;
    };
}
like image 562
J'e Avatar asked May 20 '19 17:05

J'e


1 Answers

You can also use normal TS syntax in JSDOC.

See next example:

/**
 * 
 * @param {Record<string, string> & {bar:string}} arg
 */
const foo = (arg) => {}

You can even use utility types:

/**
 * 
 * @param {Partial<{age:number}>} arg 
 */
const partial = (arg) => { }

You can find more utils here

like image 150
captain-yossarian Avatar answered Oct 15 '22 08:10

captain-yossarian