有关TypeScript字符串文字类型的TSDocs

时间:2019-06-25 07:18:20

标签: javascript typescript types

我正在尝试找到一种方法,使TSDocs在TypeScript中使用字符串文字类型声明。

例如:


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')

我希望'num1''num2在VSCode或其他带有注释的工具中显示TSDoc,但是当前输入'ma1'时这些工具不显示注释。 / p>

有什么办法吗?

2 个答案:

答案 0 :(得分:2)

这是您可以执行的另一种选择:

  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

答案 1 :(得分:0)

您想要这样的东西是否正确:

/**
 * 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');

您也可以勾选the following TypeScript Playground