首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeScript字符串文字类型上的TSDocs

TypeScript字符串文字类型上的TSDocs
EN

Stack Overflow用户
提问于 2019-06-25 15:18:21
回答 2查看 209关注 0票数 1

我正在尝试找出一种在TypeScript中让TSDocs处理字符串文字类型声明的方法。

例如:

代码语言:javascript
复制
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'时不显示注释。

有没有办法做到这一点?

EN

回答 2

Stack Overflow用户

发布于 2021-04-24 22:46:26

这是你可以做的另一个选择:

代码语言:javascript
复制
  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");

票数 2
EN

Stack Overflow用户

发布于 2021-04-23 23:38:45

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

代码语言:javascript
复制
/**
 * 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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56748729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档