Typescript 是否声明与给定函数的参数列表兼容的数组类型?
对于Typescript 是否声明与给定函数的参数列表兼容的数组类型?,typescript,Typescript,对于ItemArgs,您可以使用: 实际上,我第一次正确地编写了map()调用,然后我又过度缩写了。感谢您的快速回答,我现在不知道为什么在文档中找不到参数。令人惊讶的是,它实际上可以在TypeScript中实现,而不引入新的语言特性。 function item( tag: string, kind?: string, name?: string ) : string { return ` * ${tag}${kind ? ` [${kind}]`
ItemArgs
,您可以使用:
实际上,我第一次正确地编写了
map()
调用,然后我又过度缩写了。感谢您的快速回答,我现在不知道为什么在文档中找不到参数。令人惊讶的是,它实际上可以在TypeScript中实现,而不引入新的语言特性。
function item(
tag: string,
kind?: string,
name?: string
) : string {
return ` * ${tag}${kind ? ` [${kind}]` : ""}${name ? ` ${name}` : ""}`;
}
/*
* Here, I need to declare the type ItemArgs that will be an array type
* compatible with the argument list of the item() function.
*
* How do I?
*/
function block(
tag: string,
kind?: string,
name?: string,
...items: ItemArgs[]
) : string {
// the items.map() call must compile
return ["/**", item(tag, kind, name), ...items.map(item), " */"].join("\n");
}
// ... and this must compile too
const jsDocBlock : string = block(
'typedef', 'Object', 'Foo',
['property', 'boolean', 'should'],
['property', undefined, 'something']
);
function block(
tag: string,
kind?: string,
name?: string,
...items: Parameters<typeof item>[]
): string {
return [
"/**",
item(tag, kind, name),
...items.map(args => item(...args)),
" */"
].join("\n");
}
const jsDocBlock: string = block(
'typedef', 'Object', 'Foo',
['property', 'boolean', 'should'],
['property', undefined, 'something']
);
console.log(jsDocBlock);
// "/**
// * typedef [Object] Foo
// * property [boolean] should
// * property something
// */"