Learn TypeScript For React in 50 Minutes - React TypeScript Beginner Crash Course

PedroTech
4 Jun 202450:25

Summary

TLDRThis video offers a comprehensive TypeScript in React crash course, building on a previous TypeScript introduction. The instructor addresses the importance of TypeScript for scaling React applications and reducing debugging time. The tutorial covers TypeScript basics, including variable typing, interfaces, and enums, before diving into advanced topics like TypeScript with React Hooks and the Context API. Practical examples, such as creating a user context provider and form event handling, are provided. The script concludes with a demonstration of converting a JavaScript component to TypeScript, highlighting the benefits of TypeScript in understanding and utilizing libraries.

Takeaways

  • ๐Ÿ“š The video is a continuation of a TypeScript crash course, focusing on integrating TypeScript with React to enhance application development.
  • ๐Ÿ” TypeScript is highly recommended for those learning React, especially for industry job prospects, as it is widely used and can prevent scalability issues.
  • ๐Ÿ› ๏ธ The tutorial covers the basics of TypeScript types, such as strings, numbers, booleans, arrays, and the 'any' type, which should be used sparingly.
  • ๐Ÿ’ก Interfaces in TypeScript allow for the creation of custom types, which are particularly useful for defining props in React components.
  • ๐ŸŽ“ The video demonstrates how to define and use props with types in functional components, improving code reliability and maintainability.
  • ๐Ÿ”ง Practical examples are given on using TypeScript with React Hooks, such as useState, to ensure type safety and prevent bugs.
  • ๐Ÿ“ The script explains the importance of handling potential null values when fetching data or using APIs, emphasizing TypeScript's role in error handling.
  • ๐ŸŒ The use of TypeScript with the Context API is explored, including creating context types and providers for more complex state management.
  • ๐Ÿ”„ Enums are introduced as a way to define fixed sets of values, like countries, providing a controlled way to assign specific values in props.
  • ๐Ÿš€ The video concludes with a practical example of converting a JavaScript component to TypeScript, step by step, to illustrate the process of migration.
  • ๐Ÿ”ฎ TypeScript's ability to provide insights into library implementations, such as React's useState, is highlighted as a benefit for deeper understanding of the frameworks used.

Q & A

  • What is the main focus of the video?

    -The main focus of the video is to provide a comprehensive TypeScript in React crash course, building on a previous TypeScript crash course, and covering topics necessary for using TypeScript in a React application.

  • Why is learning TypeScript important for React developers?

    -Learning TypeScript is important for React developers because it is widely used in the industry, helps in building scalable applications, and can save time in debugging. It is almost a requirement in many companies for React development.

  • What is the role of TypeScript in preventing bugs in React applications?

    -TypeScript helps in preventing bugs by enforcing type checking, which ensures that variables and functions are used with the correct data types. This reduces runtime errors and improves code reliability.

  • How does the video introduce the concept of types in TypeScript?

    -The video introduces the concept of types in TypeScript by explaining how to define types for variables, such as strings, numbers, booleans, and arrays, and how these types help in maintaining consistency and preventing type-related errors.

  • What is an 'interface' in TypeScript and how is it used in React components?

    -An 'interface' in TypeScript is a way to define a blueprint for objects, specifying the structure and types of properties an object should have. In React, interfaces are used to define the shape of props for components, ensuring that components receive the expected data types.

  • How does the video demonstrate the use of TypeScript with React Hooks?

    -The video demonstrates the use of TypeScript with React Hooks by showing how to define types for state variables and how to handle form events with proper typing. It also explains how to use TypeScript to ensure that the state and props are used correctly within the hooks.

  • What is the purpose of the 'enum' in TypeScript and how is it used in the video?

    -The 'enum' in TypeScript is used to define a set of named constants, which can be used to restrict a variable to a set of predefined values. In the video, 'enum' is used to define a list of countries, ensuring that the country property in a user object can only be one of the specified country names.

  • How does the video explain the use of TypeScript in the context of the Context API?

    -The video explains the use of TypeScript in the Context API by creating a 'UserContext' that includes typed states and functions for managing users. It demonstrates how to define the context type, create a context provider, and use the context in components with proper type checking.

  • What is the process of converting a JavaScript component to a TypeScript component as shown in the video?

    -The process involves defining types for props, states, and functions within the component. This includes creating interfaces for props, specifying types for state variables, and ensuring that functions handle data types correctly. The video demonstrates this by converting a 'UserProfileEditor' component step by step.

  • Why is it beneficial to understand how libraries like React implement their hooks internally?

    -Understanding how libraries implement their hooks internally helps developers gain a deeper insight into how the libraries work, which can aid in more effective and efficient use of the libraries. It also helps in debugging and optimizing code, as well as in learning best practices from the library's design.

Outlines

00:00

๐Ÿ“š Introduction to TypeScript in React

The video begins with an introduction to a comprehensive TypeScript in React crash course. The presenter acknowledges positive feedback from a previous TypeScript crash course and expresses the desire to continue the topic due to audience requests. The main focus of the video is to teach the necessary TypeScript concepts for using it in a React application. The presenter emphasizes that while TypeScript is not strictly required, it is highly beneficial for understanding and implementing in React, especially for those looking to work in the industry or build scalable applications. The video also mentions a sponsorship by Brilliant.org, a platform for learning STEM-related topics through interactive problem-solving.

05:00

๐Ÿ› ๏ธ Setting Up a TypeScript React Application

The presenter demonstrates how to set up a React application using TypeScript. They use the `vit` command to create a new project, choosing React as the framework and TypeScript as the language. The video covers the basics of defining variables with types in TypeScript, explaining the importance of type definitions for preventing type-related errors. The presenter also introduces the concept of arrays, the `any` type, and the creation of interfaces for custom types, using a `person` component as an example to illustrate these concepts.

10:00

๐Ÿ—๏ธ Building Components with TypeScript in React

The video continues with a deeper dive into building components in React using TypeScript. The presenter explains how to define props using TypeScript, emphasizing the importance of type safety to prevent bugs and ensure correct data handling. They demonstrate how to create a `person` component, define props with specific types, and use interfaces to enforce type correctness. The presenter also shows how TypeScript can help in handling null values and how to use enums to restrict prop values to a specific set of options.

15:03

๐Ÿ” Understanding TypeScript with React Hooks

The presenter introduces the use of TypeScript with React hooks, specifically focusing on the `useState` hook. They explain how to define the type of state and how TypeScript can infer types automatically. The video demonstrates creating a toggle function for a component's state and how to handle form events with TypeScript. The presenter also discusses the use of unions to allow multiple types for a state variable, providing an example of a state that can be either a boolean or null.

20:05

๐Ÿ“ Working with TypeScript and Context API

The video explores the use of TypeScript with the Context API in React. The presenter creates a `userContext` and explains how to define the context type, including a list of users and functions for adding, updating, and deleting users. They demonstrate how to create a context provider component and how to pass the context value with the correct types. The presenter also discusses the importance of defining types for props when accessing children in a React component.

25:08

๐ŸŒ Enhancing Components with TypeScript

The presenter continues to enhance the `user` component by adding a biography feature that updates based on user input. They explain how to handle form events and define the types for event objects in TypeScript. The video also covers how to create a context provider for managing user data across the application, emphasizing the importance of type definitions for functions and states within the context.

30:09

