0

I'm using JSDoc in a mjs to mimic a ts function that I have. The mjs is checked using the TypeScript compiler. The original function (overloaded), from ts file is like this:

export function inline<TypeS extends boolean, TypeT = unknown, TypeF = undefined>(S?: TypeS, T?: TypeT, F?: TypeF): TypeS extends true ? TypeT : TypeF {}

export function inline<TypeS extends boolean, TypeT = unknown, TypeF = undefined>(
    S?: TypeS,
    T?: TypeT,
    F?: TypeF,
): unknown {}

I'm trying something like:

/**
 * @callback Inline
 * @param {TypeS} S
 * @param {TypeT} T
 * @param {TypeF} F
 * @return {TypeS extends true ? TypeT : TypeF}
 * @type {Inline}
 */
export function inline(
    S,
    T,
    F,
) {}

That is almost what I expect, but, I haven't the generic types and default values of generics. Is there a way to do it properly with JSDoc?

The content that I'm using to trying to follow to solve is:

But tryng by pieces, without overload first, and then, maybe, with overload, if possible (I really don't understand well this thing, it's tricky for me)

Adrian Miranda
  • 315
  • 3
  • 9

1 Answers1

1

you forgot to define the templates

/**
 * @template {boolean} TypeS
 * @template [TypeT=unknown]
 * @template [TypeF=undefined]
 * @param {TypeS} [S]
 * @param {TypeT} [T]
 * @param {TypeF} [F]
 * @return {TypeS extends true ? TypeT : TypeF}
 */
export function inline(
    S,
    T,
    F,
) { }
Adrian Miranda
  • 315
  • 3
  • 9
Antal Alin
  • 156
  • 1
  • 8
  • Much better, but there is a subtle difference: mjs with JSdoc: ```js function inline(S?: TypeS | undefined, T?: TypeT | undefined, F?: TypeF | undefined, ...args: any[]): TypeS extends true ? TypeT : TypeF ``` ts file: ```ts function inline(S?: State | undefined, T?: TypeF | undefined, F?: TypeF | undefined): TypeS extends true ? TypeT : TypeF ``` – Adrian Miranda Feb 25 '23 at 12:19
  • 1
    seems like JSDoc ignores the templates like: ``` * @template {\*} TypeT = unknown * @template {\*} TypeF = undefined ``` and adds and ...args: any[] – Adrian Miranda Feb 25 '23 at 12:27