TSDocs on TypeScript String Literal Types

Viewed 310

I'm trying to figure out a way to have TSDocs work with a string literal type declaration in TypeScript.

For example:


type InputType = 
/** the TSDoc doesn't works for num1, but i'd like it to */
'num1' |
/** the TSDoc doesn't work for num2, but i'd also like it to */
'num2'

export const test = (...input: InputType[]) => {
    return input
}

tac('num1')

I'd like 'num1' & 'num2 to show the TSDoc in VSCode or other tools with the comment, but the tools do not currently show the comment when typing in 'ma1'.

Is there any way to do this?

2 Answers

Here is another option you can do:

  type InputType = "num1" | "num2";

  /**
   *
   * @param {"num1"} input description for num2
   */
  function test(...input: ["num2", ...string[]]): ["num2"];
  /**
   *
   * @param {"num1"} input description for num1
   */
  function test(...input: ["num1", ...string[]]): ["num1"];
  /**
   *
   * @param {string} input description for numX
   */
  function test(...input: [string, ...string[]]): ["numX"];
  function test<T extends InputType, R extends T[]>(...input: [...R]) {
    return input;
  }


  
  test("num4");

enter image description here

Is it correct that you want something like this:

/**
 * the TSDoc works for num1
 */
type A = 'num1';

/**
 * the TSDoc works for num2
 */
type B = 'num2';

export const test = (...input: (A | B)[]) => {
    return input
}

test('num1');

You can also check the following TypeScript Playground.

Related