Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use typescript utility types with jsdoc?

When I used jsdoc into vscode. I saw I could use typescript utility types. For example:

/**
 * Func 1
 * @param {number} x 
 */
function func1(x) {
    return [x, x]
}

/**
 * Func 2
 * @param {ReturnType<func1>} x  // Here !
 */
function func2(x) {
    return x[0];
}

So I wonder if this is a correct usage of jsodc. And if there is a way to create my own type transformers, e.g. create one to unwrap promise resolve type.

like image 531
Koala-Gentil Avatar asked Nov 27 '25 23:11

Koala-Gentil


1 Answers

To create new a type transformer which unwrap promise :

/**
 * @template T
 * @typedef  {T extends Promise<infer Value> ? Value : T} PromiseValue
 */

Or with type-fest :

/**
 * @template T
 * @typedef {import('type-fest').PromiseValue<T>} PromiseValue 
 */
like image 157
Koala-Gentil Avatar answered Nov 29 '25 14:11

Koala-Gentil



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!