TypeScript "as const" assertion is 🔥🔥🔥

  Рет қаралды 33,881

Dave Gray

Dave Gray

Жыл бұрын

Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGrayWebDevRoadmap
Typescript "as const" assertion is 🔥
Javascript const is not really defining a constant, but Typescript's "as const" assertion fixes this for JS.
🚩 Subscribe ➜ bit.ly/3nGHmNn
📬 Course Updates ➜ courses.davegray.codes/
❓ Questions - Please post them to my Discord ➜ / discord
☕ Buy Me A Coffee ➜ www.buymeacoffee.com/davegray
👇 Follow Me On Social Media:
Github: github.com/gitdagray
Twitter: / yesdavidgray
LinkedIn: / davidagray

Пікірлер: 44
@bballer3010
@bballer3010 Жыл бұрын
Kinda a new dev, I’ve been doing development for websites for about a year and a half now. Never had a senior developer. Thank you so much for answering these small questions that don’t appear to be a big deal, until they are. Much love for stumbling on this channel and hope it grows to help developers everywhere.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Glad I could help!
@0xtz_
@0xtz_ Жыл бұрын
I'm working on a project and the senior is using Typescript so I need to use it too and maaan it's amazing no bugs pain etc u fix all in the editor ... Now no vanilla JavaScript in big projects 👌🏼
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Excellent!
@emmanueltech9705
@emmanueltech9705 Жыл бұрын
Dave on his way to 100k subs you deserve more man 💯
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Thank you!
@thebowshock7729
@thebowshock7729 Жыл бұрын
Hey Dave, there are also cases such as these: type Fruit = "Orange" | "Apple" const myVariable: Fruit = "Orange" I have noticed there are cases when TS doesn't like that const value and treats it as a nondescript string, but doing _as const_ seems to lock it down: const myVariable: Fruit = "Orange" as const
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Right on!
@DiegoBM
@DiegoBM Жыл бұрын
You left it in a cliff-hanger! Good move! Hehe
@Its.all.goodman
@Its.all.goodman Жыл бұрын
That's interesting, never thought this before because Im currently learning typescript. Im subscribing for more content like this
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Glad it was helpful!
@abdulazeez.98
@abdulazeez.98 Жыл бұрын
Excellent tip!
@jampy42
@jampy42 Жыл бұрын
This is really cool, thanks!
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Welcome!
@undefined_object
@undefined_object 18 күн бұрын
There are object.freeze and seize methods to make the objects real const
@jasondotjson34
@jasondotjson34 9 ай бұрын
Yeah, but I like to use object as constants like this: const obj:object = {message:"hello"}; I cannot override variable but i can override properties. So it's kind of constant cuz it cannot be re-assignment. But yeah it's kinda cool operator to be honest.
@starlines07
@starlines07 Жыл бұрын
Cool info 👍
@olteanumihai1245
@olteanumihai1245 Жыл бұрын
its equivalent with final
@mikamanelka1326
@mikamanelka1326 Жыл бұрын
haha, perfect. needed this today
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Right on! 🤘
@supportic
@supportic Жыл бұрын
Object.freeze(obj) in JS. You're welcome
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Already knew about that. The same functionality but not self-documenting like TypeScript.
@catbb
@catbb Жыл бұрын
🙏
@sney2002
@sney2002 Жыл бұрын
Are nested objects also constant?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Yes, try it out. Typescript will flag it even layers deep.
@sayonara3465
@sayonara3465 Жыл бұрын
But what the useful from that? In js when i getten a result from database or form etc, how i can push it to array const, and yeah i want the array as const not let or var coz i don’t want to change the name of variable, so again i don’t understand what the point or useful from this?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
This is a concept from functional programming to understand that is also applied to libraries/frameworks like React - Do not "mutate" (aka modify) the original state, but instead, create a new object, array, etc.
@Alex-bc3xe
@Alex-bc3xe Жыл бұрын
my old trick
@efari
@efari Жыл бұрын
const *in JS* doesn't mean "constant value" it means "constant assignment"
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
This video explains that "const" in JS is not actually a "constant" as others may think about from other programming languages - so we're probably in agreement here. What does constant mean in programming languages? "In computer programming, a constant is a value that should not be altered by the program during normal execution, i.e., the value is constant." Reference: en.wikipedia.org/wiki/Constant_(computer_programming)
@efari
@efari Жыл бұрын
@@DaveGrayTeachesCode yea i meant "const in JS doesn't mean..." i was indeed agreeing with you, but expanding the explanation :)
@scroogemcduckenjoyer
@scroogemcduckenjoyer Жыл бұрын
Interesting, why they named that tho?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
JS has some weird parts. Using const does "constantly" refer to the same memory pointer for objects.. but the weird part compared to most languages is that the inner properties (or elements if an array) can still be changed. TS provides a way to curb that behavior with the "as const" assertion.
@PeidosFTW
@PeidosFTW Жыл бұрын
It's the same behaviour for the final keyword in java. Declaring an arraylist as final will still allow you to change the contents, pretty standard behaviour for many common languages
@simply-dash
@simply-dash Жыл бұрын
all that does is add Object.freeze?
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Typescript adds type safety. You need to heed the warnings it provides. If you do not want to use TS, Object.freeze might be the way to go.
@CodingAqyanoos
@CodingAqyanoos 10 ай бұрын
A const is a constant variable And that's correct. The point of const is that you can't create another variable with the same name.
@Maxim9575
@Maxim9575 9 ай бұрын
const always const, don't deceive people!
@WesleyDuSellone
@WesleyDuSellone Жыл бұрын
Great! const as const
@ScriptRaccoon
@ScriptRaccoon Жыл бұрын
This is not entirely correct. 1) TypeScript won't prevent constants from being reassigned in runtime. It "only" gives you a warning in development. 2) There is a JavaScript method to achieve this: Object.freeze(). This prevents reassignment also in runtime.
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
Given more than the 60 seconds of a short, I can give a deeper discussion. That said, you are basically saying "If you use Typescript, but choose to ignore it..." and while true, I didn't think I needed to say that. You can say the same for anything TS does as all Javascript is valid when you ignore the TS warnings.
@okonkwo.ify18
@okonkwo.ify18 Жыл бұрын
First as usual
@theonlyarjun
@theonlyarjun Жыл бұрын
Realised this the hard way, by taking down the prod
@DaveGrayTeachesCode
@DaveGrayTeachesCode Жыл бұрын
That's the school of hard knocks! Ouch!
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 108 М.
КАХА и Джин 2
00:36
К-Media
Рет қаралды 3,9 МЛН
Be kind🤝
00:22
ISSEI / いっせい
Рет қаралды 15 МЛН
顔面水槽をカラフルにしたらキモ過ぎたwwwww
00:59
はじめしゃちょー(hajime)
Рет қаралды 31 МЛН
TypeScript Generics are EASY once you know this
22:21
ByteGrad
Рет қаралды 120 М.
The TypeScript feature I never use
10:49
Andrew Burgess
Рет қаралды 11 М.
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 191 М.
PROOF JavaScript is a Multi-Threaded language
8:21
Beyond Fireship
Рет қаралды 260 М.
Type your functions in TypeScript and SAVE TIME
8:31
ThePrimeTime
Рет қаралды 47 М.
Typescript Generics | Beginners Tutorial with Examples
24:43
Dave Gray
Рет қаралды 16 М.
TypeScript Utility Types You Must Learn
14:07
TomDoesTech
Рет қаралды 16 М.
Why Signals Are Better Than React Hooks
16:30
Web Dev Simplified
Рет қаралды 448 М.
I Cannot Believe TypeScript Recommends You Do This!
7:45
Web Dev Simplified
Рет қаралды 158 М.
КАХА и Джин 2
00:36
К-Media
Рет қаралды 3,9 МЛН