π Search Terms
"jsdoc", "@template", "constraint", "constraints", "conditional types", "conditional types"
β
Viability Checklist
β Suggestion
Support constraints in JSDoc @template tags, based on the thread: c5fd2f14#r165189007
π Motivating Example
From https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
type Flatten<T> = T extends any[] ? T[number] : T;
// Extracts out the element type.
type Str = Flatten<string[]>;
type Str = string;
// Leaves the type alone.
type Num = Flatten<number>;
I suggest the following equivlent JS code is supported (or something similar):
/**
* @template {T extends any[] ? T[number] : T} T
* @param {T} arg
*/
const Flatten = arg => /* (Implementation not important) */
// type string
const Str = Flatten(['hello']);
// type number
const Num = Flatten(42);
π» Use Cases
-
What do you want to use this for?
Write generic functions in JavaScript that support conditional type constraints.
-
What shortcomings exist with current approaches?
This is currently not supported within JSDocs.
-
What workarounds are you using in the meantime?
Import a type with conditional type constraints defined in a d.ts file may work around this.
π Search Terms
"jsdoc", "@template", "constraint", "constraints", "conditional types", "conditional types"
β Viability Checklist
β Suggestion
Support constraints in JSDoc
@templatetags, based on the thread: c5fd2f14#r165189007π Motivating Example
From https://www.typescriptlang.org/docs/handbook/2/conditional-types.html
I suggest the following equivlent JS code is supported (or something similar):
π» Use Cases
What do you want to use this for?
Write generic functions in JavaScript that support conditional type constraints.
What shortcomings exist with current approaches?
This is currently not supported within JSDocs.
What workarounds are you using in the meantime?
Import a type with conditional type constraints defined in a
d.tsfile may work around this.