【问题标题】:TSDocs on TypeScript String Literal Types关于 TypeScript 字符串文字类型的 TSDocs
【发布时间】:2019-11-06 23:28:46
【问题描述】:

我正在想办法让 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' 时,这些工具目前不显示评论。

有什么办法吗?

【问题讨论】:

  • 你到底想在这里记录什么?
  • @VLAZ,我有几百个不同的选项可以将值输入到test,我希望能够为每个可能的输入提供上下文

标签: javascript typescript types


【解决方案1】:

你想要这样的东西对吗:

/**
 * 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

【讨论】:

  • 不,这些 cmets 不会通过 tsdocs 传递到其他任何地方。
【解决方案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");

【讨论】:

  • 看来你写的很多)
猜你喜欢
  • 1970-01-01
  • 2017-03-31
  • 2017-11-13
  • 1970-01-01
  • 2017-08-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-31
相关资源
最近更新 更多