๐Ÿ”„ Converting JavaScript to TypeScript

The video concludes with a practical demonstration of converting a JavaScript component to TypeScript. The presenter identifies the need to define types for props, states, and functions within the component. They create interfaces for the user data and the update function, ensuring type safety throughout the component. The presenter also highlights the benefits of using TypeScript, such as understanding how libraries work internally and improving code maintainability.

Mindmap

Keywords

๐Ÿ’กTypeScript

TypeScript is a superset of JavaScript that adds optional static typing to the language. In the video, TypeScript is emphasized as essential for understanding and using in React applications. It helps in catching errors at compile time and improving code quality. The script mentions that TypeScript is used in almost every company for building React applications, highlighting its industry relevance.

๐Ÿ’กReact

React is a popular JavaScript library for building user interfaces, especially single-page applications. The video focuses on integrating TypeScript with React to enhance development practices. React is mentioned in the context of building applications and components with TypeScript, emphasizing the synergy between the two technologies for better application scalability and maintainability.

๐Ÿ’กType Definitions

Type definitions in TypeScript are used to explicitly specify the types of variables, function parameters, and return values. The script discusses the importance of defining types to prevent type-related errors and ensure that variables hold expected data types. Examples include defining a string type for a name or a boolean type for a marital status.

๐Ÿ’กInterfaces

Interfaces in TypeScript are used to define the shape of an object, which can include properties and methods. The video script uses interfaces to define the structure of props for React components, ensuring that components receive the correct data types. This helps in maintaining consistency and predictability in component behavior.

๐Ÿ’กHooks

Hooks in React are functions that allow you to 'hook into' the React state and lifecycle features from function components. The video discusses using TypeScript with React Hooks, such as `useState`, to manage state with type safety. This is demonstrated by creating a state that toggles the display of information, ensuring that the state type is correctly defined.

๐Ÿ’กContext API

The Context API in React is used for passing data through the component tree without having to pass props down manually at every level. The script explains how to use TypeScript with the Context API to create a `UserContext` that provides typed access to user data and functions for manipulating that data, enhancing the type safety and maintainability of the application.

๐Ÿ’กEnums

Enums in TypeScript are a way of giving more friendly names to sets of numeric values. In the video, enums are used to define a set of country names, ensuring that only valid country names can be used in the application. This provides a type-safe way to handle predefined sets of values, preventing invalid data entry.

๐Ÿ’กPropTypes

PropTypes in React are used for type-checking of props to ensure that components receive the correct data types. Although not explicitly mentioned in the script, the concept is analogous to TypeScript's type definitions. The video implies the use of TypeScript for similar prop type checking, enhancing the robustness of the component's prop handling.

๐Ÿ’กVite

Vite is a build tool that aims to provide a faster and leaner development experience for modern web projects. The video script mentions using Vite to create a new React application with TypeScript, demonstrating the ease of setting up a project with these technologies for efficient development workflows.

๐Ÿ’กBrilliant.org

Brilliant.org is an online platform for learning STEM-related topics through interactive problem-solving. The video script includes a sponsorship mention of Brilliant.org, highlighting its courses in mathematics and computer science. The platform is recommended for viewers interested in enhancing their understanding of topics that complement programming skills.

๐Ÿ’กType Inference

Type inference in TypeScript allows the compiler to automatically determine the type of a variable based on the value it is initialized with. The script discusses how TypeScript infers types for state variables in React components, such as inferring a boolean type for a state that controls the display of information.

Highlights

Introduction to a comprehensive TypeScript in React crash course, building on a previous TypeScript basics video.

Explanation of why learning TypeScript is essential for scaling React applications and its industry demand.

Demonstration of creating a React application using Vite, a tool for generating React apps with TypeScript.

Importance of defining variable types in TypeScript to prevent type-related errors in React applications.

Overview of different TypeScript types such as strings, numbers, booleans, arrays, and the 'any' type with its drawbacks.

Introduction to interfaces in TypeScript for creating custom types for objects, enhancing code reusability.

Explanation of defining props types using interfaces to ensure type safety in React components.

Discussion on the benefits of using TypeScript for handling bugs and ensuring data integrity in applications.

Tutorial on using TypeScript with React Hooks, such as useState, and the importance of type definitions for state.

Illustration of handling form events with TypeScript in React, emphasizing the correct typing for event objects.

Guide on creating a context using TypeScript in React, including defining context types and provider components.

Explanation of using enums in TypeScript to restrict variable values to a specific set, ensuring data consistency.

Conversion of a JavaScript component to TypeScript, demonstrating the process of adding types for props and state.

Insight into how TypeScript can help understand and work with library internals, such as React's useState hook.

Encouragement for viewers to learn from various sources and the importance of combining video learning with interactive problem solving.

Sponsorship acknowledgment and recommendation for Brilliant.org, a platform for learning STEM topics through interactive problem solving.

Final thoughts and call to action for viewers to like, comment, and subscribe for more content on TypeScript and React.

Transcripts

play00:00

hey guys how's it going I'm back here

play00:01

with another video and in today's video

play00:03

I'm bringing to you guys my full

play00:06

comprehensive typescript in react crash

play00:09

course so this is a continuation from my

play00:12

last week's video which was my

play00:14

typescript crash course and I got a lot

play00:16

of positive feedback from you guys and I

play00:18

really wanted to make a video like this

play00:20

because it was requested now you

play00:22

technically don't need to learn

play00:23

typescript or watch the previous video

play00:25

in order to understand this one however

play00:27

it would definitely help you get the

play00:29

topics uh faster however in this video

play00:32

um what we're going to be learning is

play00:33

basically all of the typescript

play00:35

necessary to understand in order to use

play00:38

typescript in a react application so if

play00:40

youve already been used to working in

play00:42

react using JavaScript this video is for

play00:45

you because I'm going to be not only

play00:47

going over stuff like how to build an

play00:50

application in V using typescript um how

play00:53

to work with the different typescript

play00:55

types when dealing with your variables

play00:57

but also how to implement typescript in

play00:59

Your Hooks

play01:00

contexts and much more so in the end

play01:03

we're going to be building a full

play01:04

component that is going to be fully

play01:06

written in typescript before we get into

play01:08

the video if you could leave a like And

play01:09

subscribe I would massively appreciate

play01:10

it it is a thing that is really quick

play01:12

for you guys to do and it will actually

play01:14

help push my videos to more people so I

play01:16

would be eternally grateful if you guys

play01:18

could do that so um with that in mind

play01:20

let's get into the video

play01:32

this portion of the video is sponsored

play01:34

by today's sponsor brilliant.org for

play01:36

those who don't know brilliant is a

play01:38

platform where you're able to learn a

play01:40

variety of STEM related topics through

play01:43

interactive problem solving like courses

play01:45

they have courses related to literally

play01:47

everything you might want to know

play01:49

ranging from uh mathematics to different

play01:51

sciences and even computer science I've

play01:54

personally talked about brilliant in the

play01:56

past to you guys and I've recommended

play01:57

some of their courses like their llm

play01:59

course and more specifically their

play02:01

algorithms and data structures course

play02:04

which can help you if you're actually

play02:06

going to study for an interview but more

play02:08

specifically I enjoy brilliant because I

play02:10

believe that we have very similar styles

play02:12

of teaching all of brilliant's courses

play02:14

are designed in a way in which they

play02:16

bring on real life examples to

play02:18

facilitate your understanding of the

play02:20

topic so if you kind of like my style of

play02:22

teaching I definitely think brilliant

play02:23

will be a good platform for you like

play02:25

I've mentioned I've personally done some

play02:26

of the brilliant courses in the past but

play02:28

I wanted to actually give a shout out in

play02:30

this video for all of their courses in

play02:32

mathematics if you go to brilliant.org

play02:33

and go to their advanced mathematics

play02:35

course category you'll see that they

play02:37

have courses ranging from uh stuff

play02:39

related to pre-calculus to uh linear

play02:42

algebra and Vector calculus and anything

play02:45

that you would learn in a normal

play02:46

mathematics degree and speaking as

play02:48

someone who actually graduated with a

play02:49

math degree um I can tell you that math

play02:52

helps a lot with programming it's not a

play02:54

necessary skill but it definitely levels

play02:56

you up so I wanted to shout that course

play02:58

out because I think you guys would

play02:59

benefit a lot from it plus their handson

play03:02

approach means that you're not just

play03:03

passively watching their videos you're

play03:05

actually actively solving problems that

play03:08

you would in a normal day-to-day

play03:09

scenario if you're interested in

play03:10

checking them out I'll put a link for

play03:13

brilliant.org padot Tech in the

play03:15

description just for my viewers you guys

play03:17

will get a 20% discount when you sign up

play03:19

for their annual premium subscription

play03:21

again it's uh brilliant.org / pedot Tech

play03:25

and now let's get into the tutorial okay

play03:27

everyone so right off the bat you see I

play03:28

have here a small um vs code window

play03:31

popped up and I'm inside a folder called

play03:34

typescript react so I'm going to show

play03:35

you guys how to install a react

play03:38

typescript application but before we do

play03:40

that I just want to give you guys a

play03:42

brief explanation of why you definitely

play03:44

need to learn typescript in order to

play03:45

build react apps so if you're learning

play03:48

react I assume is because you either

play03:50

want to uh get a job in the industry or

play03:54

you want to build an application and in

play03:56

both scenarios I would 100% say that

play03:59

it's necessary to get to understand

play04:01

typescript because if you're learning

play04:03

react or using react in order to get a

play04:05

job in the industry I doubt that you're

play04:07

going to be hired if you don't know

play04:08

typescript because typescript is used in

play04:10

almost every single company and if there

play04:12

is a company that is still building

play04:14

react applications using pure JavaScript

play04:16

I would gladly say that I don't think

play04:19

it's a good company to work on because

play04:21

um the application will probably not

play04:24

scale well and it will actually cause a

play04:26

lot of problems that will end up in the

play04:28

hands of you the developer so I think in

play04:30

that scenario for sure you need to learn

play04:32

this but also if you're building an

play04:34

application on your own then you can

play04:36

make this decision to learn typescript

play04:38

as a technology as a language and spend

play04:41

I don't know one or two days actually

play04:43

just learning it and that will save you

play04:45

months of debugging trust me in the long

play04:48

run this will save you so much time of

play04:49

debugging and it will actually make your

play04:51

application scale way more than it would

play04:53

if you were using just normal JavaScript

play04:56

so now that I explained why you

play04:58

definitely want to learn this

play05:00

let's get to actually building the

play05:01

application so uh like I said I'm going

play05:03

to be using uh vit to build this so if

play05:06

you've never used V in the past it's

play05:08

basically this it's just a way for you

play05:10

to generate your react apps just like

play05:13

nextjs just like uh create react tap

play05:16

however vit is a very handy way for you

play05:18

to do it so in order to create an app

play05:20

with vit I'm going to run npm create

play05:24

v um and then I'll say at latest just so

play05:28

we get the latest vers version and then

play05:30

I'm going to put a dot over here so I'm

play05:32

going to press enter and uh it's going

play05:35

to ask us what kind of uh framework we

play05:38

want to create a vit app with so you can

play05:40

actually use it for a lot of different

play05:41

options I'm going to create it with

play05:43

react and then you can see V actually

play05:45

allows you to just choose the language

play05:48

straight up in the terminal so I'm going

play05:49

to choose typescript and as you can see

play05:51

it will have generated the app but to

play05:53

actually run it I have to run the

play05:55

command npm install to install all the

play05:58

packages in the package. J B on and uh

play06:01

when that's done and we want to run the

play06:03

app I can run npm runev but for now

play06:06

we'll actually not run it I just want to

play06:08

open up one of the components over here

play06:10

um and start explaining to you guys

play06:12

using this as a template to explain to

play06:14

you guys how type script will actually

play06:16

work so let's keep this simple right uh

play06:20

if you've never worked with uh with

play06:23

typescript before it is important for

play06:25

you to understand the different types

play06:27

that exist inside of typescript and I

play06:29

will just briefly mentioned them so you

play06:31

can create variables right so for

play06:33

example you can create the variable name

play06:35

and set it equal to a string called

play06:36

Pedro but in typescript you have to

play06:39

define the type that this variable will

play06:41

have so since this is a string I have to

play06:44

come over here and say that it will have

play06:46

a value of string because if you don't

play06:48

do that then um typescript won't

play06:50

actually allow you to create this

play06:52

variable if you are in what is known as

play06:54

strict mode which by the way I

play06:56

definitely recommend watching my

play06:57

previous video to understand this is

play06:59

just a way for you to um that is

play07:01

determined for you to actually keep

play07:03

checking to see if you're using

play07:05

typescript correctly but defining types

play07:07

like this prevent you from later on

play07:10

coming over here and saying name is

play07:12

equal to 45 because then it will give

play07:15

you an error saying that you can assign

play07:17

a a number to a string so this is basic

play07:21

the basic concept of what typescript

play07:22

does right and in react this is great

play07:25

because any variable you want to create

play07:27

right name age you can Define over here

play07:30

the actual value or the type and set

play07:34

something I can create like a is married

play07:37

bullion and just say that this is a

play07:40

bullan and set it equal to true or false

play07:42

so you kind of get the idea now I'll

play07:45

just uh list three more types that I

play07:47

think are important for you to

play07:48

understand uh before we get into

play07:49

actually doing what is mostly uh just

play07:52

pure react typescript stuff this is more

play07:54

General typescript but this is just what

play07:56

I think is important for you to get

play07:58

first so to make a list for example a

play08:00

list of uh I don't know numbers like a

play08:03

list of Ages uh uh you actually just set

play08:06

the type like this you say it's a number

play08:10

array just like this and then you set it

play08:12

equal to uh a list of numbers right so

play08:16

if we ever work with lists which you

play08:18

probably will a lot um you have to

play08:21

Define it like this uh what else so

play08:23

there's the any type so if you have a a

play08:27

thing that you actually don't know what

play08:28

the type is you can use any and then you

play08:31

can see that I can set this equal to

play08:33

anything like I can say person is equal

play08:35

to a string and it won't won't actually

play08:38

give us an error but the thing is I

play08:39

don't recommend using any because then

play08:41

it def defies the whole purpose of using

play08:44

typescript right and the last thing that

play08:47

I want to show to you guys is is the

play08:50

idea of creating um interfaces so

play08:53

creating your own type if I were to

play08:56

create a component over here now we're

play08:58

mostly going into to what uh typescript

play09:01

you actually use in react so if I were

play09:02

