[英]Typescript for groups of optional values as function parameters
我有一个函数,可以将任意数量的分组值作为参数。 我认为最好的方法是传递数组(但可以接受其他选择)。 每个数组可以具有1-4个必须匹配特定类型的值。 我对此进行编码的尝试如下所示:
const transition = (...properties: [string, number?, string?, number?][]): string => {
// Do something
};
用法如下所示:
transition(['color', 2]);
transition(['color', 2], ['background']);
transition(['color', 2], ['background', 2, 'ease']);
transition(['color', 2], ['background'], ['opacity', 3, 'ease']);
我的编译器抛出与?
相关的错误?
类型的用法:
SyntaxError: Unexpected token, expected ","
如果我删除?
我没有得到编译错误,但它随后希望传递数组中的所有值。
有一个简单的解决方案吗?
// Declare a type for arguments
export interface ITransitionProperties {
color: string,
value?: number,
type?: string,
value2?: number
}
// and use it in defining the type of argument
const transition = (ArgumentName: ITransitionProperties[]): string => {
// Do something
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.