簡體   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