Skip to content

Add support for conditional type constraints within jsdoc @template tagΒ #57149

@johnemau

Description

@johnemau

πŸ” 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

  1. What do you want to use this for?
    Write generic functions in JavaScript that support conditional type constraints.

  2. What shortcomings exist with current approaches?
    This is currently not supported within JSDocs.

  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    QuestionAn issue which isn't directly actionable in code

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions