This saved me HOURS of refactoring time

  Рет қаралды 46,575

Matt Pocock

Matt Pocock

2 жыл бұрын

Learning about the difference between optional properties (name?: string) and undefined unions (name: string | undefined) can give you an amazing productivity boost for certain refactors.
Become a TypeScript Wizard with Matt's upcoming TypeScript Course:
www.totaltypescript.com/
Follow Matt on Twitter
/ mattpocockuk

Пікірлер: 48
@grgry06
@grgry06 2 жыл бұрын
Hi, I subscribed to your channel because I love your content! And I also liked your more straightforward approach of teaching advanced TypeScript. After almost 2 years of writing in TypeScript, I wanna learn more so yeah.
@sickhowl7951
@sickhowl7951 2 жыл бұрын
Awesome content mate, keep it up!
@enfieldli9296
@enfieldli9296 2 жыл бұрын
Sweet tip! So solid
@user-fg6ng7ej6w
@user-fg6ng7ej6w 2 жыл бұрын
cool series of tips
@nimeshgurung6600
@nimeshgurung6600 2 жыл бұрын
Gonna buy tickets to that Advanced Typescript show on the first day it releases. Learnt so much on these tiny trailers, can't wait for the whole damn movie. When is it being released?
@brokula1312
@brokula1312 2 жыл бұрын
Wow, this never crossed my mind to be honest. Mostly because I thought these two "?" and "undefined" are the same thing. Today I found out there is a built in type called "PropertyKey" which is just a union of string, number and Symbol. In my code I was totally unaware or just completely forgot about the Symbol type, but with this I don't have to care any longer. Thank you Matt. I'm sharing your channel with my colleagues.
@austincodes
@austincodes Жыл бұрын
Similar but yeah not the same
@MaNiarus
@MaNiarus 2 жыл бұрын
Matt when you will release your TS Course? Can't wait :)
@paulblinder5729
@paulblinder5729 Жыл бұрын
Very Nice!!
@justintaddei
@justintaddei 2 жыл бұрын
Hi Matt. Absolutely love these short videos! May I ask which extension you use for that awesome inline auto complete?
2 жыл бұрын
This is probably GitHub Copilot.
@RTCDigitalS
@RTCDigitalS 2 жыл бұрын
@ R.I.P
@christopheanfry2425
@christopheanfry2425 Жыл бұрын
Really nice trick I’m not as good as I want but if I have undefined I really have to deal with it or just add ? In my key so my code will not show error. Is that correct ? Sorry but just starting typescript but love how you explain things. Thanks
@sigma_z
@sigma_z 7 ай бұрын
Maybe I don't understand but by saying that you'll change it later, is that not the same as being ok with adding technical debt?
@r3d-soft
@r3d-soft 10 ай бұрын
I like the behavior of “| undefined” to keep things explicit. Even if I end up passing undefined, the next caller needs to think about what value should be passed rather than being accidentally swallowed by “?”. Using this approach on react components gives a nice auto completion experience as well
@sq5321
@sq5321 10 ай бұрын
i think "null" is made for explicitly saying it's not a value, undefined is the same but in an implicit way
@bobobo1673
@bobobo1673 Жыл бұрын
Interesting THANK!
@kevinrobertandrews
@kevinrobertandrews Жыл бұрын
Cool tip! What's a use case for being explicit about making a property undefined?
@mattpocockuk
@mattpocockuk Жыл бұрын
If, for instance, you need to ensure that the key always exists for Object.keys.
@kevinrobertandrews
@kevinrobertandrews Жыл бұрын
@@mattpocockuk Ah, cool idea, thanks!!
@ShantanuAryan67
@ShantanuAryan67 Жыл бұрын
@@mattpocockuk why not use null?
@PeteC62
@PeteC62 Жыл бұрын
​@@ShantanuAryan67 say for example you have an optional string parameter to a function. Its type would be string | undefined. That means you could assign it directly to a property of type string | undefined. If you made the property string | null, you'd get a type mismatch error.
@danielschwartz516
@danielschwartz516 9 ай бұрын
I temporarly remove the ?, check each error then return the ?
@thydevdom
@thydevdom Ай бұрын
Wizadry!
@thedelanyo
@thedelanyo Жыл бұрын
Yeah true,, I really like the undefined rather than adding? to type defs.
@codefinity
@codefinity Жыл бұрын
Feels really weird to be passing `undefined` explicitly like that. At least in JS standards we usually say to avoid using `undefined` - use `null` if you must.
@austincodes
@austincodes Жыл бұрын
Null makes more sense if your data is coming from an external source like a server or something. In this case the undefined type makes sense since it makes you pass in some value in every function. You could just pass null into the function and have the type be undefined though
@tuananhdo1870
@tuananhdo1870 4 күн бұрын
explicit is always good
@ColinRichardson
@ColinRichardson 2 жыл бұрын
is this a re-upload?
@mattpocockuk
@mattpocockuk 2 жыл бұрын
It's probably a cross-post from Twitter, like all of these are!
@ColinRichardson
@ColinRichardson 2 жыл бұрын
@@mattpocockuk AHHH, okay.. I knew I had seen this before, but couldn't find it in the history..
@yuriygerasimovich4187
@yuriygerasimovich4187 2 жыл бұрын
Just wow o_0
@uzayrsyed6293
@uzayrsyed6293 Жыл бұрын
Thanks for the tip but why would anyone want to not have it be explicitly stated that role is undefined?
@gasparsigma
@gasparsigma 10 ай бұрын
In many cases you just don't care if it's optional or if it's explicitly undefined because your code surrounding it will be the same. But sometimes you don't even have that choice, if let's say an API has optional properties in the backend it'll not be sent explicitly undefined, it'll just be omitted. Rather than parse and map it to explicitly undefined a optional with the question mark makes more sense
@gasparsigma
@gasparsigma 10 ай бұрын
Another example is optional props in react. Imagine if setting a style required that all properties you do not want to set were required, it'd be a nightmare
@nomadshiba
@nomadshiba Жыл бұрын
i would rather use null instead of undefined undefined should only be returned and never should be set by the user for anything
@MaPhongBa129
@MaPhongBa129 2 жыл бұрын
Smart way haha
@ufukbakan4741
@ufukbakan4741 Жыл бұрын
there is no advantage of it. intellisense already shows optional properties too and you can optional chain with ? operator. thats not even a tip
@altairbueno5637
@altairbueno5637 Жыл бұрын
If the language were any good, you wouldn’t need weird tricks to refactor things lol
@dmytro-skh
@dmytro-skh 9 ай бұрын
good example of why TS is bad, pure type gymnastics with no real value in this case
@babri1402
@babri1402 5 ай бұрын
Not at all. On the contrary it is a good example of TS aiding you to double check where you might need to make changes.
@PavelLitkinBorisovich
@PavelLitkinBorisovich Жыл бұрын
Just do not understand how it happened, how everybody got so obsessed about typescript, that it became a must in enterprise? It is just nice linter, nothing more than that...
@thearcticmonkey
@thearcticmonkey Жыл бұрын
It is not just nice linter. You can ship products and features with less bugs, easier to maintain, refactor...
@jeromesnail
@jeromesnail Жыл бұрын
Lol
@zhehuizhou
@zhehuizhou Жыл бұрын
lol
@pigozs
@pigozs Жыл бұрын
😂😂😂
The SECRET POWER of indexed access types - Advanced TypeScript
1:19
8 TypeScript Tips To Expand Your Mind (and improve your code)
10:54
Must-have gadget for every toilet! 🤩 #gadget
00:27
GiGaZoom
Рет қаралды 11 МЛН
Василиса наняла личного массажиста 😂 #shorts
00:22
Денис Кукояка
Рет қаралды 9 МЛН
Вечный ДВИГАТЕЛЬ!⚙️ #shorts
00:27
Гараж 54
Рет қаралды 4,4 МЛН
MEGA BOXES ARE BACK!!!
08:53
Brawl Stars
Рет қаралды 33 МЛН
My Favorite TypeScript Tips and Tricks
10:21
Lachlan Miller
Рет қаралды 5 М.
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 111 М.
I Cannot Believe TypeScript Recommends You Do This!
7:45
Web Dev Simplified
Рет қаралды 162 М.
7 Awesome TypeScript Types You Should Know
8:57
Josh tried coding
Рет қаралды 77 М.
Refactoring a React component - Design Patterns
15:19
Cosden Solutions
Рет қаралды 91 М.
Фишки TypeScript о которых ТЫ НЕ ЗНАЛ!
30:55
This TypeScript Trick Blew my Mind
6:17
Josh tried coding
Рет қаралды 36 М.
TypeScript Utility Types You Must Learn
14:07
TomDoesTech
Рет қаралды 16 М.
25 nooby Python habits you need to ditch
9:12
mCoding
Рет қаралды 1,7 МЛН
Must-have gadget for every toilet! 🤩 #gadget
00:27
GiGaZoom
Рет қаралды 11 МЛН