to create a component over here for

play09:04

example I create a component called

play09:05

person right first of all uh the

play09:09

extension is not going to be JS and it's

play09:11

not going to be jsx it's going to be TS

play09:14

or TSX now if you're going to be

play09:16

creating a component inside of this file

play09:18

you definitely have to make this into a

play09:20

TSX or else it won't work if you make it

play09:23

like this uh it usually used for files

play09:26

like creating your types creating

play09:28

functions and stuff like that but if

play09:30

you're going to create a component

play09:31

definitely make it

play09:33

TSX then over here let's create a

play09:36

component right I'm going to export

play09:39

const

play09:40

person and let's make it a simple

play09:43

component uh I'll just return the text

play09:47

person now over here in the app. CSX

play09:51

let's import this

play09:53

component so I'll say

play09:56

slashers and let's import person over

play09:58

here

play10:00

now what I want to introduce to you guys

play10:02

is the idea of how do we Define props

play10:08

using typescript because if you don't

play10:10

recall when if I wanted to for example

play10:13

take in some props for an individual

play10:15

person over here right if I wanted to

play10:17

say that the props in this is uh and

play10:21

I'll say props over here and I'll say

play10:22

that it will take in the following

play10:24

things a person can take in a name right

play10:27

let's say Pedro it can take in an age

play10:31

let's say 22 and uh it can take in is

play10:37

married right which I'm not married so

play10:40

I'll say false right so it takes in this

play10:43

props and we have here props at the top

play10:45

and actually I just want to display that

play10:47

information so I'll create here A P tag

play10:50

and I'll say um first descrip the name

play10:53

so name and then say props

play10:56

name and then I'll do the same for

play11:00

age um age and then I'll say props that

play11:03

Ag and then over here I'll just

play11:06

say this

play11:09

person um and then I'll just change this

play11:13

depending on the Boolean so I'll say

play11:14

props do is married and I'll say if it

play11:17

is married then this person is married

play11:21

if the person is not married then I'll

play11:22

say is single right so what if I want to

play11:26

create a component like this and reuse

play11:28

it how many ever times I want to I'll

play11:30

even create another one just as an

play11:33

example

play11:35

um let's say I don't know

play11:39

Colin

play11:40

24 and also not married or actually

play11:43

let's make him married so true if I

play11:45

wanted to create this you can see

play11:47

there's a red line over here because it

play11:48

says that parameter props implicitly has

play11:50

an any type so this is actually one of

play11:52

the best use cases for typescript in

play11:54

react because the following reason when

play11:57

you create a component if I were to come

play11:59

over here and I were to actually pass in

play12:02

a string for age instead of a number it

play12:06

would actually work the same so let's

play12:10

test this out let's run this thing I'll

play12:12

say npm run dep and let's grab this and

play12:17

then just paste it over here okay so

play12:19

this is the app right now right it shows

play12:21

the two people but if I were to change

play12:25

this to a string you see that it still

play12:28

show shows the same thing it shows ages

play12:32

22 right which means it must be working

play12:35

right if I Chang this to uh I don't know

play12:39

a list of strings then it will probably

play12:42

give us an error visually speaking but

play12:45

it doesn't even give us an error

play12:46

visually speaking which is crazy right

play12:48

it's just showing what what's there so

play12:50

that's the thing uh with uh typescript

play12:54

um or with react in general props can

play12:57

whenever they have an implicit it any

play12:59

type which means that since we didn't

play13:01

Define this type the type for props it's

play13:03

assuming that the type is any right um

play13:09

it can actually show us no visible sign

play13:11

of of actual misconduct in our code like

play13:15

this so it seems like everything is

play13:16

working perfectly but in itself if we

play13:19

ever use this variable for anything more

play13:22

than just displaying this it can

play13:23

actually cause some bugs because it's

play13:25

not the type that we wanted it to be so

play13:28

this is this is kind of why we

play13:30

definitely want to Define the types now

play13:32

you've seen this before if you've ever

play13:34

worked with prop types which is

play13:35

something that comes directly with react

play13:37

however uh typescript makes it also very

play13:40

easy to do the exact same thing which is

play13:42

Define exactly the type of props you

play13:44

want and the way we do that is by using

play13:47

what is known as an interface so like I

play13:49

said uh there are some primitive types

play13:51

in typescript like string number and

play13:53

Boolean but you can actually create um

play13:56

custom types that define object object s

play13:59

they are known as interfaces so I'll

play14:02

create a interface called props and I'll

play14:06

just assign the props of this component

play14:08

to be equal to that so we want this

play14:11

props object over here to have three

play14:13

Fields name age and is mered so we'll

play14:16

come over here and we'll Define the

play14:18

three Fields like this so I'll say that

play14:20

this has a name which is a string it has

play14:24

an age which is a number and it has has

play14:28

an is married which is AUM so now

play14:33

typescript doesn't give us a red

play14:34

squiggly line anymore because it knows

play14:37

that um we have defined the correct type

play14:40

and more importantly you see this is a

play14:43

Boolean right so if we were to use this

play14:45

bul and wrong over here we're definitely

play14:46

using it right we're asking okay if the

play14:48

person is married if true then do this

play14:51

if false do this right but if we try to

play14:54

for some reason display the Boolean as a

play14:58

as a string right right so I said props

play15:02

isar we'll see that nothing really

play15:05

happens but it also doesn't show it in

play15:07

our screen but if we were to compare

play15:09

this to something like uh I don't know a

play15:13

name like Pedro you'll see that

play15:15

immediately it gives us an error because

play15:17

the comparison seems to uh try to

play15:20

compare to things that are not of the

play15:22

same type which means that typescript is

play15:24

definitely checking almost all

play15:25

interactions that you have with your app

play15:28

um and by Def your prop like this it

play15:30

makes it easier for you to not cause

play15:32

stuff like what I did before because now

play15:34

if I were to change this to a string it

play15:37

immediately notifies us and I think it

play15:39

also um might show over here um it

play15:43

actually depends a lot on how you define

play15:45

your TS config apparently doesn't seem

play15:48

to be doing that um but if we were using

play15:51

uh some sort of uh linking Library like

play15:53

is lint um it could actually manually

play15:56

trigger a break in our app so we can

play15:58

actually have a more clear visible error

play16:01

um if there's a typescript error now in

play16:03

this case we just get the red squea line

play16:05

which let us know that this isn't the

play16:07

correct type but as you might have

play16:09

noticed this is really useful because

play16:11

also you can just make multiple types in

play16:13

your app you can actually uh if if this

play16:16

should define a person right we we

play16:18

called it person over here so instead of

play16:20

putting props we could actually just

play16:21

call this person and then use this in

play16:24

many different places in our app maybe

play16:26

when we're trying to deal with data

play16:28

Maybe when we're making a query that

play16:30

grabs back per people right so we could

play16:33

do something like that also if we wanted

play16:36

to make this into a list right uh the

play16:38

props is actually a list of people I

play16:41

could actually just put the array over

play16:43

here and um Loop through this props uh

play16:47

that is now is an array it's not an

play16:48

object and display the information just

play16:50

like I said it would so um you can see

play16:53

how useful defining props in your types

play16:55

actually is now one reason why this

play16:57

might also be very useful is um dealing

play17:00

with bugs with bugs where you're you're

play17:03

actually rendering a component based on

play17:05

some sort of I don't know function right

