我正在尝试找到一种方法,使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>
有什么办法吗?
答案 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");
答案 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');