I Can’t Believe This TS Feature Has No Documentation
1:27

I Can’t Believe This TS Feature Has No Documentation

Web Dev Simplified 23.04.2026 59 994 просмотров 2 370 лайков

Machine-readable: Markdown · JSON API · Site index

Поделиться Telegram VK Бот
Транскрипт Скачать .md
Анализ с AI
Описание видео
🌎 Find Me Here: My Blog: https://blog.webdevsimplified.com/ My Courses: https://courses.webdevsimplified.com/ Patreon: https://www.patreon.com/WebDevSimplified Twitter: https://twitter.com/DevSimplified Discord: https://discord.gg/7StTjnR GitHub: https://github.com/WebDevSimplified CodePen: https://codepen.io/WebDevSimplified #Shorts

Оглавление (1 сегментов)

Segment 1 (00:00 - 01:00)

The TypeScript documentation isn't great, but in this case, there's absolutely no documentation for this incredible feature. Take for example this function first. It's a generic function that takes in an array of elements and returns to me the first element. In our case, we're passing an array of 1 2 and three. So, I would expect the return type of this to be either the value of 1 2 or 3 cuz that's the only things in my array. But if I hover over this num, you can see it actually has a return type of number instead of 1 2 or three. In order to make sure I get the correct return type, I would need to pass as const onto the end of this, which essentially says that this is a constant readonly array. And that gives me a little bit of a stricter type. And now when I hover over my number, you can see I get 1 2 or three, just like I would expect by stricting this down to a more strict type. But what if I don't want to have to be forced to pass as const every single time I call this function? I just know I want to take the strictest type every time. This is where passing a const generic comes in handy. you can pass const before the name of any generic. And essentially what that does is it acts like you passed as const at the end of whatever thing you're passing into this function. So in our case, you can see our number is now 1 2 or 3. It automatically counts this as a constant variable and gives us that stricter typing. Now in this particular use case, it's not super important, but when you're doing more advanced TypeScript projects, this type of feature is incredibly important. And if you want to see a use case of a real world project where I use this, I will link a video down at the bottom of your screen that uses this const feature heavily inside of a project for very particular use cases.

Другие видео автора — Web Dev Simplified

Ctrl+V

Экстракт Знаний в Telegram

Экстракты и дистилляты из лучших YouTube-каналов — сразу после публикации.

Подписаться

Дайджест Экстрактов

Лучшие методички за неделю — каждый понедельник