说我有这样的反对意见:
const Companies = {Mazda : {revenue:50000, employees:1000},
Honda: {revenue: 102324, employees:2031}}
此功能:
const totalRevenue = (companies) =>
Object.values(companies)
.map(companyObject => companyObject.revenue)
.reduce((total, companyRevenue)=> total + companyRevenue)
我如何使用JSDoc来告诉函数
totalRevenue
companies
对象是它拥有的每个键的对象,其值类型为{revenue: Number, employees: Number}
我对WebStorm可以理解的东西特别感兴趣。
答案 0 :(得分:3)
您可以使用{Object.<string, number>}
表示法描述对象中的每个键。在此处查看更多语法示例:http://usejsdoc.org/tags-type.html
针对您的特定情况:
/**
* @param {Object.<string, {revenue: number, employees: number}>} companies
*/
const totalRevenue = (companies) =>
Object.values(companies)
.map(companyObject => companyObject.revenue)
.reduce((total, companyRevenue)=> total + companyRevenue)