CohortX
Блог

TypeScript for beginners: why types

2 августа 2026 г. · 2 мин чтения · Читать по-русски · Антон Молотило

The problem types solve

In JavaScript a variable can hold anything. That's convenient while a project is small and painful once it grows.

The usual troubles: a string passed where a number was expected; accessing a field that doesn't exist; a function that sometimes returns an object and sometimes nothing. All of it surfaces at run time, often in front of a user.

Types move such errors to the moment you write the code. The editor underlines the problem immediately, before anything runs.

The second effect is underrated: types act as documentation. Reading a function's signature tells you what it takes and returns without reading its body.

The minimum to start

Primitive types.

let title: string = "Note";
let count: number = 0;
let done: boolean = false;

Often you needn't annotate: assigning a value immediately infers the type.

Arrays and objects.

const tags: string[] = ["work", "home"];

type Note = {
  id: number;
  title: string;
  body?: string;
};

The question mark marks an optional field.

Functions.

function shorten(text: string, limit: number): string {
  return text.length > limit ? text.slice(0, limit) + "…" : text;
}

Unions. A value may be one of several:

type Status = "new" | "in_progress" | "done";

One of the most useful features: instead of a string that could contain anything, you get a strict set of permitted values.

What not to do at the start

Don't switch off the checks. The temptation to reach for the "any" type at the first difficulty is strong, but that gives you ordinary JavaScript with extra punctuation.

Don't get carried away with advanced constructs. The type language is powerful and you can write astonishing things in it. Working tasks almost never need that.

Don't annotate everything by hand. Much is inferred. Annotate the boundaries: function arguments, return values, data structures.

Should a beginner learn it

Not first. JavaScript to a working level comes first: without understanding the language, types are just a stream of irritating errors.

But don't postpone indefinitely. Typed codebases appear in postings ever more often, and it's easier to get used to types on small projects than later on someone's large one.

The sensible moment is once you've built a couple of plain-JavaScript projects and started losing track of your own data shapes. That feeling is the signal.

Starting painlessly

Take an existing small project of yours and convert gradually: describe the data structures first, then function arguments. Full strictness from day one is a reliable way to hate the tool.

After a week of that, the point usually lands: the editor starts helping, and "wrong field" errors disappear by themselves.

Хватит читать — пора делать

На CohortX можно найти команду под пет-проект и получить тот самый опыт, о котором спрашивают на собеседовании.

Похожие статьи