Перейти к основному содержимому
← ⚡ JavaScript & the browser·Модуль C5 · Урок 9
ЗаданиеTwo functions in starter. (1) Add @param/@returns JSDoc to the exported greet(name) — it's a library API. (2) Leave bumpCount(state) alone — it's local. Call greet("world") and bumpCount({n:0}) and log both.

When NOT to add types

100 XP7 min
Теория

Types have a real cost

JSDoc is free at runtime, but it adds visual noise and maintenance. Every @param is one more thing that has to stay in sync with the code. For some files, the cost beats the benefit.

Skip types when

  1. Tiny scripts. One-off automation, glue between two CLI tools, a 20-line cron script. Reading the code IS reading the types.
  2. Prototype / spike. You're exploring an idea you might delete tomorrow. Don't pay the typing tax before knowing the shape sticks.
  3. Single-use callbacks. arr.map(x => x * 2) doesn't need @param {number} x — the context tells you what x is.
  4. REPL experiments. Console snippets, code golf, throwaway demos.

Reach for types when

  • The function is called from 3+ places.
  • The return value is consumed by code you don't own.
  • The parameters could legitimately be more than one shape (union types).
  • You're touching a function whose bug is "wrong arg type came in".

The shape of the rule

> Types pay for themselves at the interface, not the implementation.

Type /* lib */ exports. Skip types on /* local */ helpers a few lines below.

The task

Look at the two functions in the starter. One is a 4-line internal helper. The other is a module-exported library function called from elsewhere. Add JSDoc to the EXPORTED one only.

🔒

Зарегистрируйся чтобы кодить

Теория открыта всем. Интерактивный редактор, живой превью и проверка открываются с 7-дневным бесплатным trial — нужна карта, отмена в любое время.

Регистрация — бесплатный trial →

Первые 10 уроков в каждом треке — бесплатные. Карта не нужна.

← НазадСледующий урок →

Получай один совет по Python или веб-разработке — каждый день в почте

Коротко, без воды, без спама. Отписаться — в один клик.