play17:08

that makes a request let me create an

play17:10

example here like fetch

play17:12

user and as a an example over here I'm

play17:16

not actually going to be fetching

play17:17

anything imagine that this returns back

play17:21

uh an object for a user right it returns

play17:24

back uh name is equal to

play17:27

Pedro um

play17:30

age is equal

play17:31

to um 22 but then is

play17:35

married is equal to null right so

play17:39

imagine that this is what it's returning

play17:41

and then over here I grab back the user

play17:45

user fetched equals to fetch user let's

play17:49

pretend we call this function and I pass

play17:52

uh the information User fetched name

play17:56

user fetch. Ag and and then user fetched

play18:00

dot is married this will give us an

play18:02

error because it is saying that it can

play18:05

be null so this kind of forces you to

play18:08

even when you're fetching data um from

play18:10

an API or something to actually make

play18:12

sure that you're handling cases in which

play18:15

the data is null so that um inside of

play18:19

this function I would probably make a

play18:20

call like oh if any of the fields are

play18:21

null then display an error so it will

play18:24

force you to actually error handle

play18:26

everything which is great for preventing

play18:29

bugs okay let me just contrl Z all of

play18:32

this um and let's move on from the

play18:35

interface part of this tutorial now the

play18:38

next thing I think is important for you

play18:39

guys to understand is using uh

play18:42

typescript when you're working with

play18:43

certain hooks so let's as an example

play18:47

over here inside of the person's uh uh

play18:50

component create a hook right let's

play18:53

create a a state um called um use State

play18:58

and this state will be a button which um

play19:02

will toggle some sort of uh will toggle

play19:05

if it shows the information or not right

play19:08

so I'll say show info or

play19:13

is show info and the other one is set

play19:17

show

play19:18

info so I'm going to wrap all of this

play19:21

around with the is show info and we only

play19:25

want to display this information if that

play19:28

Boolean is

play19:29

true and um I do have to put this to

play19:34

wrap all of this around and I probably

play19:36

need to put a small bracket over

play19:39

here just like this not bracket uh a

play19:42

small fragment and then this how we

play19:46

normally would do with JavaScript is we

play19:48

would actually put the initial value of

play19:50

the State uh to actually infert the type

play19:53

so if this is a Boolean we would first

play19:55

put false and then basically react would

play19:58

know that this is is AIA right um but

play20:01

the problem is that um things can happen

play20:05

right I could come over here and say set

play20:07

show info is equal to Pedro now because

play20:12

typescript already is inferring that

play20:15

this is a Boolean it actually will

play20:16

already in enforce this um for us so we

play20:20

actually don't have to Define any Types

play20:22

on the use State however it's still

play20:25

highly recommended that you put the

play20:27

actual type um inside of the state so

play20:31

that you actually are sure that you're

play20:33

using it correctly so the way you set

play20:36

the type for this or determine the type

play20:38

for this is by adding uh a little thing

play20:42

over here and writing the type so I'll

play20:44

say B just like this and the great thing

play20:46

about this is sometimes uh the type of a

play20:49

state can be something like a Boolean or

play20:53

a string or something but it could also

play20:55

be F null right it can also be empty

play20:57

data so when you're working with a

play20:59

situation like that imagine that I want

play21:02

to accept it to be false or true but I

play21:03

also want to accept it to be no um you

play21:06

can actually use what is known as a

play21:07

union so a union allows you to um say

play21:10

that this type over here will allow

play21:13

either one thing or another so by doing

play21:16

by by adding the union like this you can

play21:19

say that this accepts both Boolean or

play21:21

null so you can see null doesn't have a

play21:23

right Squig line anymore but also true

play21:25

will not have it false will not have it

play21:29

but Pedro will because it's not part of

play21:32

the type unless I say that a string is

play21:34

also allowed over here so we'll keep it

play21:37

actually just as a bon that was just a

play21:39

tangent that I wanted to show to you

play21:40

guys and let's create a function called

play21:44

toggle uh

play21:47

info um and this function is pretty

play21:51

simple whenever we click on it we just

play21:54

set the show info to be equal to the

play21:57

previous value and the opposite of what

play21:59

it was before so uh if it was true it

play22:02

would become false if it was false it

play22:03

will become true and then I'll put a

play22:04

button over here that will say

play22:09

toggle info and we'll set the onclick to

play22:13

that now this should work as you can see

play22:17

over here uh if I click on toggle info

play22:20

it should show that if I click on this

play22:21

one it should show the other one if I

play22:22

click again it should hide and it works

play22:25

perfectly right but the important thing

play22:26

is that we are sure that our state has

play22:29

the correct type now what if we have a

play22:32

state that is a little bit more

play22:33

complicated instead of just a a button

play22:36

imagine we have an input right we have

play22:38

an input over here which will be a will

play22:42

accept a text and I actually want to

play22:44

have a state that allows me to type

play22:47

something in my screen like a mini

play22:50

description of this person right so what

play22:53

if that's a situation I want to have so

play22:55

the way I'm going to do it is I'm going

play22:57

to have a little uh P tag over here

play22:59

which is going to say something like uh

play23:03

let me grab the name so Props do

play23:05

name uh bio something like that and then

play23:08

over here I want to display some sort of

play23:10

biography of this person but this

play23:12

biography will only be determined as I

play23:14

type on this input how would I do that

play23:17

well first of all let's delete this and

play23:18

let's create a state that is going to

play23:20

keep track of what I'm typing on the

play23:22

input so I'm going to say const is equal

play23:24

to use State again this will be a string

play23:27

so I have to Define thep over here and

play23:30

initially it will be an empty string so

play23:33

actually in this situation it's a great

play23:35

moment for us to maybe say that this

play23:37

could also be null instead of empty

play23:39

string because that's just an easier

play23:41

identifier right over here we can say if

play23:45

uh let's actually create the name of

play23:47

this state let's say per bio bio yeah

play23:52

I'll say person bio and then set person

play23:57

bio something like this so we can say if

play24:00

person bio is equal to

play24:04

null then we just want to say something

play24:06

like uh no

play24:11

bioavailable but if it is not null then

play24:14

I want to set it equal to person bio uh

play24:18

if you're checking for uh something to

play24:20

be null you can actually not even do

play24:22

this you can just say okay if not person

play24:26

bio then uh show this because this is

play24:29

the same thing as asking if person bu is

play24:31

equal to no so now let's implement the

play24:34

functionality oh and also let me delete

play24:36

this because we're not doing that

play24:37

anymore uh let me write the

play24:39

functionality for typing on this input

play24:42

and being able to store that value

play24:44

inside of this because this is what

play24:45

where I'm want to get I want to show you

play24:47

guys how to deal with typescript in

play24:49

situations where you're working with

play24:50

form events because that's a little bit

play24:53

weird when you're starting out so inside

play24:56

of this input we're going to have an on

play24:58

change right and it's going to call a

play25:00

function called handle change and let's

play25:03

create that function const handle change

play25:08

is equal

play25:09

to uh a normal function that just sets

play25:14

the person bio equal to whatever the

play25:17

value of this input is so in this

play25:21

function we're going to have an event

play25:23

right but the problem is I can't just

play25:26

say event. Target

play25:29

value because this has no value you see

play25:32

it hasn't any type now what is the type

play25:34

that we put for this event well this is

play25:36

