Search code examples
typescriptjsdoctsd-jsdoc

JSDoc generic typedef for TypeScript function


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)


Solution

  • 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,
    ) { }