Загрузка...

Union Types in TypeScript: One Value, Two Types #coding #codingtips

A value isn't always just one type. A union type lets a parameter be a number OR a string, and a typeof check tells TypeScript which one it is in each branch. That's narrowing.

In under a minute:
→ Why label("free") fails when the parameter is a number
→ How number | string allows both
→ Why toFixed breaks on a union
→ How typeof narrows the type inside an if
→ What's left after the if: just number

Next up: literal types.

Follow @BiteSizedCoding for a new bite-sized TypeScript tip every day.

#typescript #javascript #webdev

Видео Union Types in TypeScript: One Value, Two Types #coding #codingtips канала BiteSized Coding
Яндекс.Метрика
Все заметки Новая заметка Страницу в заметки
Страницу в закладки Мои закладки
На информационно-развлекательном портале SALDA.WS применяются cookie-файлы. Нажимая кнопку Принять, вы подтверждаете свое согласие на их использование.
О CookiesНапомнить позжеПринять