a predetermined type that already comes

play25:39

with react if we if we want to so um the

play25:43

specific type for when you want to grab

play25:45

a change event from an input is defined

play25:49

like this it is called react do change

play25:54

event so whenever you have an input um

play25:57

that you're you're using the unchange

play26:00

and you want to grab the type from that

play26:01

it is the change event from that input

play26:04

and then you specify inide of this thing

play26:07

which specific type of input or form

play26:09

element um you are describing in this

play26:12

case it is an HTML input element now

play26:17

this seems weird but this is exactly the

play26:20

type specific for the event inside of

play26:22

the unchange function um and if you ever

play26:26

forget this don't worry if you do a

play26:29

small Google search you'll find the

play26:30

answer for this because I remember when

play26:32

I was learning this I was like I'm never

play26:33

going to memorize this um until I did

play26:36

but after a while right because it took

play26:38

me a while to memorize this but I could

play26:40

just search on Google whenever I wanted

play26:42

so you see in this case this would work

play26:45

and we could see that this would

play26:47

actually generate the app that we want

play26:48

like it says right now no bio available

play26:50

but I can say Pedro is a very nice

play26:55

person and it works perfectly so this is

play26:59

uh kind of the idea for this

play27:01

specifically um there are other event

play27:04

types for example if you I know this

play27:07

isn't a form but if you had a form and

play27:09

you had a handle submit function that I

play27:13

mean it's not applicable to this example

play27:14

specifically but um you guys definitely

play27:17

ran into a situation where you had a a

play27:19

function that submits a form um and you

play27:21

wanted to grab the event uh inside of

play27:23

this function the type for that would be

play27:25

react do form event

play27:28

because it's a form and then it would be

play27:30

an HTML form element so just memorize

play27:35

this if you're interested but if you

play27:36

ever have an un unsubmit uh you can do

play27:39

that and then you can use this event to

play27:40

do something like prevent default or

play27:43

whatever you would want to do in this

play27:45

scenario but this is just some basic

play27:47

form events uh that I think are

play27:49

important for you to understand and

play27:51

there are specific types okay everyone

play27:53

so now that we dealt with some form

play27:54

events um I think it's time for us to

play27:57

get into a little a little bit more

play27:58

complicated stuff so what I want to

play28:01

teach you guys is how to correctly use

play28:03

typescript when working with the context

play28:05

API and this is a little bit big because

play28:08

um it allows you to understand a lot of

play28:10

type uh interface creation and defining

play28:14

types with different um hooks in react

play28:16

like the the use context hook right so

play28:19

what I want to do is to continue this

play28:21

example that we've been working on so

play28:23

far I want to create a file called the

play28:27

user context

play28:29

so I'll create a file called the user

play28:33

context do um TSX so let's create a file

play28:38

over here called the user context

play28:43

provider and this will actually be a TSX

play28:46

file because we're going to return back

play28:48

a custom uh provider component for this

play28:52

uh context that we're going to create so

play28:54

first things first um I know this is

play28:57

called person but let's change the name

play28:59

of this to user because I feel like it's

play29:00

more uh it's an easier thing to

play29:03

understand uh when we're working with uh

play29:06

websites instead of person and I'll

play29:08

change all of this and I'll keep the

play29:11

name of the file as person whatever but

play29:13

one thing I want to do is you see that

play29:16

this user type over here was created

play29:18

inside of this

play29:20

component if we're using the context

play29:23

API uh and and we want to work with it

play29:26

we probably want to keep them separate

play29:28

we want to create actually a type for

play29:31

the props over here not call the user

play29:34

because not

play29:36

necessarily they will be the same thing

play29:38

in this case they kind of will but um in

play29:42

many other scenarios they might not be

play29:44

and inside of the user context provider

play29:46

we'll create the user type or just user

play29:50

right and we'll export it for here from

play29:52

here so that it can be used anywhere

play29:54

else in our app it's better keeping it

play29:56

here than keeping it inside of

play29:58

uh an individual component and then we

play30:02

have to create what is known as the

play30:04

context type because uh we're going to

play30:06

create a context that is going to

play30:09

include not only um a list of users but

play30:13

also a function that is going to um be

play30:17

able to add a user to a list of users um

play30:20

also a function that is going to be able

play30:21

to delete uh user from the list want to

play30:24

update and one to just see the list of

play30:27

uh users so it's a very simple crud um

play30:30

app inside of this context so let's do

play30:34

that let's create the interface that is

play30:36

going to actually represent what the

play30:38

context will return so we'll call it

play30:41

user

play30:42

context type and inside of here what do

play30:46

we want we want to have a list of users

play30:48

so I'm going to say users and it's going

play30:50

to be uh user list but it can also be

play30:54

null because imagine in a normal app we

play30:56

would probably be fun fetching this from

play30:59

an API and the list can obviously be

play31:02

null so we have to account for that then

play31:06

um we want to create a list uh or

play31:08

actually have here a function for adding

play31:11

user which will probably include um a

play31:14

user inside of here so uh we can Define

play31:18

the actual type of user like this by

play31:21

seeing that the function will have an

play31:23

argument called user and its type will

play31:25

be user and we need to actually add a

play31:28

return type I haven't explained this yet

play31:30

but when you define functions instead of

play31:33

interfaces you have to Define it like

play31:35

this um the add user function will take

play31:39

in a user and it will return void so we

play31:42

can just do it like this then we want to

play31:44

have the same thing but for uh updating

play31:48

deleting and reading so uh reading

play31:50

actually is okay we can just use the

play31:53

list of users over here to read so for

play31:56

updating um we could for example take in

play31:59

over here uh ID of the user which would

play32:02

be a string obviously this is all

play32:04

examples but would be a string and then

play32:06

we can also take in um I don't know

play32:09

the the is

play32:12

married for the user this is the thing

play32:15

that we're trying to update and we can

play32:16

take in the new value for it and um

play32:21

actually we don't even need to take that

play32:22

in because uh we can just change it to

play32:24

whatever it is like the opposite of what

play32:27

it is right now and then for delete

play32:29

user we can just take in the ID of the

play32:32

user and delete it from the list now

play32:35

this is the actual what what we're going

play32:38

to have inside of the context this is

play32:40

what we can access if you here I'm

play32:42

assuming you're used to the context API

play32:45

so you kind of understand but the thing

play32:47

is we're going to have a context that is

play32:48

going to pass in all of those values so

play32:51

in order to determine that that's

play32:52

actually how the context is going to be

play32:54

I'm going to come over here and create

play32:56

the context I'm going to say use

play32:58

user

play33:00

context is equal to

play33:02

create

play33:05

context and then inside of this we can

play33:10

actually put in uh the user context type

play33:15

to tell react that this is has to be

play33:18

what we pass in to this context we also

play33:21

have to obviously import the create

play33:22

context as you can see from react and uh

play33:26

we have to P pass a type over here

play33:28

so what I like to do is I like to put

play33:31

pass in the initial like type for this

play33:34

so I'll create a state called um context

play33:39

initial values and it will just be an

play33:42

object containing uh users and its

play33:45

initial value will be uh just an empty

play33:48

array then or null

play33:51

actually then for add user we can just

play33:54

say that the initial value is just a

play33:56

function that returns null as well uh

play33:59

update

play34:01

user will be a function that returns

play34:05

null delete

play34:07

user is just a function that returns n

