RecipesChapter 1: Project Setup

Type-Checking JavaScript

Recipe 1.1 from The TypeScript Cookbook

/**
 * @typedef {Object} Article
 * @property {number} price
 * @property {number} vat
 * @property {string} string
 * @property {boolean=} sold
 */

/**
 * Now we can use Article as a proper type
 * @param {[Article]} articles
 */
function totalAmount(articles) {
  return articles.reduce((total, article) => {
    return total + addVAT(article);
  }, 0);
}
Open in TypeScript Playground →