繁体   English   中英

如何键入定义解构箭头 function 表达式 arguments?

[英]How to type define destructured arrow function expression arguments?

我正在尝试做一点 function 将单词的第一个字母大写,但 typescript 让我很伤心,我不知道如何输入定义它。

你能帮我输入定义这个 function:

const capitalize = ([firstLetter, ...rest]) => firstLetter.toUpperCase() + rest.join('')

正如@Bergi 正确指出的那样, string不是数组。

因此,您希望用户调用您的方法,例如:

操场

const capitalize = ([
  firstLetter,
  ...rest
]: string[]): string => firstLetter.toUpperCase() + rest.join('');

console.log(capitalize('my string'.split('')));

要么您使用其他方式来大写。

操场

const capitalize = (str: string): string => str.length ? str.substr(0, 1).toUpperCase() + str.substr(1) : '';

console.log(capitalize('my string'));

暂无
暂无

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

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