play34:10

as well so this is just so we can have

play34:12

an initial value for this context over

play34:15

here and it stops giving us an error

play34:17

right because remember this context

play34:20

requires an object of this type so here

play34:23

not necessarily this is how the data

play34:25

that we're going to be returning uh

play34:27

because in inside of this uh file we're

play34:29

going to deal with fetching data

play34:31

updating and doing all those stuff but

play34:33

for now let's just pass in an initial

play34:34

value like this then since we now have

play34:37

this user context let's create the

play34:39

provider so the way we do this is we

play34:42

have to create a component called the

play34:45

context user provider so I'm going to

play34:48

say const user provider and this

play34:52

function will take in some props

play34:56

obviously and um the

play34:59

prop that we actually really want is

play35:02

just the children of whatever we wrap

play35:04

this component around so down here we'll

play35:08

just return back uh for now nothing but

play35:11

then we'll return back the

play35:13

props children just like this now you

play35:17

might realize that it gives us an error

play35:19

because it says that it has an any type

play35:22

now if I were to click this it would

play35:24

Define the type for props like this but

play35:26

the thing is when you ever have to

play35:28

access the children from a prop in react

play35:31

and you're using typescript so the way I

play35:33

would do this is I would actually create

play35:35

over here the interface for props again

play35:40

but just for this one and I would just

play35:43

Define the children type as a

play35:47

react. react node this is how I normally

play35:52

do it uh for detering determining a

play35:54

children when accessing it using

play35:56

typescript you'll probably run into this

play35:58

in the future so it's good to memorize

play35:59

this specifically so we'll create this

play36:02

user provider and the great thing about

play36:04

this if you've never actually done this

play36:06

kind of formatting for creating uh

play36:09

providers and contexts is that inside of

play36:11

here I can create the functions that are

play36:12

going to be returned back inside of this

play36:16

uh context and create actual States

play36:18

because now it's all inside of this

play36:21

component so we can actually create

play36:22

States so I'll create a state called

play36:24

users and set users now what type this

play36:29

is is um a list of users so I'll say

play36:32

user list and it can also be null for

play36:36

recall this is specifically the way we

play36:38

defined the users's type which means

play36:41

that uh initially it will be null but

play36:43

let's imagine that uh we'll have a user

play36:45

fact over here and in this user fact in

play36:49

this example I would

play36:52

uh fetch some data over here uh and

play36:57

return back and set the users to be a

play37:00

list of the following

play37:02

thing uh let's create a fake user over

play37:05

here so we'll say name is equal to

play37:08

Pedro age is equal to 22 and is married

play37:13

is equal to false so obviously I'm just

play37:17

doing this as an example but inside of

play37:18

here you would fetch data and set the

play37:20

users equal to that data but since I

play37:22

don't have any API to fetch this I'm

play37:24

just setting the users to the to this

play37:26

fake data that I'm putting inside of

play37:27

here here but at least we can now use

play37:29

this users list right now what I want is

play37:33

to wrap all of this around with the user

play37:35

context provider so I'm going to say

play37:37

user

play37:39

context

play37:41

um.

play37:42

provider then over here I'll do the

play37:45

exact same thing user context. provider

play37:48

and I'll pass in the

play37:50

value for this context as uh we only

play37:55

have right now users so I'll say users

play37:58

but right off the bat you can see that

play37:59

there's an error because it is following

play38:02

it's it's it's missing the following um

play38:04

properties add user update user and

play38:06

delete user so you might see already

play38:09

that it is actually uh checking to see

play38:11

if we're actually passing the correct

play38:13

information and it I'm happy that we

play38:16

were able to actually Define the types

play38:18

for this context up here at the top so

play38:20

it's telling us that we definitely need

play38:22

these three functions so let's just

play38:23

create them not going to actually

play38:25

Implement them but let's just do it

play38:28

const um add

play38:31

user um and it requires uh

play38:34

user and let's actually just return null

play38:38

for this one and then pass in over

play38:41

here uh then let's do the same for

play38:44

update and delete both of them I think

play38:46

they just require an ID which is a

play38:49

string so let's just do that and they'll

play38:52

both return n as

play38:55

well and delete user and now let's pass

play38:59

them and you see that as we pass them

play39:02

they will uh the the right cular lines

play39:04

will actually disappear the only one

play39:06

here is uh this one because apparently

play39:09

has no closing tag so let's check what

play39:12

this is uh oh yeah we should have put

play39:14

this over here and then close this and

play39:18

you'll see that now our context provider

play39:20

works perfectly which means that if I

play39:22

wrap all of this around with the user

play39:25

context or user provider actually and I

play39:28

import that

play39:30

component means that now inside of this

play39:33

person component over here I could

play39:36

technically uh access that by using the

play39:39

uh use context hook if I pass in over

play39:43

here the user context that I have to

play39:47

export actually

play39:50

user context right if I uh import that

play39:54

user context

play39:58

uh why is it not importing

play40:01

it uh sorry about that let me just uh

play40:04

try to import

play40:08

this over

play40:10

here uh import

play40:13

from slash user context provider and let

play40:17

me import the user context if I do that

play40:22

and I put this oh actually I have to

play40:25

pass this over here not there sorry

play40:27

about that but if I do this um you'll

play40:30

see that it autocompletes for us see it

play40:33

says it knows exactly the types that

play40:34

this context can return so it can return

play40:36

a users it can return an update user it

play40:39

can return a delete user and so on so I

play40:41

hope you kind of got the idea I know it

play40:43

it feels a little bit convoluted but

play40:46

whenever you work with the context API

play40:47

using typescript this is kind of how I

play40:49

would go about it I would Define a type

play40:52

for the data that you're going to be

play40:54

manipulating or using in this context uh

play40:57

a type for actual the return values of

play41:00

this context then pass all of those

play41:02

types to their individual respective

play41:04

places and then actually make sure you

play41:07

pass them as the value of that provider

play41:09

so this is kind of how a context would

play41:12

look like in typescript now moving on

play41:15

from this uh I want to actually add a go

play41:18

back to this thing that I was working on

play41:20

right and show you guys a concept called

play41:22

the enum which is actually a great way

play41:24

for you to define a specific type of a

play41:28

property inside of your application

play41:30

especially for situations like this

play41:32

imagine that we want to add something to

play41:34

this props we want to add the country in

play41:36

which the user was born in but the thing

play41:39

is there's only so many countries

play41:41

there's actually a specific list of

play41:42

countries in the world and it's a

play41:44

defined amount so you don't actually

play41:46

want to leave this up like like up to

play41:49

interpretation you don't want to make

play41:50

country be a string because there's

play41:53

actually a specific uh amount of options

play41:57

that can go inside of this type so what

play41:59

you can do is create an enum for this so

play42:02

I can say enum

play42:05

countries and then over here I can

play42:08

actually Define a specific value for

play42:09

each of the countries for example I can

play42:11

make Brazil as one of the options and

play42:13

set the value of it to

play42:15

Brazil then I can make friends an option

play42:18

set it equal to friends I can make India

play42:22

an option and set it equal to India and

play42:25

so on right I'll just put uh United

play42:28

States as an option and set it equal to

play42:31

United

play42:32

States and then over here instead of

play42:34

having uh country as a string let's keep

play42:37

it as a as a country right and then down

play42:40

here I'll just say uh p uh country of

play42:46

