繁体   English   中英

TypeScript 字符串文字类型的 TSDocs

[英]TSDocs on TypeScript String Literal 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'时,这些工具当前不显示注释。

有没有办法做到这一点?

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

  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");

在此处输入图片说明

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

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

您还可以查看以下 TypeScript Playground

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM