実現したいこと
1. 名前付き引数を用いる。
2. デフォルト引数を設定することで、型注釈を省略する。
発生している問題・分からないこと
コンパイルエラーが発生しました。
エラーメッセージ
error
1Type 'any[] | { [x: string]: any; }' is not an array type or a string type. 2 3
該当のソースコード
TypeScript
1特になし
試したこと・調べたこと
- teratailやGoogle等で検索した
- ソースコードを自分なりに変更した
- 知人に聞いた
- その他
上記の詳細・結果
TypeScript
1// 編集前のコード 2function sumScore(...scores:number[]): number { 3 let total = 0; 4 5 for (const score of scores) total += score; 6 7 return total; 8} 9 10const total6 = sumScore(10, 9, 9, 10, 8, 9); 11console.log(`6人の審査員の合計点: ${total6}`);
TypeScript
1// 失敗例 2function sumScore({...scores = []}) { 3 let total = 0; 4 5 for (const score of scores) total += score; 6 7 return total; 8} 9 10const total6 = sumScore({scores: [10, 9, 9, 10, 8, 9]}); 11console.log(`6人の審査員の合計点: ${total6}`);
補足
特になし

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2025/09/09 08:16