"Imperatively" assign a predefined @callback signature to a function definition?

Viewed 25

If I have a function signature defined either in JSDoc with @callback or in a .t.ds library using type, can I somehow use JSDoc to "attach" that signature to multiple function definitions?

/**
 * @callback ruleDefiner
 * @param {RuleDefinitionBuilder} builder
 */

Then:

/** @thisFunctionIsA {ruleDefiner} */
function spellfix(builder) {

    // The IDE should know that builder is a RuleDefinitionBuilder
    const {depends, produces, cmd, T} = builder;

    const input = depends("foo.txt");
    const output = produces("foo.spellfixed.txt");
    cmd(T`spellfix ${input} > ${output}`);
}

/** @thisFunctionIsA {ruleDefiner} */
function sign(builder) {

    // The IDE should know that builder is a RuleDefinitionBuilder
    const {depends, produces, cmd, T} = builder;

    const input = depends("foo.spellfixed.txt");
    const output = produces("foo.signed.txt");
    cmd(T`sign ${input} > ${output}`);
}

What should I use instead of pseudo-JSDoc @thisFunctionIsA?

1 Answers

Yes, you can, using the @type tag.

/**
 * @callback ruleDefiner
 * @param {RuleDefinitionBuilder} builder
 */

/** @type {ruleDefiner} */
function spellfix(builder) {

    // The IDE should know that builder is a RuleDefinitionBuilder
    const {depends, produces, cmd, T} = builder;

    const input = depends("foo.txt");
    const output = produces("foo.spellfixed.txt");
    cmd(T`spellfix ${input} > ${output}`);
}

/** @type {ruleDefiner} */
function sign(builder) {

    // The IDE should know that builder is a RuleDefinitionBuilder
    const {depends, produces, cmd, T} = builder;

    const input = depends("foo.spellfixed.txt");
    const output = produces("foo.signed.txt");
    cmd(T`sign ${input} > ${output}`);
}

EDIT: If this is truly/strictly for JSDoc, see this JSDoc documentation for more. However, if you, like me, only use JSDoc with JavaScript for IntelliSense in your editor, you probably want this TypeScript documentation (which JavaScript IntelliSense is actually using) instead.

Related