简体   繁体   English

如何将 typescript 类型添加到数组到 function 参数中?

[英]How to add typescript type to an array into function argument?

I have a problem with defining array type: Code below perfectly works:我在定义数组类型时遇到问题:下面的代码完美运行:

const messageCustomStyles: Array<keyof IAlertMessage> = [
            'font',
            'margin',
            'padding'
        ];
return getCustomStylesFrom(styles, 'message', messageCustomStyles);

what I want to do is to move this array into function argument with its type, like so:我想要做的是将此数组移动到具有其类型的 function 参数中,如下所示:

return getCustomStylesFrom(
  styles, 
  'message', 
  ['font', 'margin', 'padding']: Array<keyof IAlertMessage>);

But this throws ts error saying 'Expected 3 arguments, but got 4.'但这会引发 ts 错误,提示“预期 3 arguments,但得到 4。” Looks like after ':' it is counting type as a new parameter.看起来像在':'之后它正在将类型计数为新参数。

How can I fix this?我怎样才能解决这个问题?

You can do it like this:你可以这样做:

return getCustomStylesFrom(
  styles, 
  'message', 
  ['font', 'margin', 'padding'] as Array<keyof IAlertMessage>);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM