TypeScript 5.3 is a no-brainer upgrade

  Рет қаралды 87,358

Matt Pocock

Matt Pocock

Күн бұрын

Пікірлер: 159
@mattpocockuk
@mattpocockuk Жыл бұрын
For those stumbling in late, I missed this feature for this video because it wasn't mentioned in the release notes: www.totaltypescript.com/the-typescript-5-3-feature-they-didn-t-tell-you-about
@blizzy78
@blizzy78 Жыл бұрын
sick animations in this video, well done
@CodeWithMario
@CodeWithMario Жыл бұрын
Matt, your tutorials are consistently so clear, polished And easy to follow. thank you 🙇
@RefactoringRyan
@RefactoringRyan Жыл бұрын
Psyched for your book. Always appreciate these quick updates Matt!
@magnusmarkling
@magnusmarkling Жыл бұрын
Great content! I did notice some lip-sync issues at the end of the clip.
@arnaudchefdeprojet4184
@arnaudchefdeprojet4184 Жыл бұрын
As usual, great job Matt ! Thank’s 👍
@Metruzanca
@Metruzanca Жыл бұрын
All great features. Thanks for the breakdown as usual Matt.
@hugodsa89
@hugodsa89 Жыл бұрын
With the switch(true) in order for people to usually get the "right" type out, usually I have instead done a typeguard to assert it first and then return it. It is more cumbersome, but it is also accurate and can easily be put on multiple small functions that can also be reused later on. Thanks for sharing :)
@prionkor
@prionkor Жыл бұрын
Why would someone return from an if statement then add "else if" below? "if return" pattern is more clean and easy to read solution then switch!
@HerrThomasE
@HerrThomasE Жыл бұрын
Thank you very much. Good explanations and good video!!! I have subcribed.
@Metruzanca
@Metruzanca Жыл бұрын
Inlay hints is a feature that the rust language server has had for a while. I've been wondering when other languages would gain the same thing.
@Svish_
@Svish_ Жыл бұрын
Upgrading Typescript _should_ be very little hassle, but unfortunately we're still stuck on older versions because of dependencies claiming not to support Typescript greater than x version... \*sigh\*
@feldinho
@feldinho Жыл бұрын
That truly sucks! I wish more library authors would embrace semantic versioning. At least this is not python, where some major libraries have dependencies pinned to patch versions.
@anarchoyeasty3908
@anarchoyeasty3908 Жыл бұрын
Make PR's for those packages to remove the limitation. Or fork it and use your fork.
@Svish_
@Svish_ Жыл бұрын
Easier said than done. Packages that are very slow to update their peer dependencies, are usually also the ones who are very slow to fix things in general or just slow to accept PRs. Forking and using my own fork is also not something I want to do, especially if it's for a company or professional work. So I tend to instead either just live with it, or try to get rid of the library, which of course is also often easier said than done...
@RefactoringRyan
@RefactoringRyan Жыл бұрын
Have you considered hosting your own forked versions of deps with upgraded underlying dependencies as your project requires? Depending on the package it might not be the best use of time, but I've done this in the past when absolutely necessary.
@jeffreysmith9837
@jeffreysmith9837 Жыл бұрын
Typescript itself doesn't follow semantic versioning. Maybe fix that first
@alian714
@alian714 7 ай бұрын
2:50 why not do `switch (typeof input)`?
@struggopuggo
@struggopuggo Жыл бұрын
I'm confused by switch true narrowing as it's a handy hack in JS but could they not have backed an actual pattern matching spec?
@mattpocockuk
@mattpocockuk Жыл бұрын
Someday, but switch(true) is a great use of existing tools
@struggopuggo
@struggopuggo Жыл бұрын
@@mattpocockuk yeah, it's certainly an improvement and nice to have but feels like this advocates for this use case. I guess I just feel funny about it, even though I've made use of it.
@andrueanderson8637
@andrueanderson8637 Жыл бұрын
Agreed, the switch true thing is gross and frankly bad for the language. It's the worst kind of mismatch: a semantic mismatch. Makes it harder for new people to learn.
@rand0mtv660
@rand0mtv660 Жыл бұрын
@@andrueanderson8637 it's something that Javascript supports so it makes sense that Typescript supports it properly. Yeah it's gross, but it's not like it didn't exist before this update.
@DemanaJaire
@DemanaJaire Жыл бұрын
@@andrueanderson8637 I've been a programmer and linguist for over 15 years and I'm getting sick and tired of that "harder for new people to learn" talking point that's brought up every time when discussing a language feature that requires some effort to understand. The majority of the language users are not newbies and as a newbie, you're not supposed to learn everything at once in the first month of your learning journey. And once you're not a newbie, you're expected to understand trivial stuff like switch(true), because it's not rocket science as it uses two basic language features (1. switch case accepts values, 2. expressions evaluate a single value) and it's actually really clever. And there are cases where switch(true) looks much neater than if statement (for example, when you have multiple unrelated simple conditions that should evaluate the same expression). And I'm pretty sure the people who say it's gross would be the first ones to commit atrocities such as using `as any as SomeType`, or mutating a variable inside a disgusting while loop. Skill issue if I ever saw one.
@zwanz0r
@zwanz0r Жыл бұрын
Using switch true should be a crime with a 10 years in jail penalty
@0xtz_
@0xtz_ Жыл бұрын
The goto def is 🔥
@lasindunuwanga5292
@lasindunuwanga5292 Жыл бұрын
Thanks
@FunctionGermany
@FunctionGermany Жыл бұрын
that switch should be "switch (typeof input) ..." what about this? does it work with type narrowing?
@maelstrom57
@maelstrom57 Жыл бұрын
`typeof null === "object"` is the issue.
@feldinho
@feldinho Жыл бұрын
@@maelstrom57 I missed that detail. You're right.
@KuroBayashi
@KuroBayashi Жыл бұрын
I'm really confused by the `switch(true)`, I would never have thought of using a `switch` like that, or even put `true` in the `switch`. What are the benefit over something like: ```ts switch ( typeof input ) { case 'string': case 'number': return ( input ); case 'object': return ( !! input ? input : undefined ); } ```
@mattpocockuk
@mattpocockuk Жыл бұрын
It means you can layer in complex if statements in a nice, readable format. If you need to do 100 different checks, a switch statement will end up being much nicer than 100 if/else if's.
@lapissea1190
@lapissea1190 Жыл бұрын
@@mattpocockuk I'm honestly not sure what is better/worse here. Needing to write else ifs or needing to deal with the horrible C style switch that is begging me to forget a break statement
@lapissea1190
@lapissea1190 Жыл бұрын
I don't really get why TS limits itself to the JS switch. They could very transpile something like a statement switch in to the C style switch. For example: ``` when { typeof thing == "number" && thing%2 == 0 -> { handleEvenNumber(); } thing === "hello" -> { greetBack(); } } ```
@oidualx
@oidualx Жыл бұрын
@@mattpocockuk if you need to do 100 checks in a single function, the code really needs some refactoring, I'm not convinced that the switch is the solution here
@sabinpandey
@sabinpandey Жыл бұрын
Awesome
@Dev-Siri
@Dev-Siri Жыл бұрын
from early, to now we are almost at the mid typescript times (x.5 to x.7 release) and now typescript 4 seems so last year
@Danhosaur
@Danhosaur Жыл бұрын
My hero 💖
@kieranmckenzie2995
@kieranmckenzie2995 Жыл бұрын
Been using jump to variable type in vim for ages (gY normally)
@mattpocockuk
@mattpocockuk Жыл бұрын
It's also been in VSCode for a long time, but just not in the inlay hints.
@patrick.miharisoa
@patrick.miharisoa Жыл бұрын
inlay hints 🔥 Home sweet home rust devs
Жыл бұрын
switch (typeof input) would make more sense to me, but i saw the github issues about this. i don't like the switch (true) syntax, seems strange to switch on a literal
@NamesAreVacuous
@NamesAreVacuous Жыл бұрын
What’s the issue with using switch(typeof input)
@ChillAutos
@ChillAutos Жыл бұрын
typeof null is object
Жыл бұрын
@@CodeWithMario im not sure you wanted to reply to my comment or just post a regular comment
@CodeWithMario
@CodeWithMario Жыл бұрын
@ 🤦 thanks for pointing that out
@rcnhsuailsnyfiue2
@rcnhsuailsnyfiue2 Жыл бұрын
Why two return points in the switch(true) example? Why not just add a third case to one single return block?
@mattpocockuk
@mattpocockuk Жыл бұрын
Good point! Just wanted to demonstrate that you _could_ do different things if you wanted to.
@rcnhsuailsnyfiue2
@rcnhsuailsnyfiue2 Жыл бұрын
@@mattpocockukcool, just checking. PhpStorm normally shouts at me when I do that with match() statements :)
@lordkedos
@lordkedos Жыл бұрын
if(!input && input !== 0) return; switch(typeof input) { case 'string': case 'number': return input; case 'object': return input.toString(); }
@TheBswan
@TheBswan Жыл бұрын
That's not the same function. Guard clause should be if (!input) return, except that would also fail for func(0). Also don't stringify the object. Correct way to rewrite is: if (typeof input === "string") return input if (typeof input === "number") return input if (typeof input === "object" && !!input) return input
@lordkedos
@lordkedos Жыл бұрын
@@TheBswan yes, ! was incorrect. Thank you
@Sylvadorr
@Sylvadorr Жыл бұрын
What, wasn't there also the “value || throw new Error()”? I mainly waited for this feature to come
@mattpocockuk
@mattpocockuk Жыл бұрын
They were planning to champion that in TC39, not sure where that ended up.
@RedStone576
@RedStone576 Жыл бұрын
pattern matching at home:
@maziasty
@maziasty Жыл бұрын
That unnecessary else statement definitely makes the case better for switch(true).
@Brawaru
@Brawaru Жыл бұрын
With query parameters you can have an env.d.ts file define them (e.g., `declare module "*.svg?url" { ... }`), how would that work with import asser- attributes?
@mattpocockuk
@mattpocockuk Жыл бұрын
Not sure yet, but I'm sure it will be able to work the same way given enough iteration.
@TheOneAnOnlyGuy
@TheOneAnOnlyGuy Жыл бұрын
You can't switch(typeof input) in TS?
@rezaz7167
@rezaz7167 Жыл бұрын
what does switch (true) snipper you used in the example is for?! returning input if its object string or number? why not write them whole in one IF statement?
@feldinho
@feldinho Жыл бұрын
Hey Matt! Great work as always! Can you help me with a thing? I love lodash but their typings sucks ass. Once I tried typing the pipe/combine functions just for fun but was completely stuck. Can I suggest you do a video working on that kind of stuff? I think it would yield great content, and there's lots of cases like that in the wild if format pans out. :)
@zorzysty
@zorzysty Жыл бұрын
Use remeda instead of lodash. It's fully typed. That is remeda, not ramda.
@feldinho
@feldinho Жыл бұрын
@@zorzysty Thanks for the tip! Anyways, I think it's a nice problem to tackle, as creating a pipe function using reduce is very easy but typing it is very tricky.
@thi-m10
@thi-m10 Жыл бұрын
@@zorzysty Thanks for the tip. I used ramda for about a year and I had a hard time to add types to most of the functions. I'm definitely checking this out
@disgruntled_fish
@disgruntled_fish Жыл бұрын
Ts-belt is another good alternative that's worth checking out
@HerrThomasE
@HerrThomasE Жыл бұрын
One Question: Which tool you are using to make the videos?
@mattpocockuk
@mattpocockuk Жыл бұрын
Keynote
@TheBswan
@TheBswan Жыл бұрын
if (typeof input === "string") return input if (typeof input === "number") return input if (typeof input === "object" && !!input) return input Switch true looks a lot dumber when you write the original function clearly
@barssavas9938
@barssavas9938 Жыл бұрын
I have a large code base written in TSX.Right now I am using typescript version 4.1.1 how to I upgrade it to 5.3 ? Íf I try to upgrade any chance I see errors, crashes or conflicts ?
@mattpocockuk
@mattpocockuk Жыл бұрын
Just bump it and see what happens. TS tends to upgrade slow. I doubt you'll have many issues.
@barssavas9938
@barssavas9938 Жыл бұрын
@@mattpocockuk I will notify subscribers in here with result's
@AmodeusR
@AmodeusR Жыл бұрын
that import attribute looks quite verbose. If it's supposed to specify a type, why not something like import image from "./folder/img.png" asType X or something like it?
@adiadiadi
@adiadiadi Жыл бұрын
what do you use to animate the code snippets?
@chris94kennedy
@chris94kennedy Жыл бұрын
powerpoint
@mattpocockuk
@mattpocockuk Жыл бұрын
Keynote
@adiadiadi
@adiadiadi Жыл бұрын
oh wow! totally and it comes built-in! I need to figure this out...
@barneylaurance1865
@barneylaurance1865 Жыл бұрын
Do the interactive inlay hints affect non VS-code development? E.g. using WebStorm or another Jetbrains IDE?
@mattpocockuk
@mattpocockuk Жыл бұрын
I think Webstorm _will_ be able to do it, given a bit of catch up time.
@barneylaurance1865
@barneylaurance1865 Жыл бұрын
@@mattpocockuk Thanks!
@pharmajoe990
@pharmajoe990 Жыл бұрын
Webstorm should by default open hints if the cursor or pointer is over the type or issue. CMD+b will navigate to the definition on Mac, not sure what the default mapping is on other OSes.
@AleksandarStefanovic
@AleksandarStefanovic Жыл бұрын
Interactive Inlay Hints is not a Typescript change, is it?
@mattpocockuk
@mattpocockuk Жыл бұрын
Yep, the TS team needed to make some changes to tsserver to help VSCode get it working.
@psybitcoin
@psybitcoin Жыл бұрын
Which tool do you use to create those code animations?
@tieTYT
@tieTYT Жыл бұрын
I didn't understand what inlay hints changes. What about that experience is new? Edit: Oh I think i get it. Those were inferred types you could hover over, right? That sounds like the most useful of the 3
@spam1712
@spam1712 Жыл бұрын
Thanks for confirming my hate for JavaScript
@jtw-r
@jtw-r Жыл бұрын
No. if you use switch(true), i’m closing out your pull request and blocking you on github
@vitalysuper3193
@vitalysuper3193 Жыл бұрын
I agree with guys, the switch true example is horrible you could just use multiple if without else since you use return!
@wasd3108
@wasd3108 Жыл бұрын
why would you use inlay hints when you can, or rather SHOULD type it yourself? when I turn it on I dont know whether I wrote it or it's the hint lmfao, would have to change font or something about it it's good for javascript, but all I see for typescript, it's making it look like you typed it, it's hard to tell even, how is this not a bad practice XD
@mattpocockuk
@mattpocockuk Жыл бұрын
Yeah I also don't use them. Others do, and this is a nice upgrade for them.
@MartinWauligmann
@MartinWauligmann Жыл бұрын
According to some recent polls on Twitter, most devs seem to prefer inferred types over explicit types. With inferred types, inlay hints are useful since you then don't need to hover over a variable to find out its type. Also they are quite visually distinct from normal text, at least in IntelliJ.
@AnthonySBD
@AnthonySBD Жыл бұрын
im so confused as a non full time web dev. you do switch true everywhere instead of switch(typeof input) mind blown at how thats not the baseline and people just do switch true lmao.
@mattpocockuk
@mattpocockuk Жыл бұрын
Of course you can do switch (typeof input) too. switch (true) is just one choice among many.
@tamaniphiri
@tamaniphiri Жыл бұрын
Bro is the owner of TS😅🔥🙌great content
@EverydayKarma
@EverydayKarma Жыл бұрын
haha yup he iss
@nordern1
@nordern1 Жыл бұрын
People are overreacting to that "switch (true)" bit. It doesn't suggest this as a syntax you should use, it was simply a case the compiler didn't cover correctly before and now it does.
@IDOLIKIofficial
@IDOLIKIofficial Жыл бұрын
switch(true) makes me wanna hate JS even more 😢
@K.Huynh.
@K.Huynh. Жыл бұрын
Switch (true) is new for me ❤❤
@jessypouliot8374
@jessypouliot8374 Жыл бұрын
please don't use switch true, thx
@DSOlaLG
@DSOlaLG Жыл бұрын
switch true... really ?
@oidualx
@oidualx Жыл бұрын
For the love of god, DO NOT USE switch(true). What's the point? It's just a more cumbersome if-else statement
@DirkLuijk
@DirkLuijk Жыл бұрын
I like it, reminds me of the Kotlin “when” blocks. Kinda pattern matching style.
@netero8888
@netero8888 Жыл бұрын
if you have 10 conditions to handle, I'd rather have them in a switch(true) than in an if-else concatenation, just cleaner to the eye and less cluttered, in my opinion
@DigitalEyePCs
@DigitalEyePCs Жыл бұрын
Agreed if there are race or overlapping states it makes for odd behavior
@iMagicIAm
@iMagicIAm Жыл бұрын
I think this is a bad take. switch(true) definitely has it's uses. Makes code much cleaner than the alternative and it's easy to follow. Statements are evaluated from top to bottom stopping on the first truthy statement. Not rocket science.
@mbuzogan
@mbuzogan Жыл бұрын
I had to deal with quite complicated logic combining 4 or more variables sometimes just some of them sometimes all, not all were dummy simple variables, some of those conditions needed to be resolved on runtime ... imagine reviewing such code or returning to it after few months :D switch(true) is to me much more readable with no impact on performance
@TheStone190
@TheStone190 Жыл бұрын
I've taught myself to avoid else clauses whenever possible, I found it to be a good practice to keep track of my state. I can see myself replacing some of my if statements with switch (true) statements when they have bunch of conditions.
@Gunzy83
@Gunzy83 Жыл бұрын
No brainer unless you are using Pulumi.
@chigozie123
@chigozie123 Жыл бұрын
You are taking a shortcut by not specifying the return type of that switch true function. In a properly configured typescript project with some basic strict type check options enabled, the compiler should complain that a return type is not specified and that it's missing a final return statement.
@sunofabeach9424
@sunofabeach9424 Жыл бұрын
⚡: operator ==== for _very_ precise comparisons added operator === now does only approximate comparisons for example: 3 === "3.1" false 3 === 3.1 true 3 ==== 3.1 false
@RedStone576
@RedStone576 Жыл бұрын
why??
@lapissea1190
@lapissea1190 Жыл бұрын
The biggest problem with typescript is that you are actually just writing javascript. Don't think they can fix that
@manon.grivot
@manon.grivot Жыл бұрын
Am I the only person in this world that hates inlay hints and always disable all form of hints in my IDEs? I find them troublesome, they add things that I did not write and aren't part of the source code, I find them more distracting than anything else and I absolutely hate that they sometime shift the rest of the line by 0.5 characters or even weirder values.
@nickwoodward819
@nickwoodward819 Жыл бұрын
Can I PUH-LEASE just finish a project before my -framework- -library- language is updated? 😆 /jk
@nickwoodward819
@nickwoodward819 Жыл бұрын
@_Karlsson 😄
@barneylaurance1865
@barneylaurance1865 Жыл бұрын
@_Karlsson A finished project is one that no-one uses any more.
@marusdod3685
@marusdod3685 Жыл бұрын
bruh switch(true) is literally pattern matching
@robinparadise
@robinparadise Жыл бұрын
Nothing interesting for me 🫤
Enums considered harmful
9:23
Matt Pocock
Рет қаралды 218 М.
Generics: The most intimidating TypeScript feature
18:19
Matt Pocock
Рет қаралды 190 М.
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
How to treat Acne💉
00:31
ISSEI / いっせい
Рет қаралды 108 МЛН
BAYGUYSTAN | 1 СЕРИЯ | bayGUYS
36:55
bayGUYS
Рет қаралды 1,9 МЛН
I Cannot Believe TypeScript Recommends You Do This!
7:45
Web Dev Simplified
Рет қаралды 181 М.
5 Useful F-String Tricks In Python
10:02
Indently
Рет қаралды 344 М.
C++ Super Optimization: 1000X Faster
15:33
Dave's Garage
Рет қаралды 334 М.
TypeScript 5.3 что в релизе?
12:17
PurpleSchool | Anton Larichev
Рет қаралды 8 М.
The TSConfig Cheat Sheet
5:36
Matt Pocock
Рет қаралды 39 М.
as const: the most underrated TypeScript feature
5:38
Matt Pocock
Рет қаралды 129 М.
This Algorithm is 1,606,240% FASTER
13:31
ThePrimeagen
Рет қаралды 863 М.
Solving the 'any' problem is harder than you think
5:04
Matt Pocock
Рет қаралды 22 М.
This TypeScript Trick Blew my Mind
6:17
Josh tried coding
Рет қаралды 38 М.
TypeScript 5.5 is a BANGER
9:16
Matt Pocock
Рет қаралды 68 М.
VIP ACCESS
00:47
Natan por Aí
Рет қаралды 30 МЛН