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)

1 Answer 1

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,
) { }
Sign up to request clarification or add additional context in comments.

2 Comments

Much better, but there is a subtle difference: mjs with JSdoc: js function inline<TypeS extends boolean, TypeT extends unknown, TypeF extends unknown>(S?: TypeS | undefined, T?: TypeT | undefined, F?: TypeF | undefined, ...args: any[]): TypeS extends true ? TypeT : TypeF ts file: ts function inline<TypeS extends boolean, TypeT = unknown, TypeF = undefined>(S?: State | undefined, T?: TypeF | undefined, F?: TypeF | undefined): TypeS extends true ? TypeT : TypeF
seems like JSDoc ignores the templates like: ``` * @template {*} TypeT = unknown * @template {*} TypeF = undefined ``` and adds and ...args: any[]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.