Stop using 'as' in your Typescript code

preview_player
Показать описание
Most Typescript developers have run into an error that looks something like this:

Argument of type '{ name: string; }' is not assignable to parameter of type 'User'.
Property 'id' is missing in type '{ name: string; }' but required in type 'User'.

That "is not assignable to parameter of type" error usually comes from trying to call a function that takes in a specifically-typed parameter. A quick fix is to force the Typescript compiler to believe your code (writing "as User," for example.) I explain in this video why that can be problematic overtime. Instead, it's best to try and refactor your code and avoiding using Type Assertions at all.

// Chapters //

00:00 Intro - Why are type assertions dangerous?
03:18 Approach 1: Redefine your variable
4:00 Approach 2: Type Predicates
Рекомендации по теме
Комментарии
Автор

There is also an operator "satisfies" that checks if a given type satisfies another type. It can be used instead of "as" in this case

awqsomee
Автор

I think the fact that you can lie to typescript IS actually a necessary feature, you just have to treat that piece of Code with maximum care. I like the fact that typescript guides the development and protects from errors, but having the option to opt out is necessary to create functionality out of the constraints that have been defined. Also, sometimes a type doesn't reflect the reality of the data and It is necessary to be able to express that reality, and some times its not possible to change this in the origin of the specified type.

Athetss
Автор

Very important topic that you covered here! Do you also have a video about the "satisfies" operator? 🤔

TypeScriptTV
Автор

The one place I use 'as' most often is unit tests, since I don't want to be yelled at for not filling out every property on my mock objects. But outside of tests, I try to avoid it.

joe-skeen
Автор

I very much like the second approach but I don't think the type predicate "is" is needed there (correct me if I'm wrong). That's basically how to check if the shape of the object satisfies the function in plain JavaScript.

Anyway, thank you for pointing out the pitfalls of using "as". One good use case of "as" for me is using it with "const", ie. "as const" to stop typescript complaining about an array/object that we would never modify. But technically it's not wrong - we could accidentally modify it somewhere, so again, using "as const" can be a bad thing too.

BeeBeeEight
Автор

How does one skip any when using something like better-sqlite3 that doesn't have <> support?

more-sun
Автор

OK, new subscriber here. Some good points you have.

LA_VIVLIA
Автор

i smashed the freaking heck out of the like button

mokoboko
Автор

I liked the "type predicates" approach, and definitely want to try it out.

classicdrumpercussion
Автор

Javascript devs not realizing they sound like a first grader giving a lecture to kindergartners on the subject of, "did you know L-M-N-O-P is more than one letter?" is far too common on youtube.

Anyway, here's another just like that.

ConernicusRex
Автор

Is it okay to use "as" for primitives, like "as string"? I expect it to be only string. For objects I undestand it may not work properly.

firewatermoonsun
Автор

Thank you so much 💜 from Kyrgyzstan 🇰🇬
A very useful video 💯

elcho
Автор

what about working with html? I can't count how many times I have to cast from Element to HTMLElement or casting an HTMLElement to HTMLImageElement. Is there a way to avoid this?

ninhdang
Автор

Thank you for sharing :) definitely keep it up!

Joelitop
Автор

Tip: Type assertion should only be used when you really know more than typescript code.

HarshShah
Автор

I have a legacy code on my project and 'as' and any are used a lot there... Struggling to fight with it. So this video is helpful 👍

xaxaxa
Автор

Awesome vid, very helpful! Please keep at it

gomork
Автор

when you said like channel little bit lighting happened on like button

abrahimzaman
Автор

I cast types all the time... i just can't help myself. It's like junkfood, I know I shouldn't.

alpaykasal
Автор

I don't really know if I like TS ... the syntax can be pretty dirty and hard to read... but on the other side coding in pure JS would be suicide.. 😁

gass-tube
welcome to shbcf.ru