origin is equal to props do

play42:52

country uh oh I I have to wrap this

play42:54

around

play42:55

sorry props do country now you'll see

play42:59

that first of all it's giving us some

play43:00

red squiggler lines because we haven't

play43:02

defined the country over here now what

play43:04

do I put for this do I just put a string

play43:07

do I just put Brazil well it will say

play43:10

that we can't do that because Brazil is

play43:13

not assignable to the type countries

play43:15

actually with enums you can do a really

play43:17

cool thing which is if I export this

play43:19

enum

play43:21

type right I could come over here and

play43:23

import this like this uh

play43:27

countries and then just choose what

play43:30

country I

play43:32

want like this because if you actually

play43:36

uh had this as an open-ended option I'll

play43:39

show you what could happen so let's do

play43:42

country and let's do countries dot I

play43:45

don't know

play43:48

India and keep it like this right now it

play43:51

allows us for to us to have actual

play43:54

control over exactly what is displayed

play43:56

over here because because you see all of

play43:58

them are in capital letters right but if

play44:01

this was a

play44:02

string and uh I wanted to actually just

play44:06

write Brazil over here you'll see that

play44:08

it would allow me to whilst the one I

play44:11

used the enum um is Captain in capital

play44:15

letter so it allows you to keep some

play44:16

standard and actually um keep users from

play44:20

coming here and writing something like I

play44:22

don't know wakanda right which is not a

play44:24

country in the real world but it's clear

play44:27

really allowed here makes us have some

play44:29

control over that so using enums is

play44:31

really cool and really important in my

play44:33

opinion okay guys as uh final part of

play44:36

this tutorial uh like I said I want to

play44:38

show you guys how to convert a component

play44:40

that was previously written in

play44:42

JavaScript and make it into a typescript

play44:45

component so as you can see I made this

play44:47

component over here called user profile

play44:49

editor it's a pretty intermediate

play44:51

component with like a bunch of different

play44:53

stuff and I definitely want to make some

play44:55

changes that you've learned in this

play44:56

video video that will allow you to uh

play45:00

stop having some red squ lines because

play45:02

uh no types were defined right so right

play45:06

off the bat um I want to take a look at

play45:08

the props right let's start with the

play45:10

props um I see that there's two props

play45:13

user and on update so let's create the

play45:16

interface for

play45:17

props and let's put those two types user

play45:22

and I don't know the type for that yet

play45:23

but then uh I'll put that on update um

play45:27

now let's see what they what both of

play45:30

these types look like so let's go to

play45:32

user where is user used so I'm going to

play45:35

contrl f and let's see where user is

play45:37

used so it's we see already it's used

play45:40

inside of the on update um but it

play45:43

doesn't seem to be used Oh see it's used

play45:46

over here it's used um to Define two

play45:48

different states so it's used to define

play45:50

the name and the email so those are two

play45:53

different things that definitely need to

play45:54

be in uh the user type now since there's

play45:57

multiple things inside of a user a user

play46:00

will be an interface of its own so let's

play46:02

say

play46:03

interface

play46:05

user and let's grab the name the name

play46:08

type which is probably a string right

play46:11

I'm just assuming here and email will be

play46:14

a string as

play46:15

well now um is this correct is there

play46:20

more that we can add here um let's take

play46:22

a

play46:24

look seems like no

play46:29

not so I'll just actually set the user

play46:33

equal to this

play46:35

perfect now let's take a look at the on

play46:37

update seems like on update is not

play46:39

returning anything it's just making a

play46:41

call which means that I know that it

play46:43

will be a function that returns void but

play46:45

it takes in a user and also uh it seems

play46:51

like it Alters the value of user so

play46:54

actually um it just actually takes the

play46:57

user as an argument so we'll cre we'll

play46:59

say that this function returns void but

play47:02

it takes in an object like user of type

play47:06

user perfect so now it seems like that's

play47:09

working and how I def I say that this

play47:12

props is equal to that interface well by

play47:15

just doing it like this now let's take a

play47:18

look at what else we have to do there's

play47:19

more red squiggly lines well there's a

play47:21

handle submit over here and we've

play47:24

actually dealt with this in the past

play47:25

right um we have to in order to grab

play47:27

this event we have to Define its type

play47:30

and if you remember I'll give you like

play47:32

10 seconds to see if you actually

play47:33

remembered uh the way you define this is

play47:35

by saying react. form event HTML form

play47:40

event or form element this is for the

play47:43

handle submit not for the AR change and

play47:46

it seems like actually now everything is

play47:48

fine right so do you notice anything

play47:50

else that we probably want to do even

play47:52

though it doesn't actually show us an

play47:54

error well if you didn't notice we

play47:56

haven't defined the types for the states

play47:59

now it's inferring the type because we

play48:01

passed the prop which we Define the

play48:03

their its types over here but we still

play48:06

do need to set up the types for the

play48:09

states so I'm going to come over here

play48:11

and say that the type of this is

play48:14

string and the type of this is also

play48:17

string and now we officially converted

play48:20

this whole component that used to be

play48:22

JavaScript into a typescript component

play48:25

so if you're migrating from a Javascript

play48:27

app to a typescript one uh this is the

play48:29

kind of work that you would probably be

play48:30

dealing with uh maybe if it's a more

play48:32

complicated one uh more complicated

play48:34

component you will probably have to

play48:36

spend more time but I think that you got

play48:38

the idea from this now one cool thing

play48:41

about using typescript is the fact that

play48:43

it allows you to understand how

play48:46

libraries work internally way easily for

play48:49

example if I click on the US state we

play48:51

can actually see how react defined the

play48:53

use state right see it has some types

play48:56

specific specifically um these are are

play48:58

known as generics uh I didn't explain

play49:01

this video but um you would understand

play49:04

if you watched my other video but um you

play49:06

can see that you can see all the types

play49:08

and different typescript uh

play49:10

functionality that is implemented with

play49:13

react and their specific hooks now if

play49:16

you were working with any other Library

play49:18

you could see it as well on your own and

play49:21

um it actually helps a lot understanding

play49:23

how how how react works as a as a

play49:26

general thing thing like you just um

play49:27

take a look at how libraries Implement

play49:29

their own things and how the things that

play49:31

you're using were actually made so this

play49:35

is basically it for the video If you

play49:36

enjoyed it please leave a like down

play49:38

below and comment what you want to see

play49:39

next subscribe cuz I'm posting every

play49:41

week and I would massively appreciate it

play49:43

again thank you so much brilliant for

play49:45

sponsoring this video I definitely think

play49:47

you guys should check them out because

play49:49

um just watching my videos it works but

play49:51

the thing is always complimenting with

play49:52

knowledge that you can gain elsewhere is

play49:55

really important and you would

play49:57

definitely help the channel if there's a

play49:58

way for you to help the channel while

play49:59

also learning a lot is by doing that by

play50:02

checking them out and I'll massively

play50:04

appreciate if you could do that so yeah

play50:06

that's that's pretty much it really hope

play50:07

you guys enjoyed it and I see you guys

play50:09

next time

Rate This
โ˜…
โ˜…
โ˜…
โ˜…
โ˜…

5.0 / 5 (0 votes)

Related Tags
TypeScriptReactCrash CourseApp DevelopmentWeb DevelopmentProgrammingSoftware EngineeringEducationalTech TutorialCode Enhancement