Stop using "as" in your Typescript code

Publié le: 13 septembre 2023
sur la chaîne: Carmelle Codes
10,257
675

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


Sur cette page du site, vous pouvez voir la vidéo en ligne Stop using "as" in your Typescript code durée heure minute seconde en bonne qualité , qui a été Téléchargé par l'utilisateur Carmelle Codes 13 septembre 2023, Partagez le lien avec vos amis et connaissances, sur youtube cette vidéo a déjà été regardée 10,257 fois et il a aimé 675 téléspectateurs. Bon visionnage!