All language subtitles for English (auto-generated)_en_0_Practical TypeScript – Course for Beginners(1080P_HD)

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranî)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:00,080 --> 00:00:05,040 John smilga teaches this course about 2 00:00:02,240 --> 00:00:06,759 the Practical applications of typescript 3 00:00:05,040 --> 00:00:09,080 typescript is a strongly typed 4 00:00:06,759 --> 00:00:11,480 programming language that builds on 5 00:00:09,080 --> 00:00:14,160 JavaScript this is an excellent course 6 00:00:11,480 --> 00:00:16,160 for people who want to learn typescript 7 00:00:14,160 --> 00:00:17,960 hey what's up it's John from coding anic 8 00:00:16,160 --> 00:00:20,279 and welcome to practical typescript 9 00:00:17,960 --> 00:00:22,359 course we all focus on practical 10 00:00:20,279 --> 00:00:24,760 applications of typescript so you can 11 00:00:22,359 --> 00:00:27,160 start using it effectively in your 12 00:00:24,760 --> 00:00:29,279 projects right away during the course 13 00:00:27,160 --> 00:00:31,920 we'll cover everything from setting up 14 00:00:29,279 --> 00:00:35,160 your development environment to 15 00:00:31,920 --> 00:00:38,640 deploying your typescript applications 16 00:00:35,160 --> 00:00:41,760 the course consists of three major parts 17 00:00:38,640 --> 00:00:44,960 first part typescript fundamentals where 18 00:00:41,760 --> 00:00:47,120 we'll explore basic types interfaces 19 00:00:44,960 --> 00:00:51,000 classes generics and rest of the good 20 00:00:47,120 --> 00:00:53,320 stuff second part react and typescript 21 00:00:51,000 --> 00:00:56,359 where we'll integrate typescript into 22 00:00:53,320 --> 00:00:59,079 react projects create typed components 23 00:00:56,359 --> 00:01:00,840 and manage state with typescript and the 24 00:00:59,079 --> 00:01:02,680 third part 25 00:01:00,840 --> 00:01:05,519 is where we'll put our typescript 26 00:01:02,680 --> 00:01:08,280 knowledge to good use and build a modern 27 00:01:05,519 --> 00:01:11,799 store application with typescript shat 28 00:01:08,280 --> 00:01:13,880 UI and react router if you wish to 29 00:01:11,799 --> 00:01:17,200 explore all of the features of the store 30 00:01:13,880 --> 00:01:21,159 project it's located at this URL 31 00:01:17,200 --> 00:01:26,400 typescript comy sloth ny. apppp again 32 00:01:21,159 --> 00:01:28,920 the URL is typescript comy sloth ny. 33 00:01:26,400 --> 00:01:32,320 apppp quick sign not during this video 34 00:01:28,920 --> 00:01:34,079 will only cover Parts one and two so if 35 00:01:32,320 --> 00:01:36,560 you enjoy the content and wish to work 36 00:01:34,079 --> 00:01:40,159 on the big project as well consider 37 00:01:36,560 --> 00:01:42,960 enrolling in the full Course located at 38 00:01:40,159 --> 00:01:46,960 coding addict IO again the full course 39 00:01:42,960 --> 00:01:49,479 is available at coding addict IO okay 40 00:01:46,960 --> 00:01:53,119 and first let's recap that JavaScript is 41 00:01:49,479 --> 00:01:56,520 considered Loosely or dynamically typed 42 00:01:53,119 --> 00:02:00,920 because variable types are determined at 43 00:01:56,520 --> 00:02:04,200 runtime and can change as the program 44 00:02:00,920 --> 00:02:06,079 executes this means you don't have to 45 00:02:04,200 --> 00:02:09,039 declare the type of the variable when 46 00:02:06,079 --> 00:02:12,520 you create and variable can hold values 47 00:02:09,039 --> 00:02:15,360 of different types at different times so 48 00:02:12,520 --> 00:02:18,400 the following code is a valid JavaScript 49 00:02:15,360 --> 00:02:21,120 code however where are we going to 50 00:02:18,400 --> 00:02:24,400 encounter an error if we try to run this 51 00:02:21,120 --> 00:02:26,959 code correct at runtime and this is 52 00:02:24,400 --> 00:02:28,640 manageable if you have few lines of code 53 00:02:26,959 --> 00:02:30,400 but the bigger the project the more 54 00:02:28,640 --> 00:02:34,560 challenging it will be 55 00:02:30,400 --> 00:02:36,319 to spot such technically innocent bugs 56 00:02:34,560 --> 00:02:39,800 and this is where typescript enters the 57 00:02:36,319 --> 00:02:42,959 picture because it is a programming 58 00:02:39,800 --> 00:02:46,319 language that builds on JavaScript by 59 00:02:42,959 --> 00:02:49,360 adding static typing this means you can 60 00:02:46,319 --> 00:02:50,480 specify things like types of variables 61 00:02:49,360 --> 00:02:53,599 function 62 00:02:50,480 --> 00:02:57,440 parameters return values and some other 63 00:02:53,599 --> 00:03:00,920 things to help catch errors early and 64 00:02:57,440 --> 00:03:03,599 improve the code quality it is a super 65 00:03:00,920 --> 00:03:06,239 set of JavaScript so yes all the 66 00:03:03,599 --> 00:03:08,680 JavaScript code is also a valid 67 00:03:06,239 --> 00:03:11,519 typescript code now one way to think 68 00:03:08,680 --> 00:03:13,799 about it is as follows by using 69 00:03:11,519 --> 00:03:17,040 typescript in our project we'll still 70 00:03:13,799 --> 00:03:19,760 write the JavaScript we already know but 71 00:03:17,040 --> 00:03:23,040 with some elements or sprinkles of 72 00:03:19,760 --> 00:03:27,680 typescript and these elements will act 73 00:03:23,040 --> 00:03:30,920 as safeguards or constraints to help us 74 00:03:27,680 --> 00:03:35,080 catch potential errors more quickly and 75 00:03:30,920 --> 00:03:37,239 without the need for 10,000 console logs 76 00:03:35,080 --> 00:03:41,360 when we introduce typescript to our 77 00:03:37,239 --> 00:03:46,080 project we will write our app in the TS 78 00:03:41,360 --> 00:03:49,439 or in the case of react in the DSX file 79 00:03:46,080 --> 00:03:52,720 there will be a compile or transpile 80 00:03:49,439 --> 00:03:55,920 step where all of the typescript code is 81 00:03:52,720 --> 00:03:59,079 transpiled to good old JavaScript code 82 00:03:55,920 --> 00:04:02,400 and at the end since it is a vanilla 83 00:03:59,079 --> 00:04:04,280 JavaScript code we can run it anywhere 84 00:04:02,400 --> 00:04:07,120 quite often you'll hear people say 85 00:04:04,280 --> 00:04:09,959 something along the lines of well once I 86 00:04:07,120 --> 00:04:12,519 built my first project with typescript 87 00:04:09,959 --> 00:04:15,560 it was very hard to go back and build 88 00:04:12,519 --> 00:04:17,079 with just vanilla JS and even though I 89 00:04:15,560 --> 00:04:20,280 was also very skeptical of such 90 00:04:17,079 --> 00:04:23,240 statements I can verify that it was the 91 00:04:20,280 --> 00:04:26,600 same in my case yes typescript still has 92 00:04:23,240 --> 00:04:29,560 learning curve and yes it involves more 93 00:04:26,600 --> 00:04:31,639 work but at the same time you'll notice 94 00:04:29,560 --> 00:04:34,560 how how you're able to fix bunch of the 95 00:04:31,639 --> 00:04:36,680 errors quickly at build time without 96 00:04:34,560 --> 00:04:38,639 running around like a headless chicken 97 00:04:36,680 --> 00:04:40,960 before we set up a brand new typescript 98 00:04:38,639 --> 00:04:43,320 project I want to mention a few useful 99 00:04:40,960 --> 00:04:45,960 things if you're confused about some 100 00:04:43,320 --> 00:04:47,960 topic or simply want to research a 101 00:04:45,960 --> 00:04:50,440 concept in more detail a good place to 102 00:04:47,960 --> 00:04:54,479 start is typescript docs which are 103 00:04:50,440 --> 00:04:58,800 located at typescript l.org again the 104 00:04:54,479 --> 00:05:02,039 URL is typescript l.org also in there 105 00:04:58,800 --> 00:05:05,440 under the playground tab you will find a 106 00:05:02,039 --> 00:05:07,919 Nifty code sandbox or typescript 107 00:05:05,440 --> 00:05:10,240 playground which I might utilize from 108 00:05:07,919 --> 00:05:13,080 time to time during the course to 109 00:05:10,240 --> 00:05:16,160 underscore some particular point it 110 00:05:13,080 --> 00:05:19,919 works as follows on the left side we 111 00:05:16,160 --> 00:05:23,280 write a valid typescript code and we get 112 00:05:19,919 --> 00:05:25,240 back the resulting JavaScript code now 113 00:05:23,280 --> 00:05:28,400 you won't be able to build the entire 114 00:05:25,240 --> 00:05:31,199 project in there but it is a very cool 115 00:05:28,400 --> 00:05:34,240 option if you you quickly want to test 116 00:05:31,199 --> 00:05:37,039 something as far as the code editor I'm 117 00:05:34,240 --> 00:05:40,039 going to use VSS code and since it's 118 00:05:37,039 --> 00:05:42,680 practically an industry standard these 119 00:05:40,039 --> 00:05:46,400 days my assumption is that you'll use 120 00:05:42,680 --> 00:05:49,280 the same one since I record courses I 121 00:05:46,400 --> 00:05:51,960 use the very basic default theme and 122 00:05:49,280 --> 00:05:54,840 when it comes to my extensions I will 123 00:05:51,960 --> 00:05:57,280 mention some useful ones once there is a 124 00:05:54,840 --> 00:05:59,840 need during the course in order to work 125 00:05:57,280 --> 00:06:02,000 with typescript you will need no 126 00:05:59,840 --> 00:06:05,759 installed on your machine again 127 00:06:02,000 --> 00:06:07,880 something I assume you already have done 128 00:06:05,759 --> 00:06:11,800 but if it's not the case please visit 129 00:06:07,880 --> 00:06:15,039 nodejs.org again the URL is nodejs.org 130 00:06:11,800 --> 00:06:17,720 pick your operating system and follow 131 00:06:15,039 --> 00:06:20,039 the install steps also during the course 132 00:06:17,720 --> 00:06:22,960 I'm going to reference the main course 133 00:06:20,039 --> 00:06:26,840 repo where you'll find source code 134 00:06:22,960 --> 00:06:30,160 challenges notes useful references and 135 00:06:26,840 --> 00:06:32,680 tons of other useful stuff so I highly 136 00:06:30,160 --> 00:06:35,400 suggest getting a hold of the repo 137 00:06:32,680 --> 00:06:38,360 before you continue with the videos just 138 00:06:35,400 --> 00:06:42,360 look for my GitHub profile and under the 139 00:06:38,360 --> 00:06:44,599 repos search for typescript course 140 00:06:42,360 --> 00:06:47,199 repository there are few ways how we can 141 00:06:44,599 --> 00:06:50,160 add typescript to our project since it's 142 00:06:47,199 --> 00:06:53,319 available as an npm package we can 143 00:06:50,160 --> 00:06:56,919 install it globally or on the per 144 00:06:53,319 --> 00:06:59,599 project basis set up the configuration 145 00:06:56,919 --> 00:07:01,720 and run the compile step manually the 146 00:06:59,599 --> 00:07:04,879 this is definitely an option but we're 147 00:07:01,720 --> 00:07:06,080 going to take a different approach which 148 00:07:04,879 --> 00:07:09,720 is going to 149 00:07:06,080 --> 00:07:12,479 be pre-built template because at this 150 00:07:09,720 --> 00:07:15,199 point at least in my opinion it's a more 151 00:07:12,479 --> 00:07:19,000 realistic scenario chances are whether 152 00:07:15,199 --> 00:07:22,360 you want to build a nextjs or remix or a 153 00:07:19,000 --> 00:07:25,319 good old react app there will be already 154 00:07:22,360 --> 00:07:27,919 a template that contains the technology 155 00:07:25,319 --> 00:07:30,639 you want to use as well as types 156 00:07:27,919 --> 00:07:33,879 configuration so more often than not 157 00:07:30,639 --> 00:07:36,160 your job will be to write a valid code 158 00:07:33,879 --> 00:07:39,039 instead of losing your sleep over 159 00:07:36,160 --> 00:07:41,840 correct configuration setup for this 160 00:07:39,039 --> 00:07:43,879 course I picked V since it's one of the 161 00:07:41,840 --> 00:07:46,080 most popular build tools out there but 162 00:07:43,879 --> 00:07:49,000 please keep in mind as far as the 163 00:07:46,080 --> 00:07:51,440 typescript logic is concerned it's the 164 00:07:49,000 --> 00:07:55,720 same if for example you're going to use 165 00:07:51,440 --> 00:07:58,520 nextjs with a typescript template now we 166 00:07:55,720 --> 00:08:01,120 want to run one command in order to set 167 00:07:58,520 --> 00:08:03,720 up our template and I'll show you two 168 00:08:01,120 --> 00:08:07,520 places where you can get the command 169 00:08:03,720 --> 00:08:11,120 First Option you can navigate to V docs 170 00:08:07,520 --> 00:08:15,120 which are located at bejs 171 00:08:11,120 --> 00:08:16,440 dodev then click on getting started and 172 00:08:15,120 --> 00:08:18,639 pretty much you're looking for this 173 00:08:16,440 --> 00:08:22,199 command and then we just need to pick 174 00:08:18,639 --> 00:08:26,039 the template and since my npm version is 175 00:08:22,199 --> 00:08:28,440 bigger than seven I'm going to run my 176 00:08:26,039 --> 00:08:32,640 command with the Double 177 00:08:28,440 --> 00:08:35,519 Dash now as you can see we have tons of 178 00:08:32,640 --> 00:08:39,640 templates available and the idea is 179 00:08:35,519 --> 00:08:43,399 following we go with npm create V latest 180 00:08:39,640 --> 00:08:46,920 then project name then Double 181 00:08:43,399 --> 00:08:49,440 Dash go with the template one and pick 182 00:08:46,920 --> 00:08:52,279 the template as you can see we can go 183 00:08:49,440 --> 00:08:56,880 with reacts as a say not something we're 184 00:08:52,279 --> 00:09:00,959 going to use in the part two and three 185 00:08:56,880 --> 00:09:03,880 we can also for example set up The View 186 00:09:00,959 --> 00:09:07,320 with typescript and we're actually going 187 00:09:03,880 --> 00:09:09,920 to start the course with vanilla 188 00:09:07,320 --> 00:09:11,560 typescript template so this is just 189 00:09:09,920 --> 00:09:13,240 going to be an application built with 190 00:09:11,560 --> 00:09:16,800 vanilla JavaScript we're not going to 191 00:09:13,240 --> 00:09:19,640 use react and uh to tell you honestly we 192 00:09:16,800 --> 00:09:21,240 don't really care about the template we 193 00:09:19,640 --> 00:09:25,720 just want to use it in order to learn 194 00:09:21,240 --> 00:09:27,920 typescript so pretty much most of the 195 00:09:25,720 --> 00:09:29,760 first part of the course will spend in 196 00:09:27,920 --> 00:09:33,480 one file and you'll see what I'm talking 197 00:09:29,760 --> 00:09:35,720 about in a second now remember in the 198 00:09:33,480 --> 00:09:36,920 previous video I mentioned the main 199 00:09:35,720 --> 00:09:39,480 course 200 00:09:36,920 --> 00:09:43,240 repo navigate over here and then look 201 00:09:39,480 --> 00:09:46,920 for typescript tutorial and under this 202 00:09:43,240 --> 00:09:49,320 directory look for read me and here 203 00:09:46,920 --> 00:09:52,120 you'll right away get the command and as 204 00:09:49,320 --> 00:09:55,560 a this is something that I'm going to 205 00:09:52,120 --> 00:09:57,440 copy in literally a few minutes and I 206 00:09:55,560 --> 00:09:59,920 suggest actually doing the same because 207 00:09:57,440 --> 00:10:02,760 in here again you'll find find all of 208 00:09:59,920 --> 00:10:05,040 the notes and challenges and all that 209 00:10:02,760 --> 00:10:07,200 cool stuff so if you want to save 210 00:10:05,040 --> 00:10:10,360 yourself time on typing just navigate 211 00:10:07,200 --> 00:10:14,279 over here and grab the command again npm 212 00:10:10,360 --> 00:10:15,800 create V at latest then at least in my 213 00:10:14,279 --> 00:10:17,959 case the project name is going to be 214 00:10:15,800 --> 00:10:20,079 typescript then double dashes then 215 00:10:17,959 --> 00:10:23,240 template and we're going to go with 216 00:10:20,079 --> 00:10:28,120 vanilla DS so let me grab the 217 00:10:23,240 --> 00:10:31,320 command let me navigate I guess to 218 00:10:28,120 --> 00:10:34,360 desktop and and I'm going to zoom 219 00:10:31,320 --> 00:10:36,639 in then let me set up my 220 00:10:34,360 --> 00:10:39,440 command and once I have successfully 221 00:10:36,639 --> 00:10:41,519 installed the project on my machine I 222 00:10:39,440 --> 00:10:45,600 want to open up my text 223 00:10:41,519 --> 00:10:47,720 error drag and drop the folder and next 224 00:10:45,600 --> 00:10:50,959 we'll take a look at the files and 225 00:10:47,720 --> 00:10:53,959 folders available in this template as 226 00:10:50,959 --> 00:10:56,200 far as the files and folders we've got 227 00:10:53,959 --> 00:10:58,839 over here a public one so this is where 228 00:10:56,200 --> 00:11:01,279 we can place the public assets then we 229 00:10:58,839 --> 00:11:04,519 have a S one essentially this is where 230 00:11:01,279 --> 00:11:06,440 we'll do all of our work a get ignore 231 00:11:04,519 --> 00:11:10,360 where we specify which files are going 232 00:11:06,440 --> 00:11:13,519 to be ignored by The Source control then 233 00:11:10,360 --> 00:11:17,639 index HTML which is actually going to be 234 00:11:13,519 --> 00:11:20,040 our homepage also a package Json 235 00:11:17,639 --> 00:11:23,680 something we're going to cover in more 236 00:11:20,040 --> 00:11:26,399 detail in a few minutes and also a TS 237 00:11:23,680 --> 00:11:29,760 config which we'll cover in Greater 238 00:11:26,399 --> 00:11:32,040 detail later on in the course and before 239 00:11:29,760 --> 00:11:36,160 we take a look at the specific 240 00:11:32,040 --> 00:11:39,480 files and cover the build step here are 241 00:11:36,160 --> 00:11:42,200 a few things I want to do first since 242 00:11:39,480 --> 00:11:45,040 it's a node application we do want to 243 00:11:42,200 --> 00:11:48,200 install the dependencies so effectively 244 00:11:45,040 --> 00:11:53,200 just open up the integrated terminal and 245 00:11:48,200 --> 00:11:57,240 run npm install now also again I highly 246 00:11:53,200 --> 00:11:59,920 suggest getting the readme since in my 247 00:11:57,240 --> 00:12:02,880 opinion you will find find it super 248 00:11:59,920 --> 00:12:06,079 useful as you're following 249 00:12:02,880 --> 00:12:09,440 along so just navigate back to the 250 00:12:06,079 --> 00:12:12,240 project and somewhere create a read me 251 00:12:09,440 --> 00:12:16,120 markdown one and just paste the contents 252 00:12:12,240 --> 00:12:18,639 over here save it and once we install 253 00:12:16,120 --> 00:12:21,880 dependencies now we also get the node 254 00:12:18,639 --> 00:12:25,440 modules so this is where all of our 255 00:12:21,880 --> 00:12:29,440 dependencies are located then we want to 256 00:12:25,440 --> 00:12:32,639 go with npm run Tav which is going to 257 00:12:29,440 --> 00:12:35,600 spin up our project in local development 258 00:12:32,639 --> 00:12:37,880 and we're looking for Local Host 259 00:12:35,600 --> 00:12:40,560 5173 and this is what we should see on 260 00:12:37,880 --> 00:12:43,279 the screen so essentially there's some 261 00:12:40,560 --> 00:12:45,760 images over here a text as well as the 262 00:12:43,279 --> 00:12:48,360 counter now if I'm being perfectly 263 00:12:45,760 --> 00:12:50,760 honest like I mentioned previously we 264 00:12:48,360 --> 00:12:53,839 really don't care about the actual 265 00:12:50,760 --> 00:12:56,600 vanilla JS application I believe towards 266 00:12:53,839 --> 00:13:00,160 the end of the first part of the course 267 00:12:56,600 --> 00:13:02,800 yes we will build a tiny app but most of 268 00:13:00,160 --> 00:13:06,199 our time we'll spend over here in a 269 00:13:02,800 --> 00:13:10,760 source one in one specific file called 270 00:13:06,199 --> 00:13:14,000 tutorial DS where effectively we will 271 00:13:10,760 --> 00:13:16,120 just learn all the typescript 272 00:13:14,000 --> 00:13:18,639 fundamentals now if we take a look at 273 00:13:16,120 --> 00:13:22,560 the package Json we see that we have the 274 00:13:18,639 --> 00:13:25,519 dev one this is the command we just 275 00:13:22,560 --> 00:13:26,800 invoked and also there's a build one now 276 00:13:25,519 --> 00:13:30,959 before we run 277 00:13:26,800 --> 00:13:33,959 build I do want to create a file and 278 00:13:30,959 --> 00:13:36,160 showcase how our setup is going to look 279 00:13:33,959 --> 00:13:38,680 like now one interesting thing that I do 280 00:13:36,160 --> 00:13:43,120 want to point out notice how before the 281 00:13:38,680 --> 00:13:47,320 build command they actually invoke 282 00:13:43,120 --> 00:13:50,399 tsnc so notice how typescript is a Dev 283 00:13:47,320 --> 00:13:53,680 dependency so before the project is 284 00:13:50,399 --> 00:13:55,800 built first it's compiled so that's the 285 00:13:53,680 --> 00:13:58,600 step I mentioned previously where we're 286 00:13:55,800 --> 00:14:02,160 going to write all of our code in the 287 00:13:58,600 --> 00:14:04,880 files with Ds extension and once we 288 00:14:02,160 --> 00:14:09,120 perform the build step it's going to 289 00:14:04,880 --> 00:14:11,800 spit out the vanilla JavaScript code so 290 00:14:09,120 --> 00:14:14,880 if we take a look at the index HTML like 291 00:14:11,800 --> 00:14:16,680 I said this will be our homepage so for 292 00:14:14,880 --> 00:14:18,959 example if you want to change the title 293 00:14:16,680 --> 00:14:23,800 it's definitely an option then they have 294 00:14:18,959 --> 00:14:26,000 over here this div with an ID of app and 295 00:14:23,800 --> 00:14:29,639 effectively in a second you'll see how 296 00:14:26,000 --> 00:14:33,000 in the main DS they in 297 00:14:29,639 --> 00:14:36,199 Jack the images the divs and all that 298 00:14:33,000 --> 00:14:39,639 into this app one so now let's navigate 299 00:14:36,199 --> 00:14:41,800 to the main DS in here they import 300 00:14:39,639 --> 00:14:44,240 styles again something we're not really 301 00:14:41,800 --> 00:14:48,000 concerned with also two images the 302 00:14:44,240 --> 00:14:49,839 typescript one and V1 and also a counter 303 00:14:48,000 --> 00:14:54,240 and then they 304 00:14:49,839 --> 00:14:57,040 select div with an ID of app and like I 305 00:14:54,240 --> 00:15:01,040 said they dynamically inject the 306 00:14:57,040 --> 00:15:03,880 elements into the d as you can see there 307 00:15:01,040 --> 00:15:05,560 are some already sprinkles of typescript 308 00:15:03,880 --> 00:15:09,079 and essentially this is something that 309 00:15:05,560 --> 00:15:13,000 we're going to cover during the first 310 00:15:09,079 --> 00:15:15,959 part of the course but most of the logic 311 00:15:13,000 --> 00:15:18,199 should look very familiar correct 312 00:15:15,959 --> 00:15:21,399 because this is straight up JavaScript 313 00:15:18,199 --> 00:15:22,880 again with just sprinkles of typescript 314 00:15:21,399 --> 00:15:24,759 now if we take a look at the counter 315 00:15:22,880 --> 00:15:26,360 it's going to be same idea this is 316 00:15:24,759 --> 00:15:28,160 straight up JavaScript with the only 317 00:15:26,360 --> 00:15:30,680 difference that now we're looking for 318 00:15:28,160 --> 00:15:34,720 element and actually we specifically say 319 00:15:30,680 --> 00:15:37,319 that the type is going to be HTML button 320 00:15:34,720 --> 00:15:39,920 element now how is our setup going to 321 00:15:37,319 --> 00:15:42,000 look like since I said that I mean we 322 00:15:39,920 --> 00:15:43,399 really don't care about this code and to 323 00:15:42,000 --> 00:15:46,160 tell you honestly we're not actually 324 00:15:43,399 --> 00:15:48,600 going to look too much in a browser 325 00:15:46,160 --> 00:15:51,920 either most of the time will spend in 326 00:15:48,600 --> 00:15:54,519 the error well in here in the source I 327 00:15:51,920 --> 00:15:57,639 want to create a new file in my case I'm 328 00:15:54,519 --> 00:16:00,480 going to call this tutorial TS the name 329 00:15:57,639 --> 00:16:03,839 is totally up to you just make sure that 330 00:16:00,480 --> 00:16:06,480 you add the TS extension so we're going 331 00:16:03,839 --> 00:16:09,360 to go over here with tutorial then TS we 332 00:16:06,480 --> 00:16:11,240 create a new file and for now I just 333 00:16:09,360 --> 00:16:12,839 want to log just so I can see that 334 00:16:11,240 --> 00:16:16,639 everything works and I'm going to go 335 00:16:12,839 --> 00:16:20,440 with typescript and then tutorial let me 336 00:16:16,639 --> 00:16:23,000 save it and then in order for this code 337 00:16:20,440 --> 00:16:25,920 to run we just want to navigate to Main 338 00:16:23,000 --> 00:16:28,600 TS and we just want to import a file 339 00:16:25,920 --> 00:16:31,680 that's it we're not going to do anything 340 00:16:28,600 --> 00:16:35,279 else we'll simply go with import and now 341 00:16:31,680 --> 00:16:37,680 we're just looking for our TS file so 342 00:16:35,279 --> 00:16:40,480 tutorial and TS and the moment we do 343 00:16:37,680 --> 00:16:42,360 that once we navigate to the browser if 344 00:16:40,480 --> 00:16:43,399 everything is correct we should see that 345 00:16:42,360 --> 00:16:46,240 console 346 00:16:43,399 --> 00:16:48,839 log and if that's the case we are in 347 00:16:46,240 --> 00:16:52,440 good shape and before we move on to 348 00:16:48,839 --> 00:16:56,079 typescript fundamentals let me show you 349 00:16:52,440 --> 00:17:01,680 the result of the build step again let's 350 00:16:56,079 --> 00:17:04,480 just recall that before V our build tool 351 00:17:01,680 --> 00:17:08,199 builds the production ready application 352 00:17:04,480 --> 00:17:11,280 it's actually going to compile our code 353 00:17:08,199 --> 00:17:13,480 which essentially just means that all of 354 00:17:11,280 --> 00:17:16,480 the typescript elements we're going to 355 00:17:13,480 --> 00:17:20,079 have in our code are going to be 356 00:17:16,480 --> 00:17:22,160 transpiled to good old vanilla 357 00:17:20,079 --> 00:17:25,280 JavaScript and essentially during this 358 00:17:22,160 --> 00:17:27,959 video I just want to showcase that so 359 00:17:25,280 --> 00:17:30,880 don't worry about the code I'm going to 360 00:17:27,959 --> 00:17:33,240 write in fact you don't have to follow 361 00:17:30,880 --> 00:17:36,480 along I just want to showcase the end 362 00:17:33,240 --> 00:17:39,480 result so let's say if we're going to 363 00:17:36,480 --> 00:17:41,440 add something typescript for example 364 00:17:39,480 --> 00:17:44,480 interface of course something we're 365 00:17:41,440 --> 00:17:47,640 going to cover during the first part of 366 00:17:44,480 --> 00:17:52,080 the course and I'm going to go with name 367 00:17:47,640 --> 00:17:54,799 String and ID number and as a sign note 368 00:17:52,080 --> 00:17:57,559 essentially it's just a shape of an 369 00:17:54,799 --> 00:18:00,840 object and then I'm going to create the 370 00:17:57,559 --> 00:18:03,799 object and I'm going to go with some 371 00:18:00,840 --> 00:18:06,520 value so add the type over here and now 372 00:18:03,799 --> 00:18:09,480 I just need to provide name which has to 373 00:18:06,520 --> 00:18:12,280 be a string now so I'm just going to go 374 00:18:09,480 --> 00:18:14,960 with random and comma and then we're 375 00:18:12,280 --> 00:18:18,480 going to go with ID and then one two 3 376 00:18:14,960 --> 00:18:22,240 and then let's also log so let's go with 377 00:18:18,480 --> 00:18:26,720 some object so once we navigate right 378 00:18:22,240 --> 00:18:30,480 now to a terminal we have few options if 379 00:18:26,720 --> 00:18:33,000 you want you can always stop the dev 380 00:18:30,480 --> 00:18:37,440 server and essentially you do that with 381 00:18:33,000 --> 00:18:39,360 contrl C or you can just open up a new 382 00:18:37,440 --> 00:18:41,640 terminal window and that is going to be 383 00:18:39,360 --> 00:18:45,200 my Approach so essentially now I want to 384 00:18:41,640 --> 00:18:48,440 go with npm and not Dev but we're 385 00:18:45,200 --> 00:18:50,720 actually looking for build so we run 386 00:18:48,440 --> 00:18:52,080 this command if we don't have any 387 00:18:50,720 --> 00:18:54,760 typescript 388 00:18:52,080 --> 00:18:58,240 errors we're going to be successful and 389 00:18:54,760 --> 00:19:00,640 now notice we have this this folder so 390 00:18:58,240 --> 00:19:04,000 this is our production ready application 391 00:19:00,640 --> 00:19:07,120 we still have index HTML okay everything 392 00:19:04,000 --> 00:19:09,480 is beautiful and then in the assets 393 00:19:07,120 --> 00:19:12,760 we'll have the index JS so this is 394 00:19:09,480 --> 00:19:16,159 essentially our application in vanilla 395 00:19:12,760 --> 00:19:17,760 JS and if we take a look at our code so 396 00:19:16,159 --> 00:19:20,360 notice I have this console log 397 00:19:17,760 --> 00:19:24,640 typescript tutorial so I clearly know 398 00:19:20,360 --> 00:19:27,840 that it's coming from our file notice 399 00:19:24,640 --> 00:19:31,000 there is no interface anymore again 400 00:19:27,840 --> 00:19:34,720 there is some kind of minified object 401 00:19:31,000 --> 00:19:36,159 over here and effectively we also conso 402 00:19:34,720 --> 00:19:38,600 log it that's it so there is no 403 00:19:36,159 --> 00:19:41,640 interface there is no type string or 404 00:19:38,600 --> 00:19:44,799 type number or in fact any of the other 405 00:19:41,640 --> 00:19:48,480 stuff that we have here as far as the 406 00:19:44,799 --> 00:19:51,880 typescript elements so again essentially 407 00:19:48,480 --> 00:19:54,880 we'll write all of our logic in files 408 00:19:51,880 --> 00:19:57,760 with Ds or DSX extension if we're 409 00:19:54,880 --> 00:19:59,240 working with react and then once we 410 00:19:57,760 --> 00:20:02,159 build our project 411 00:19:59,240 --> 00:20:03,640 we're going to get back our good old 412 00:20:02,159 --> 00:20:06,159 vanilla JavaScript code which 413 00:20:03,640 --> 00:20:08,200 essentially we can run anywhere so 414 00:20:06,159 --> 00:20:11,039 hopefully it's clear what's happening 415 00:20:08,200 --> 00:20:13,919 during the build process so now let me 416 00:20:11,039 --> 00:20:16,640 clean up our project first I want to 417 00:20:13,919 --> 00:20:19,280 remove the disc folder then also I'm 418 00:20:16,640 --> 00:20:21,760 going to remove my second terminal 419 00:20:19,280 --> 00:20:26,000 window and at the very end I just want 420 00:20:21,760 --> 00:20:28,760 to remove this code from tutorial yes so 421 00:20:26,000 --> 00:20:31,240 this is going to be our starting point 422 00:20:28,760 --> 00:20:36,080 essentially we have console log in 423 00:20:31,240 --> 00:20:38,159 tutorial DS and if we take a look at the 424 00:20:36,080 --> 00:20:40,159 browser we should see over here 425 00:20:38,159 --> 00:20:43,039 typescript tutorial and if you have the 426 00:20:40,159 --> 00:20:45,679 same result now we can move on to the 427 00:20:43,039 --> 00:20:48,520 next task okay and let's start our 428 00:20:45,679 --> 00:20:51,400 typescript journey by discussing type 429 00:20:48,520 --> 00:20:55,039 annotations which allow us to 430 00:20:51,400 --> 00:20:58,280 specify the variable type so let's say 431 00:20:55,039 --> 00:21:00,640 in my project I have a variable by the 432 00:20:58,280 --> 00:21:03,000 name of awesome name and if you have 433 00:21:00,640 --> 00:21:04,799 watched any of my courses and tutorials 434 00:21:03,000 --> 00:21:08,679 you know that the most awesome name ever 435 00:21:04,799 --> 00:21:12,120 is shake and B since I'm working in a 436 00:21:08,679 --> 00:21:14,840 typescript project I can actually 437 00:21:12,120 --> 00:21:18,279 explicitly set up the type and the 438 00:21:14,840 --> 00:21:21,200 syntax is following we go with colon and 439 00:21:18,279 --> 00:21:24,520 then since I want this to be a string I 440 00:21:21,200 --> 00:21:27,400 type string now there are quite a few 441 00:21:24,520 --> 00:21:30,279 types and I'm not going to list all of 442 00:21:27,400 --> 00:21:31,960 them I really find it pointless 443 00:21:30,279 --> 00:21:35,279 essentially the most straightforward 444 00:21:31,960 --> 00:21:37,600 ones are string number and Boolean and 445 00:21:35,279 --> 00:21:40,120 again the syntax is going to be the same 446 00:21:37,600 --> 00:21:43,000 so we just go over here with the colon 447 00:21:40,120 --> 00:21:46,919 and then the type and of course as we're 448 00:21:43,000 --> 00:21:49,279 working through the course we'll get to 449 00:21:46,919 --> 00:21:52,720 know the other types as well and the 450 00:21:49,279 --> 00:21:56,400 moment I set this type for my 451 00:21:52,720 --> 00:21:58,600 variable I get two things out of the box 452 00:21:56,400 --> 00:22:01,600 essentially it can never be something 453 00:21:58,600 --> 00:22:04,919 something else so I can go to the next 454 00:22:01,600 --> 00:22:10,039 line and set it equal to a function and 455 00:22:04,919 --> 00:22:13,240 also I'll WR way get the methods that 456 00:22:10,039 --> 00:22:15,720 are associated with this type so in this 457 00:22:13,240 --> 00:22:18,679 case it's going to be a string so just 458 00:22:15,720 --> 00:22:22,559 to demonstrate that if I go to the next 459 00:22:18,679 --> 00:22:26,240 line and if I set it equal to a value of 460 00:22:22,559 --> 00:22:28,320 something this is still valid why well 461 00:22:26,240 --> 00:22:31,880 because type is string so everything's 462 00:22:28,320 --> 00:22:35,600 correct correct also I can go to next 463 00:22:31,880 --> 00:22:39,440 line and set it equal to awesome Dot and 464 00:22:35,600 --> 00:22:40,679 notice right away I get only the methods 465 00:22:39,440 --> 00:22:43,840 that are 466 00:22:40,679 --> 00:22:46,760 associated with the typ string which 467 00:22:43,840 --> 00:22:49,520 again is super awesome because 468 00:22:46,760 --> 00:22:51,760 essentially I avoid some unnecessary 469 00:22:49,520 --> 00:22:55,000 bugs so in this case I'm just going to 470 00:22:51,760 --> 00:22:58,000 go to application and now if I log this 471 00:22:55,000 --> 00:23:00,720 value if I go with awesome name and take 472 00:22:58,000 --> 00:23:03,679 a look at my console if everything is 473 00:23:00,720 --> 00:23:06,120 correct should be equal to a something 474 00:23:03,679 --> 00:23:10,080 so we set it equal to different string 475 00:23:06,120 --> 00:23:12,559 value and then we ran the two uppercase 476 00:23:10,080 --> 00:23:14,480 method now one thing we canot do is 477 00:23:12,559 --> 00:23:17,320 something like this where I go with 478 00:23:14,480 --> 00:23:19,279 awesome name and set it equal to 20 479 00:23:17,320 --> 00:23:21,520 which is a different type so notice 480 00:23:19,279 --> 00:23:25,279 immediately typescript starts yelling at 481 00:23:21,520 --> 00:23:28,880 me type number is not assignable to a 482 00:23:25,279 --> 00:23:31,520 type string and I want to repeat the 483 00:23:28,880 --> 00:23:34,679 same thing for the number as well as the 484 00:23:31,520 --> 00:23:36,840 bullion so just so it's clear we're 485 00:23:34,679 --> 00:23:38,720 going to start with some kind of 486 00:23:36,840 --> 00:23:42,320 variable in my case I'm going to go with 487 00:23:38,720 --> 00:23:46,279 amount I'll set it equal to a number and 488 00:23:42,320 --> 00:23:48,760 again I can do anything that's allowed 489 00:23:46,279 --> 00:23:50,120 with the numbers for example and set 490 00:23:48,760 --> 00:23:53,720 equal 491 00:23:50,120 --> 00:23:58,880 amount 12 minus 1 something like that 492 00:23:53,720 --> 00:24:01,120 but cannot set amount equal to pens 493 00:23:58,880 --> 00:24:03,240 that's not going to work right away 494 00:24:01,120 --> 00:24:05,960 typescript is going to be super helpful 495 00:24:03,240 --> 00:24:08,080 it's going to say Hey listen you said 496 00:24:05,960 --> 00:24:11,360 this is going to be the number so yeah 497 00:24:08,080 --> 00:24:14,400 you can run the number methods you can 498 00:24:11,360 --> 00:24:17,480 do calculations but you cannot just 499 00:24:14,400 --> 00:24:19,360 reassign it to something else lastly 500 00:24:17,480 --> 00:24:20,640 let's take a look at the Boolean pretty 501 00:24:19,360 --> 00:24:23,520 much same 502 00:24:20,640 --> 00:24:26,720 deal we're going to go with is awesome 503 00:24:23,520 --> 00:24:29,559 we're going to add a colon set up a 504 00:24:26,720 --> 00:24:33,039 Boolean type for Star it's going to be 505 00:24:29,559 --> 00:24:36,000 true then we're going to set is awesome 506 00:24:33,039 --> 00:24:38,520 equal to false and then let's try to set 507 00:24:36,000 --> 00:24:40,799 it equal to shake and bake again the 508 00:24:38,520 --> 00:24:42,240 most awesome name ever so we're going to 509 00:24:40,799 --> 00:24:46,880 go with is 510 00:24:42,240 --> 00:24:50,600 awesome and let's go with shake and bake 511 00:24:46,880 --> 00:24:53,120 again cannot do it because the type is 512 00:24:50,600 --> 00:24:55,360 bulling and essentially in the process 513 00:24:53,120 --> 00:24:58,440 we're just avoiding this entire 514 00:24:55,360 --> 00:25:00,480 debacle where initially it can be one 515 00:24:58,440 --> 00:25:02,480 value then three lines later it's 516 00:25:00,480 --> 00:25:04,320 something else and then we're turning 517 00:25:02,480 --> 00:25:06,559 into a third thing and then start 518 00:25:04,320 --> 00:25:10,000 running some methods on it 519 00:25:06,559 --> 00:25:12,080 essentially this is static so now we say 520 00:25:10,000 --> 00:25:14,520 Hey listen this is going to be string 521 00:25:12,080 --> 00:25:17,320 and string only same goes for number 522 00:25:14,520 --> 00:25:20,679 Boolean and in the process we just avoid 523 00:25:17,320 --> 00:25:23,640 bunch of unnecessary bugs that come from 524 00:25:20,679 --> 00:25:26,600 typos that come from just using wrong 525 00:25:23,640 --> 00:25:28,559 methods and that sort of thing and 526 00:25:26,600 --> 00:25:31,080 before we work on our first challenge 527 00:25:28,559 --> 00:25:33,919 let me quickly mention something 528 00:25:31,080 --> 00:25:37,120 typescript is actually really smart and 529 00:25:33,919 --> 00:25:40,520 can infer the type of the variable based 530 00:25:37,120 --> 00:25:43,559 on the values when it's defined now the 531 00:25:40,520 --> 00:25:46,919 fancy name is type inference but 532 00:25:43,559 --> 00:25:50,679 essentially for us it just means that we 533 00:25:46,919 --> 00:25:53,399 can remove all of these type annotations 534 00:25:50,679 --> 00:25:56,159 and we'll still get all of the goodies 535 00:25:53,399 --> 00:26:00,240 that I covered in a previous video so 536 00:25:56,159 --> 00:26:02,760 now if I hover over the awesome name 537 00:26:00,240 --> 00:26:05,799 notice how based on the value when the 538 00:26:02,760 --> 00:26:08,120 variable was defined typescript can 539 00:26:05,799 --> 00:26:10,880 nicely infer the type so in this case 540 00:26:08,120 --> 00:26:13,200 it's string then it's the amount and 541 00:26:10,880 --> 00:26:17,200 then when it comes to is awesome 542 00:26:13,200 --> 00:26:20,039 typescript knows that it's a Boolean now 543 00:26:17,200 --> 00:26:22,600 don't dismiss the type annotations we're 544 00:26:20,039 --> 00:26:25,640 still going to use them but most likely 545 00:26:22,600 --> 00:26:28,360 not for such straightforward variables 546 00:26:25,640 --> 00:26:29,760 type inference also has some gotas 547 00:26:28,360 --> 00:26:33,520 especially when we start working with 548 00:26:29,760 --> 00:26:35,279 more complex values so yes typescript is 549 00:26:33,520 --> 00:26:37,000 always going to try to help you when it 550 00:26:35,279 --> 00:26:40,240 comes to functions when it comes to 551 00:26:37,000 --> 00:26:43,640 variables it will try to kind of get the 552 00:26:40,240 --> 00:26:45,880 type based on the value and obviously 553 00:26:43,640 --> 00:26:48,919 it's super awesome because it saves us 554 00:26:45,880 --> 00:26:51,120 time notice I don't have to go to Every 555 00:26:48,919 --> 00:26:53,880 variable and just make sure that the 556 00:26:51,120 --> 00:26:56,039 type is set but there are some 557 00:26:53,880 --> 00:26:57,760 questiones we need to aware of and don't 558 00:26:56,039 --> 00:27:00,000 dismiss type annotations because we're 559 00:26:57,760 --> 00:27:03,720 still going to use them throughout the 560 00:27:00,000 --> 00:27:06,120 course just not four variables like this 561 00:27:03,720 --> 00:27:08,279 okay and now it's time for our Challenge 562 00:27:06,120 --> 00:27:11,200 and since it's our first one I will 563 00:27:08,279 --> 00:27:14,200 cover the steps as well but with the 564 00:27:11,200 --> 00:27:17,200 future challenges just so we can save 565 00:27:14,200 --> 00:27:20,200 little bit of time I will most likely 566 00:27:17,200 --> 00:27:22,600 right away start working on the solution 567 00:27:20,200 --> 00:27:24,480 also since it's our first one it's going 568 00:27:22,600 --> 00:27:26,600 to be extremely 569 00:27:24,480 --> 00:27:29,279 straightforward I just wanted to get the 570 00:27:26,600 --> 00:27:31,640 ball rolling just so we get used to this 571 00:27:29,279 --> 00:27:34,399 idea that we learn some stuff and then 572 00:27:31,640 --> 00:27:36,360 we work on some kind of challenge so I 573 00:27:34,399 --> 00:27:39,320 want you to create a variable of type 574 00:27:36,360 --> 00:27:42,480 string and try to invoke a string method 575 00:27:39,320 --> 00:27:45,039 on it then same is going to be with a 576 00:27:42,480 --> 00:27:48,640 number create a variable and try to 577 00:27:45,039 --> 00:27:51,519 perform a math operation also I want you 578 00:27:48,640 --> 00:27:52,880 to create the Boolean one and perform 579 00:27:51,519 --> 00:27:57,360 The Logical 580 00:27:52,880 --> 00:28:00,360 operation and then try to just assign 581 00:27:57,360 --> 00:28:04,120 some values to those 582 00:28:00,360 --> 00:28:07,799 variables that are different types so 583 00:28:04,120 --> 00:28:10,519 just try to come up with something that 584 00:28:07,799 --> 00:28:13,360 shouldn't supposed to work and see 585 00:28:10,519 --> 00:28:17,440 whether typescript complains about it 586 00:28:13,360 --> 00:28:20,360 now you can use the type adaptation or 587 00:28:17,440 --> 00:28:24,799 you can also use the type inference 588 00:28:20,360 --> 00:28:27,039 that's totally up to you again super 589 00:28:24,799 --> 00:28:29,640 straightforward challenge where just 590 00:28:27,039 --> 00:28:33,080 want to navigate to tutorial I want to 591 00:28:29,640 --> 00:28:36,440 come up with some kind of variable for 592 00:28:33,080 --> 00:28:39,519 my string in my case I will use the type 593 00:28:36,440 --> 00:28:42,080 annotation don't ask me why just think 594 00:28:39,519 --> 00:28:44,320 that it's going to be better in this 595 00:28:42,080 --> 00:28:46,559 case so I'm going to go with hello and 596 00:28:44,320 --> 00:28:51,200 then let's go with 597 00:28:46,559 --> 00:28:54,320 typescript okay beautiful then let's run 598 00:28:51,200 --> 00:28:58,640 the two oper case so greeting is equal 599 00:28:54,320 --> 00:29:01,159 to greeting dot then two uppercase let's 600 00:28:58,640 --> 00:29:03,279 invoke this this should work we should 601 00:29:01,159 --> 00:29:05,640 have no issues and as long as I don't 602 00:29:03,279 --> 00:29:09,000 have the red squiggly lines I'm good to 603 00:29:05,640 --> 00:29:12,000 go so typescript is not yelling at me 604 00:29:09,000 --> 00:29:14,240 then also let me set up the age so in 605 00:29:12,000 --> 00:29:18,080 this case it's going to be a number I'm 606 00:29:14,240 --> 00:29:22,200 going to set it equal to 25 then let me 607 00:29:18,080 --> 00:29:24,399 add a five to it okay also works 608 00:29:22,200 --> 00:29:29,399 everything is beautiful and then we want 609 00:29:24,399 --> 00:29:33,279 to go with is adult so that's my Boolean 610 00:29:29,399 --> 00:29:36,399 let's add a type annotation here AG is 611 00:29:33,279 --> 00:29:39,200 let's say bigger or equal to 18 now this 612 00:29:36,399 --> 00:29:42,039 should be true and you know this is the 613 00:29:39,200 --> 00:29:45,480 case where I will log it so let's see is 614 00:29:42,039 --> 00:29:48,600 adult is true yep that's the one so now 615 00:29:45,480 --> 00:29:51,679 let me just set it equal to the opposite 616 00:29:48,600 --> 00:29:55,640 I'm going to go with his adult is equal 617 00:29:51,679 --> 00:29:58,200 to opposite is adult again let's log it 618 00:29:55,640 --> 00:30:01,279 and now it's going to be false and and 619 00:29:58,200 --> 00:30:04,159 then somewhere here let me just try to 620 00:30:01,279 --> 00:30:07,320 set the greeing equal to 10 that 621 00:30:04,159 --> 00:30:10,880 shouldn't work also we can go with age 622 00:30:07,320 --> 00:30:13,200 and set it equal to 30 so string again 623 00:30:10,880 --> 00:30:15,240 we're going to get the error and lastly 624 00:30:13,200 --> 00:30:17,159 we have the is adult and I'm just going 625 00:30:15,240 --> 00:30:21,120 to set it equal to yes and as you can 626 00:30:17,159 --> 00:30:24,000 see all of them result in errors so 627 00:30:21,120 --> 00:30:26,960 again we have one type and typescript is 628 00:30:24,000 --> 00:30:30,120 not happy when we just randomly decide 629 00:30:26,960 --> 00:30:32,799 that our variable is going to be of 630 00:30:30,120 --> 00:30:35,279 different type all right and before we 631 00:30:32,799 --> 00:30:37,440 continue with tutorial let me show you 632 00:30:35,279 --> 00:30:40,640 something interesting about the setup 633 00:30:37,440 --> 00:30:43,159 you see even though we have these 634 00:30:40,640 --> 00:30:48,600 typescript errors over here we're going 635 00:30:43,159 --> 00:30:50,600 to be able to run our project locally so 636 00:30:48,600 --> 00:30:55,200 what am I talking about well if I go 637 00:30:50,600 --> 00:30:57,360 here and set up the console and log and 638 00:30:55,200 --> 00:31:01,120 in my case I'm just going to look for 639 00:30:57,360 --> 00:31:05,159 all three values So reading age and is 640 00:31:01,120 --> 00:31:07,440 adult if we navigate to a console you 641 00:31:05,159 --> 00:31:09,720 can see first of all that all of the 642 00:31:07,440 --> 00:31:12,480 variables have the values that they're 643 00:31:09,720 --> 00:31:15,240 not supposed to so typescript is 644 00:31:12,480 --> 00:31:18,480 complaining but it's still works and 645 00:31:15,240 --> 00:31:21,480 also notice how our project didn't crash 646 00:31:18,480 --> 00:31:25,320 we're just getting these typescript 647 00:31:21,480 --> 00:31:28,279 errors correct so here's the deal when 648 00:31:25,320 --> 00:31:30,000 we're working with typescript actually 649 00:31:28,279 --> 00:31:33,080 project is going to work even with the 650 00:31:30,000 --> 00:31:35,559 errors locally but whenever we want to 651 00:31:33,080 --> 00:31:38,440 host a project what do we need to do 652 00:31:35,559 --> 00:31:41,760 well we need to build it that's usually 653 00:31:38,440 --> 00:31:46,440 the setup and in order to build it we'll 654 00:31:41,760 --> 00:31:48,760 have to pass the typescript checks so if 655 00:31:46,440 --> 00:31:51,480 we try to build with typescript errors 656 00:31:48,760 --> 00:31:55,039 we're going to fail and as a result we 657 00:31:51,480 --> 00:31:57,679 won't be able to host the project let me 658 00:31:55,039 --> 00:31:59,000 show you the package Json notice over 659 00:31:57,679 --> 00:32:01,679 here 660 00:31:59,000 --> 00:32:04,600 so we have Dev command which essentially 661 00:32:01,679 --> 00:32:06,240 just spins up the dev server and we also 662 00:32:04,600 --> 00:32:09,519 have the build command and remember 663 00:32:06,240 --> 00:32:12,440 before we even run feed build we 664 00:32:09,519 --> 00:32:17,039 actually compile this with typescript 665 00:32:12,440 --> 00:32:19,519 and again if we have some errors we 666 00:32:17,039 --> 00:32:22,639 won't get the I believe it was this 667 00:32:19,519 --> 00:32:25,480 folder where we have the static assets 668 00:32:22,639 --> 00:32:28,399 which in turn are going to be used to 669 00:32:25,480 --> 00:32:30,880 host our project so let me showcase that 670 00:32:28,399 --> 00:32:33,679 just so we are on the same page first I 671 00:32:30,880 --> 00:32:37,880 want to stop the dev server let me run 672 00:32:33,679 --> 00:32:42,120 npm run build so at the moment I have 673 00:32:37,880 --> 00:32:45,159 the errors typescript errors and 674 00:32:42,120 --> 00:32:48,120 immediately I just get back these error 675 00:32:45,159 --> 00:32:50,240 messages in my console so in order to 676 00:32:48,120 --> 00:32:53,799 fix it what do we need to do well in 677 00:32:50,240 --> 00:32:56,159 this case it's sort of simple where can 678 00:32:53,799 --> 00:32:59,200 just comment them out delete them 679 00:32:56,159 --> 00:33:01,120 whatever doesn't really matter again let 680 00:32:59,200 --> 00:33:04,360 me clear everything 681 00:33:01,120 --> 00:33:07,720 here let me run the last one npm run 682 00:33:04,360 --> 00:33:11,399 build and now since I don't have any 683 00:33:07,720 --> 00:33:13,639 typescript errors notice I have the this 684 00:33:11,399 --> 00:33:17,360 folder and this is where I have my 685 00:33:13,639 --> 00:33:20,720 production ready application so again 686 00:33:17,360 --> 00:33:24,279 locally even with typescript errors 687 00:33:20,720 --> 00:33:27,320 you'll be able to run the project 688 00:33:24,279 --> 00:33:30,799 however you have some issues once it's 689 00:33:27,320 --> 00:33:33,679 time to to deploy it once it's time to 690 00:33:30,799 --> 00:33:36,080 build the production ready application 691 00:33:33,679 --> 00:33:39,360 and up next let me introduce you to a 692 00:33:36,080 --> 00:33:43,760 union type which allows a variable to 693 00:33:39,360 --> 00:33:45,919 hold a value of multiple distinct types 694 00:33:43,760 --> 00:33:49,960 for now we're just going to take a look 695 00:33:45,919 --> 00:33:52,639 at some basic examples but since Union 696 00:33:49,960 --> 00:33:55,279 types are super powerful it's not the 697 00:33:52,639 --> 00:33:57,440 only time you'll see them in the course 698 00:33:55,279 --> 00:34:00,200 so once we start working with functions 699 00:33:57,440 --> 00:34:04,360 or and all that yep we're going to use 700 00:34:00,200 --> 00:34:10,280 the union type as well so let's imagine 701 00:34:04,360 --> 00:34:13,280 I have a variable by the name of tax and 702 00:34:10,280 --> 00:34:15,320 typically it's a number correct so I can 703 00:34:13,280 --> 00:34:19,200 set it equal to 704 00:34:15,320 --> 00:34:21,520 10 typescript can nicely infer the type 705 00:34:19,200 --> 00:34:23,960 and I'm good to go but let's imagine 706 00:34:21,520 --> 00:34:26,760 that whether I'm getting it from the API 707 00:34:23,960 --> 00:34:30,960 or whether user provides that value it 708 00:34:26,760 --> 00:34:34,560 can also be a string I can go here set 709 00:34:30,960 --> 00:34:37,000 up my colon then go with number so it 710 00:34:34,560 --> 00:34:40,040 will definitely be a number and I can 711 00:34:37,000 --> 00:34:43,919 use this pipe operator and then I can 712 00:34:40,040 --> 00:34:46,040 just add another type which in my case 713 00:34:43,919 --> 00:34:50,839 is going to be string now please keep in 714 00:34:46,040 --> 00:34:52,720 mind you can add 10 another types but I 715 00:34:50,839 --> 00:34:55,200 mean in our case we're just going to 716 00:34:52,720 --> 00:34:57,079 stick with two but yeah you can go here 717 00:34:55,200 --> 00:34:59,520 and add for example Boolean it's 718 00:34:57,079 --> 00:35:02,200 definitely an OP option so now I can go 719 00:34:59,520 --> 00:35:04,440 to a next line and I can set it equal to 720 00:35:02,200 --> 00:35:06,480 a 100 and typescript is not going to 721 00:35:04,440 --> 00:35:09,040 complain because yeah one of the types 722 00:35:06,480 --> 00:35:13,000 is actually number now I can also go 723 00:35:09,040 --> 00:35:14,640 here and set it equal to a string and in 724 00:35:13,000 --> 00:35:18,320 my case I'm going to go 725 00:35:14,640 --> 00:35:20,839 with uh let's go with $10 over here so 726 00:35:18,320 --> 00:35:24,280 both of them are valid because now I 727 00:35:20,839 --> 00:35:28,480 have Union type where it can be either a 728 00:35:24,280 --> 00:35:31,480 number or a string now once we start 729 00:35:28,480 --> 00:35:33,000 working with more complex examples yes 730 00:35:31,480 --> 00:35:35,960 we'll have to do some checks and all 731 00:35:33,000 --> 00:35:39,720 that so all of that is coming up but 732 00:35:35,960 --> 00:35:43,160 this is a super powerful concept where 733 00:35:39,720 --> 00:35:46,200 essentially we can say to typescript hey 734 00:35:43,160 --> 00:35:49,480 this is going to be either this or it's 735 00:35:46,200 --> 00:35:51,880 going to be the another type now also 736 00:35:49,480 --> 00:35:54,319 something that I was blown away is the 737 00:35:51,880 --> 00:35:56,599 fact that we can set up the literal 738 00:35:54,319 --> 00:35:59,000 value type so that's the fancy name 739 00:35:56,599 --> 00:36:01,000 essentially it just means that I can 740 00:35:59,000 --> 00:36:04,800 provide the literal 741 00:36:01,000 --> 00:36:08,760 values let's imagine we have a 742 00:36:04,800 --> 00:36:11,960 request status so that's my variable now 743 00:36:08,760 --> 00:36:14,520 what are the typical values for the 744 00:36:11,960 --> 00:36:17,400 request status well quite often it's 745 00:36:14,520 --> 00:36:19,240 pending success and error so for 746 00:36:17,400 --> 00:36:22,760 starters I can just go here with pending 747 00:36:19,240 --> 00:36:24,960 so that will be my initial value then go 748 00:36:22,760 --> 00:36:28,560 with the colon and now I can just 749 00:36:24,960 --> 00:36:30,720 provide the potential string values 750 00:36:28,560 --> 00:36:32,560 which are going to be pending then again 751 00:36:30,720 --> 00:36:36,640 I use the pipe 752 00:36:32,560 --> 00:36:39,640 operator I provide success then another 753 00:36:36,640 --> 00:36:43,680 pipe and I'm going to go with error and 754 00:36:39,640 --> 00:36:46,599 what's super super cool is the fact that 755 00:36:43,680 --> 00:36:49,040 whenever I work with this request status 756 00:36:46,599 --> 00:36:52,160 notice I right away get these 757 00:36:49,040 --> 00:36:55,280 suggestions so it can either be error 758 00:36:52,160 --> 00:36:58,720 pending or success and 759 00:36:55,280 --> 00:37:01,560 again it helps me to avoid errors 760 00:36:58,720 --> 00:37:04,800 because it's way harder to mistype 761 00:37:01,560 --> 00:37:07,680 something and also I kind of just go 762 00:37:04,800 --> 00:37:09,240 here and say random I mean first of all 763 00:37:07,680 --> 00:37:11,040 it doesn't match my suggestions and 764 00:37:09,240 --> 00:37:13,880 second right away typescript is going to 765 00:37:11,040 --> 00:37:16,640 complain so I don't have to find doubt 766 00:37:13,880 --> 00:37:18,720 about this bug in the runtime I actually 767 00:37:16,640 --> 00:37:20,920 can see it right away as I'm building 768 00:37:18,720 --> 00:37:24,240 the project so let me go here with 769 00:37:20,920 --> 00:37:26,599 success error is going to work so 770 00:37:24,240 --> 00:37:30,160 there's nothing wrong with that but if 771 00:37:26,599 --> 00:37:33,000 I'll try to set it equal to random as 772 00:37:30,160 --> 00:37:35,680 you just saw I mean typescript is not 773 00:37:33,000 --> 00:37:39,440 going to be happy about it topic we're 774 00:37:35,680 --> 00:37:42,839 going to come back to when we set up 775 00:37:39,440 --> 00:37:47,200 more complex examples and again Union 776 00:37:42,839 --> 00:37:50,079 types allow us to set up multiple types 777 00:37:47,200 --> 00:37:52,599 for the same variable and while we're 778 00:37:50,079 --> 00:37:56,560 still in a roll let's also cover type 779 00:37:52,599 --> 00:37:59,880 any which allows us to opt out of the 780 00:37:56,560 --> 00:38:02,200 type checking so I'm going to create a 781 00:37:59,880 --> 00:38:06,079 variable and I'm going to name it not 782 00:38:02,200 --> 00:38:09,200 sure so not sure now I will set it equal 783 00:38:06,079 --> 00:38:12,920 to four and since we already covered 784 00:38:09,200 --> 00:38:16,000 that typescript can infert a Type we'll 785 00:38:12,920 --> 00:38:19,520 see over here this number but I can also 786 00:38:16,000 --> 00:38:23,240 go with colon and set up any over here 787 00:38:19,520 --> 00:38:25,760 now any means exactly what the name 788 00:38:23,240 --> 00:38:27,920 implies this going to be anything I can 789 00:38:25,760 --> 00:38:31,400 set it up equal to function string 790 00:38:27,920 --> 00:38:33,640 Boolean whatever so in next line can say 791 00:38:31,400 --> 00:38:38,079 you know what not sure is actually going 792 00:38:33,640 --> 00:38:41,000 to be a string maybe a string instead 793 00:38:38,079 --> 00:38:43,319 then on next line I can set it equal to 794 00:38:41,000 --> 00:38:44,160 false and as you can see that script is 795 00:38:43,319 --> 00:38:48,000 not 796 00:38:44,160 --> 00:38:51,240 complaining now as you're looking at it 797 00:38:48,000 --> 00:38:52,800 probably the first thought is well wait 798 00:38:51,240 --> 00:38:56,680 a minute that's 799 00:38:52,800 --> 00:39:00,599 how variables behave in JavaScript and 800 00:38:56,680 --> 00:39:03,720 you're absolutely absolutely correct now 801 00:39:00,599 --> 00:39:06,160 as with everything in programming 802 00:39:03,720 --> 00:39:09,240 there's bunch of fights over it whether 803 00:39:06,160 --> 00:39:12,000 you should use any at all as far as my 804 00:39:09,240 --> 00:39:14,480 two sents be very careful when you 805 00:39:12,000 --> 00:39:17,079 introduce any to your 806 00:39:14,480 --> 00:39:20,200 application because it can spread like 807 00:39:17,079 --> 00:39:22,839 wildfire so you started off with setting 808 00:39:20,200 --> 00:39:25,400 up one variable type any and then next 809 00:39:22,839 --> 00:39:26,920 thing you know you have any all over 810 00:39:25,400 --> 00:39:29,200 your application because for example 811 00:39:26,920 --> 00:39:31,920 when you return any from the function 812 00:39:29,200 --> 00:39:35,040 then the result is any and on and on and 813 00:39:31,920 --> 00:39:38,839 on and on so definitely have a good 814 00:39:35,040 --> 00:39:41,760 reason why you want to use any and as I 815 00:39:38,839 --> 00:39:46,160 note any is something you'll see quite 816 00:39:41,760 --> 00:39:49,240 often when typescript is trying to come 817 00:39:46,160 --> 00:39:51,040 up with a type but we haven't provided 818 00:39:49,240 --> 00:39:54,079 any clues for example if I'm just going 819 00:39:51,040 --> 00:39:56,720 to go with some random and I'm not going 820 00:39:54,079 --> 00:39:59,920 to set up the value as you hover over it 821 00:39:56,720 --> 00:40:02,319 notice now this is going to be type any 822 00:39:59,920 --> 00:40:05,440 so this is something we quite often get 823 00:40:02,319 --> 00:40:07,440 by default when typescript is trying to 824 00:40:05,440 --> 00:40:11,040 get the type but I mean I haven't 825 00:40:07,440 --> 00:40:12,960 provided any clues what this value is 826 00:40:11,040 --> 00:40:15,560 going to be and this is essentially 827 00:40:12,960 --> 00:40:18,280 something that we'll constantly try to 828 00:40:15,560 --> 00:40:20,359 fix where for example for functions 829 00:40:18,280 --> 00:40:22,640 implicitly it starts with any but we 830 00:40:20,359 --> 00:40:26,280 don't want it to be any we want it to be 831 00:40:22,640 --> 00:40:27,880 some kind of type so just be careful 832 00:40:26,280 --> 00:40:30,359 when you use it it's definitely an 833 00:40:27,880 --> 00:40:32,599 option it's useful if let's say you want 834 00:40:30,359 --> 00:40:36,480 to test something it's also useful if 835 00:40:32,599 --> 00:40:39,880 you have a pretty big codebase and you 836 00:40:36,480 --> 00:40:42,160 just slowly want to introduce typescript 837 00:40:39,880 --> 00:40:44,640 so you still want to run the project but 838 00:40:42,160 --> 00:40:47,000 you slowly want to add it to your 839 00:40:44,640 --> 00:40:51,000 project yeah it's definitely the case 840 00:40:47,000 --> 00:40:53,480 just again be very mindful when you set 841 00:40:51,000 --> 00:40:55,599 something any because next thing you 842 00:40:53,480 --> 00:40:58,280 know it half of the application is going 843 00:40:55,599 --> 00:41:01,040 to be any and then at that point I mean 844 00:40:58,280 --> 00:41:03,200 it kind of loses the sense to use the 845 00:41:01,040 --> 00:41:06,119 typescript in the first place again 846 00:41:03,200 --> 00:41:08,680 those are just my two scents if you go 847 00:41:06,119 --> 00:41:11,400 to Google you'll find definitely way 848 00:41:08,680 --> 00:41:13,359 stronger opinions but let's just leave 849 00:41:11,400 --> 00:41:16,319 it at that and let's move on to the next 850 00:41:13,359 --> 00:41:20,040 topic and next let's see a practical 851 00:41:16,319 --> 00:41:22,680 application of type annotation where 852 00:41:20,040 --> 00:41:25,040 essentially I have a list of books so I 853 00:41:22,680 --> 00:41:28,079 just have some strings over here and I 854 00:41:25,040 --> 00:41:30,599 also have a variable which at the moment 855 00:41:28,079 --> 00:41:33,160 is undefined and there's no type since 856 00:41:30,599 --> 00:41:36,000 again I want to showcase what type is 857 00:41:33,160 --> 00:41:39,040 going to be set by default and I want to 858 00:41:36,000 --> 00:41:42,480 set up a loop where I iterate over and 859 00:41:39,040 --> 00:41:45,520 then if it matches one of these titles 860 00:41:42,480 --> 00:41:48,599 over here then I'll set the variable 861 00:41:45,520 --> 00:41:51,119 Found book equal to that title so first 862 00:41:48,599 --> 00:41:54,960 let's set up the loop I'm going to go 863 00:41:51,119 --> 00:41:57,359 with four off Loop so lead book off 864 00:41:54,960 --> 00:42:00,319 books then we're going to set up the 865 00:41:57,359 --> 00:42:01,839 condition so if book and I think I'm 866 00:42:00,319 --> 00:42:04,880 going to go with the 867 00:42:01,839 --> 00:42:08,800 first string over here so I'm going to 868 00:42:04,880 --> 00:42:13,079 go with 1984 if that's the case then 869 00:42:08,800 --> 00:42:13,079 phone book is going to be equal to a 870 00:42:13,400 --> 00:42:21,240 book and we're also going to break out 871 00:42:17,440 --> 00:42:23,280 so let's add the break keyword let's 872 00:42:21,240 --> 00:42:27,240 save it if everything is correct this 873 00:42:23,280 --> 00:42:30,280 should be equal to a title so we can 874 00:42:27,240 --> 00:42:32,520 actually go with log and then found 875 00:42:30,280 --> 00:42:37,000 book let's take a look at the console 876 00:42:32,520 --> 00:42:39,960 yep we are good to go now everything is 877 00:42:37,000 --> 00:42:42,160 awesome but let's take a look at the 878 00:42:39,960 --> 00:42:43,760 type we have over here by default it's 879 00:42:42,160 --> 00:42:47,440 actually set to 880 00:42:43,760 --> 00:42:51,559 any which leads to problems because 881 00:42:47,440 --> 00:42:53,800 anywhere whether that is in the loop or 882 00:42:51,559 --> 00:42:55,960 over here I can do something like this I 883 00:42:53,800 --> 00:42:58,960 can set it actually equal to 10 so 884 00:42:55,960 --> 00:43:01,920 typescript is not stopping me and this 885 00:42:58,960 --> 00:43:03,599 is the behavior which I don't want 886 00:43:01,920 --> 00:43:06,559 because I actually want it to be a 887 00:43:03,599 --> 00:43:09,359 string otherwise yeah in here it's going 888 00:43:06,559 --> 00:43:11,480 to be a string if I'm successful inside 889 00:43:09,359 --> 00:43:13,599 of this Loop but then eventually it 890 00:43:11,480 --> 00:43:16,800 might turn into a function again good 891 00:43:13,599 --> 00:43:19,400 old JavaScript variable Behavior but I 892 00:43:16,800 --> 00:43:22,040 don't want this I want this to be a 893 00:43:19,400 --> 00:43:24,119 typescript project where I explicitly 894 00:43:22,040 --> 00:43:26,160 know what is the type so what's the 895 00:43:24,119 --> 00:43:28,880 solution well we can navigate back over 896 00:43:26,160 --> 00:43:31,760 here and and we can set it equal to a 897 00:43:28,880 --> 00:43:35,280 string but now we have another problem 898 00:43:31,760 --> 00:43:36,960 so let me remove these console logs how 899 00:43:35,280 --> 00:43:39,880 do we know that for sure it's going to 900 00:43:36,960 --> 00:43:44,559 be a string because keep in mind this 901 00:43:39,880 --> 00:43:48,040 runs when at runtime so if let's say I 902 00:43:44,559 --> 00:43:50,319 set it equal to 903 00:43:48,040 --> 00:43:52,760 1983 well this Found book is not going 904 00:43:50,319 --> 00:43:56,760 to be equal to a book what is it going 905 00:43:52,760 --> 00:44:00,800 to be equal to well remember by default 906 00:43:56,760 --> 00:44:03,760 variable are what undefined so if you'll 907 00:44:00,800 --> 00:44:06,880 try to set up some kind of logic over 908 00:44:03,760 --> 00:44:09,240 here assuming that it's a string 909 00:44:06,880 --> 00:44:11,680 typescript is going to yell at you it's 910 00:44:09,240 --> 00:44:14,319 going to say Hey listen you are using it 911 00:44:11,680 --> 00:44:16,599 before there's even any kind of value 912 00:44:14,319 --> 00:44:18,559 because again typescript is not going to 913 00:44:16,599 --> 00:44:21,960 check whether at the runtime this will 914 00:44:18,559 --> 00:44:24,480 be equal to a book so yeah we can set up 915 00:44:21,960 --> 00:44:25,760 the logic over here where I can say if 916 00:44:24,480 --> 00:44:29,200 for 917 00:44:25,760 --> 00:44:31,040 sure the Found book is equal to a book 918 00:44:29,200 --> 00:44:34,480 typescript knows that actually these are 919 00:44:31,040 --> 00:44:37,079 strings so we can run the method on it 920 00:44:34,480 --> 00:44:40,400 for example again let's go with length 921 00:44:37,079 --> 00:44:42,280 so this is going to work but we cannot 922 00:44:40,400 --> 00:44:44,760 do it outside of the loop because in 923 00:44:42,280 --> 00:44:47,400 here there's no guarantee that actually 924 00:44:44,760 --> 00:44:49,559 this will be a string so we need to set 925 00:44:47,400 --> 00:44:52,440 up the union type where I'm going to go 926 00:44:49,559 --> 00:44:56,000 with okay it potentially could be a 927 00:44:52,440 --> 00:44:58,440 string but by default actually it is 928 00:44:56,000 --> 00:45:00,760 undefined so it's another type that we 929 00:44:58,440 --> 00:45:03,480 can use so now by default it's going to 930 00:45:00,760 --> 00:45:06,040 be undefined if we're successful it's 931 00:45:03,480 --> 00:45:08,680 going to be equal to a string and what's 932 00:45:06,040 --> 00:45:11,520 super super super awesome is the fact 933 00:45:08,680 --> 00:45:14,720 that typescript is going to be extremely 934 00:45:11,520 --> 00:45:17,599 helpful where if you try to again go 935 00:45:14,720 --> 00:45:20,160 with the good old length notice how it 936 00:45:17,599 --> 00:45:23,720 will immediately add this optional 937 00:45:20,160 --> 00:45:26,119 chaining why well because it can be 938 00:45:23,720 --> 00:45:28,680 undefined and we can see that if we take 939 00:45:26,119 --> 00:45:33,000 a look at the error message if you just 940 00:45:28,680 --> 00:45:35,800 added dot notice Found book possibly can 941 00:45:33,000 --> 00:45:38,160 be undefined and therefore typescript is 942 00:45:35,800 --> 00:45:41,839 going to add that optional chaining so 943 00:45:38,160 --> 00:45:45,359 now we only run this length if it's 944 00:45:41,839 --> 00:45:48,680 actually equal to a string if we have 945 00:45:45,359 --> 00:45:51,960 some kind of value so if we won't be 946 00:45:48,680 --> 00:45:55,119 able to find a book which is the current 947 00:45:51,960 --> 00:45:57,880 case then this actually won't trigger 948 00:45:55,119 --> 00:46:01,040 the runtime error now if we're 949 00:45:57,880 --> 00:46:03,520 successful then we'll be able to nicely 950 00:46:01,040 --> 00:46:05,160 see the length of the book you know what 951 00:46:03,520 --> 00:46:07,319 I will change this one around I'm just 952 00:46:05,160 --> 00:46:11,000 going to go here to Found 953 00:46:07,319 --> 00:46:13,640 book and set it equal to uppercase let 954 00:46:11,000 --> 00:46:16,280 me do it this way so we're going for 955 00:46:13,640 --> 00:46:18,640 uppercase let's invoke that and then 956 00:46:16,280 --> 00:46:21,040 this one I'll just log it again it 957 00:46:18,640 --> 00:46:23,440 really depends whether we're able to 958 00:46:21,040 --> 00:46:27,280 find the book or no so now let me move 959 00:46:23,440 --> 00:46:29,400 this pH book set it up here in my my 960 00:46:27,280 --> 00:46:31,440 console and let's see what is going to 961 00:46:29,400 --> 00:46:32,960 be the result so I'm going to navigate 962 00:46:31,440 --> 00:46:37,440 over here if everything is correct I'm 963 00:46:32,960 --> 00:46:40,160 going to get my four if I'm not 964 00:46:37,440 --> 00:46:42,480 successful then essentially it's just 965 00:46:40,160 --> 00:46:45,079 going to be undefined but I won't have 966 00:46:42,480 --> 00:46:47,559 that runtime error where I'm trying to 967 00:46:45,079 --> 00:46:49,839 access the property on undefined 968 00:46:47,559 --> 00:46:52,480 hopefully that is clear okay and before 969 00:46:49,839 --> 00:46:56,280 we move on to the next topic let's work 970 00:46:52,480 --> 00:46:59,640 on a small Union type challenge where 971 00:46:56,280 --> 00:47:01,400 first we want to create the order status 972 00:46:59,640 --> 00:47:03,640 and in there we'll set up the literal 973 00:47:01,400 --> 00:47:07,760 types and also we want to set up the 974 00:47:03,640 --> 00:47:10,760 discount which can be a number or string 975 00:47:07,760 --> 00:47:12,880 type and you know what just because I 976 00:47:10,760 --> 00:47:15,000 like to break the rules I'm going to 977 00:47:12,880 --> 00:47:18,040 start with the second one doesn't really 978 00:47:15,000 --> 00:47:21,240 matter I'm going to go with number or 979 00:47:18,040 --> 00:47:25,160 string by default I'll set it equal to 980 00:47:21,240 --> 00:47:27,520 20 or any other number value and I also 981 00:47:25,160 --> 00:47:29,319 want to test it whether I can set it 982 00:47:27,520 --> 00:47:33,000 equal to 983 00:47:29,319 --> 00:47:37,200 20% now this works but if I'll try to 984 00:47:33,000 --> 00:47:39,720 set up discount equal to True 985 00:47:37,200 --> 00:47:42,599 essentially a Boolean typescript will 986 00:47:39,720 --> 00:47:44,720 yell at me which is exactly what we 987 00:47:42,599 --> 00:47:49,119 expected and also let's set up that 988 00:47:44,720 --> 00:47:51,079 order status so order status variable by 989 00:47:49,119 --> 00:47:53,960 default is going to be equal to 990 00:47:51,079 --> 00:47:58,800 processing but we want to set up those 991 00:47:53,960 --> 00:48:01,839 literal types so this variable can be 992 00:47:58,800 --> 00:48:04,160 only one of these values so we're going 993 00:48:01,839 --> 00:48:07,800 to start with processing then we'll set 994 00:48:04,160 --> 00:48:11,079 up our pipe and we're going to go with 995 00:48:07,800 --> 00:48:14,720 shipped and also we want to add 996 00:48:11,079 --> 00:48:17,640 delivered over here so now we can go to 997 00:48:14,720 --> 00:48:19,240 the next line set it equal to one of 998 00:48:17,640 --> 00:48:22,599 these ones and everything is going to 999 00:48:19,240 --> 00:48:25,800 work but if I'll try to set it equal to 1000 00:48:22,599 --> 00:48:28,559 canceled again typescript will yell at 1001 00:48:25,800 --> 00:48:32,000 me so let me just set this one up 1002 00:48:28,559 --> 00:48:35,280 quickly where if we go with order status 1003 00:48:32,000 --> 00:48:37,839 I just decide that I'm going to pass 1004 00:48:35,280 --> 00:48:40,400 this value over here not going to be 1005 00:48:37,839 --> 00:48:42,599 successful since I can see this red 1006 00:48:40,400 --> 00:48:45,520 squiggly line let me comment this one 1007 00:48:42,599 --> 00:48:48,760 out we're done with the challenge and up 1008 00:48:45,520 --> 00:48:51,160 next we're going to discuss arrays in 1009 00:48:48,760 --> 00:48:53,799 tapescript and as far as the arrays in 1010 00:48:51,160 --> 00:48:57,440 typescript the general idea is following 1011 00:48:53,799 --> 00:48:59,319 let's say I have prices array and it's 1012 00:48:57,440 --> 00:49:03,880 all numbers so we're going to go over 1013 00:48:59,319 --> 00:49:06,200 here with 100 75 and then 42 if I want 1014 00:49:03,880 --> 00:49:06,960 to set up the type which is something I 1015 00:49:06,200 --> 00:49:10,240 want to 1016 00:49:06,960 --> 00:49:13,319 do again same deal we go with colon then 1017 00:49:10,240 --> 00:49:15,880 we go with type in this case number and 1018 00:49:13,319 --> 00:49:17,720 then we set up the square brackets so 1019 00:49:15,880 --> 00:49:22,400 now I'm essentially telling typescript 1020 00:49:17,720 --> 00:49:26,400 that this array will only have numbers 1021 00:49:22,400 --> 00:49:29,640 so if I had a number 34 everything is 1022 00:49:26,400 --> 00:49:31,920 fine if file tried to stick hello I'm 1023 00:49:29,640 --> 00:49:35,400 going to get the error because this one 1024 00:49:31,920 --> 00:49:37,760 is type string and I 1025 00:49:35,400 --> 00:49:40,119 explicitly told typescript that this is 1026 00:49:37,760 --> 00:49:42,799 actually going to be an array of numbers 1027 00:49:40,119 --> 00:49:46,040 and the same is going to work if I will 1028 00:49:42,799 --> 00:49:49,760 try to invoke push so if I'll try to add 1029 00:49:46,040 --> 00:49:52,000 the item number is fine string will 1030 00:49:49,760 --> 00:49:55,400 cause the error so let me comment this 1031 00:49:52,000 --> 00:49:57,200 one out the same works with string array 1032 00:49:55,400 --> 00:50:00,240 let's say we have 1033 00:49:57,200 --> 00:50:03,960 fruit and this is going to be equal to 1034 00:50:00,240 --> 00:50:06,200 apple and orange we want to set up the 1035 00:50:03,960 --> 00:50:10,000 type over here in this case it's going 1036 00:50:06,200 --> 00:50:12,440 to be a string and the same rules apply 1037 00:50:10,000 --> 00:50:16,119 if we try to add the item that doesn't 1038 00:50:12,440 --> 00:50:19,480 match type we get the red squiggly line 1039 00:50:16,119 --> 00:50:23,079 now be careful with the empty square 1040 00:50:19,480 --> 00:50:25,640 brackets so if we go here and just come 1041 00:50:23,079 --> 00:50:29,280 up with random 1042 00:50:25,640 --> 00:50:32,119 values and set it equal to empty square 1043 00:50:29,280 --> 00:50:34,520 bracket so if we won't provide the type 1044 00:50:32,119 --> 00:50:36,440 over here typ script just assumes that 1045 00:50:34,520 --> 00:50:40,880 this will always always be an empty 1046 00:50:36,440 --> 00:50:43,839 array so if I set it equal to my array 1047 00:50:40,880 --> 00:50:47,160 and start adding items typescript will 1048 00:50:43,839 --> 00:50:49,480 immediately complain and notice as we 1049 00:50:47,160 --> 00:50:52,400 hover it says well type string is not 1050 00:50:49,480 --> 00:50:54,640 assignable to an Mt right so be careful 1051 00:50:52,400 --> 00:50:56,799 with this one I know it's temp thing 1052 00:50:54,640 --> 00:50:59,760 also be careful when you don't 1053 00:50:56,799 --> 00:51:04,359 explicitly set up the type so if I were 1054 00:50:59,760 --> 00:51:08,720 to create an empty values 1055 00:51:04,359 --> 00:51:12,000 array typescript will get the type but 1056 00:51:08,720 --> 00:51:15,760 check it out now the type is any so just 1057 00:51:12,000 --> 00:51:18,880 like before we'll run into those issues 1058 00:51:15,760 --> 00:51:21,040 because now we can change these values 1059 00:51:18,880 --> 00:51:23,880 any way we want so we can take the 1060 00:51:21,040 --> 00:51:27,119 string then it turns into a number and 1061 00:51:23,880 --> 00:51:30,280 same deal so be careful yes 1062 00:51:27,119 --> 00:51:32,559 typescript will infer the type but most 1063 00:51:30,280 --> 00:51:35,880 likely you don't want to set it equal to 1064 00:51:32,559 --> 00:51:38,520 any so this is the case where we want to 1065 00:51:35,880 --> 00:51:40,799 create the MD but we explicitly want to 1066 00:51:38,520 --> 00:51:44,440 say yeah this is going to be type number 1067 00:51:40,799 --> 00:51:48,880 or string or Boolean or whatever now 1068 00:51:44,440 --> 00:51:51,440 also while we're still on a subject of 1069 00:51:48,880 --> 00:51:53,960 typescript inferring the type let's take 1070 00:51:51,440 --> 00:51:57,760 a look at this example let's say I have 1071 00:51:53,960 --> 00:52:01,680 the names array and I have over here 1072 00:51:57,760 --> 00:52:04,599 Peter and Susan so if we hover over the 1073 00:52:01,680 --> 00:52:07,799 names everything is correct typescript 1074 00:52:04,599 --> 00:52:10,000 sees that we have over here a list of 1075 00:52:07,799 --> 00:52:13,920 strings and it correctly says the type 1076 00:52:10,000 --> 00:52:17,160 for this array is string but here's the 1077 00:52:13,920 --> 00:52:20,240 gotcha since typescript is inferring 1078 00:52:17,160 --> 00:52:23,400 those values based on what we have over 1079 00:52:20,240 --> 00:52:26,240 here in the array it doesn't stop me 1080 00:52:23,400 --> 00:52:29,160 from doing this and now if we hover over 1081 00:52:26,240 --> 00:52:32,119 it we can see that actually we have this 1082 00:52:29,160 --> 00:52:34,000 Union type like I said we'll be coming 1083 00:52:32,119 --> 00:52:37,160 back to this topic and now we have 1084 00:52:34,000 --> 00:52:40,559 string or number so essentially now I 1085 00:52:37,160 --> 00:52:43,760 can add numbers as well therefore be 1086 00:52:40,559 --> 00:52:47,119 careful when you're relying 100% on 1087 00:52:43,760 --> 00:52:49,359 typescript to set the type it's not 1088 00:52:47,119 --> 00:52:52,240 going to stop you from adding different 1089 00:52:49,359 --> 00:52:55,680 values to the particular array and in 1090 00:52:52,240 --> 00:53:00,280 that case well the type also is going to 1091 00:52:55,680 --> 00:53:04,480 change now we can explicitly set up the 1092 00:53:00,280 --> 00:53:07,280 array to be one or the other type so I 1093 00:53:04,480 --> 00:53:10,160 can go with array for example and I can 1094 00:53:07,280 --> 00:53:14,839 say that you know what this array is 1095 00:53:10,160 --> 00:53:17,720 going to have either string or a 1096 00:53:14,839 --> 00:53:19,839 Boolean again we still set up the square 1097 00:53:17,720 --> 00:53:22,920 brackets over here and we set it equal 1098 00:53:19,839 --> 00:53:27,040 to an array where we set up those two 1099 00:53:22,920 --> 00:53:29,359 values so we can go with Apple 1100 00:53:27,040 --> 00:53:32,839 then true which is a Boolean and the 1101 00:53:29,359 --> 00:53:35,240 same deal orange and let's set the false 1102 00:53:32,839 --> 00:53:37,920 one over here and with this in place we 1103 00:53:35,240 --> 00:53:41,200 can start working on our next challenge 1104 00:53:37,920 --> 00:53:43,760 okay and once we're familiar with arrays 1105 00:53:41,200 --> 00:53:46,119 now let's work on a tiny challenge where 1106 00:53:43,760 --> 00:53:49,079 we want to set up the temperatures array 1107 00:53:46,119 --> 00:53:50,760 which will be type number and colors 1108 00:53:49,079 --> 00:53:53,079 which is going to be type string and 1109 00:53:50,760 --> 00:53:55,920 then we also want to set up the mixed 1110 00:53:53,079 --> 00:53:59,079 array so let's start with temperatures 1111 00:53:55,920 --> 00:54:00,319 essentially our array of numbers now 1112 00:53:59,079 --> 00:54:03,119 this one 1113 00:54:00,319 --> 00:54:06,760 probably is going to be the hardest as 1114 00:54:03,119 --> 00:54:10,960 far as the spelling yep notice over 1115 00:54:06,760 --> 00:54:14,440 here me fix that one okay good that took 1116 00:54:10,960 --> 00:54:17,960 a while then I want to set up my type 1117 00:54:14,440 --> 00:54:22,480 number and we're going to set up 20 then 1118 00:54:17,960 --> 00:54:26,160 25 and then 30 and let's try to push I 1119 00:54:22,480 --> 00:54:27,799 don't know a string of hot so let's 1120 00:54:26,160 --> 00:54:31,440 actually 1121 00:54:27,799 --> 00:54:34,040 then go with push and let's try to pass 1122 00:54:31,440 --> 00:54:37,079 in the string and we won't be successful 1123 00:54:34,040 --> 00:54:39,240 so we can nicely comment this one out 1124 00:54:37,079 --> 00:54:43,160 after that we have colors 1125 00:54:39,240 --> 00:54:46,440 one so this will be actually type of 1126 00:54:43,160 --> 00:54:50,559 string and in here we're going to go 1127 00:54:46,440 --> 00:54:53,799 with red green and blue let's save it 1128 00:54:50,559 --> 00:54:57,640 and let's try to add a Boolean why not 1129 00:54:53,799 --> 00:54:59,920 so colors here dot then push and we're 1130 00:54:57,640 --> 00:55:03,000 looking for a bull and mow which is not 1131 00:54:59,920 --> 00:55:06,319 allowed and then let's set up the mixed 1132 00:55:03,000 --> 00:55:08,200 one so we're going to go with variable 1133 00:55:06,319 --> 00:55:11,839 in this case I'm going to call this 1134 00:55:08,200 --> 00:55:16,480 mixed array and this one will be either 1135 00:55:11,839 --> 00:55:19,839 a number or a string so number pipe and 1136 00:55:16,480 --> 00:55:24,079 we set up a string so let's set it equal 1137 00:55:19,839 --> 00:55:25,960 to our array and in here you know what 1138 00:55:24,079 --> 00:55:29,079 just to make it interesting why don't we 1139 00:55:25,960 --> 00:55:31,680 try to add true so bullan and notice 1140 00:55:29,079 --> 00:55:35,280 right away I'll have my error so let's 1141 00:55:31,680 --> 00:55:38,680 go with one then two and then we want to 1142 00:55:35,280 --> 00:55:41,520 add three and with this in place we can 1143 00:55:38,680 --> 00:55:45,000 move on to the next topic and up next 1144 00:55:41,520 --> 00:55:47,400 let's discuss object in typescript in 1145 00:55:45,000 --> 00:55:50,039 order to do that I'm going to create a 1146 00:55:47,400 --> 00:55:54,480 car and here I'm going to set up the 1147 00:55:50,039 --> 00:55:58,280 brand that one will be equal to a let's 1148 00:55:54,480 --> 00:56:01,079 say Toyota and also I'll set up the year 1149 00:55:58,280 --> 00:56:03,680 so first let me set up the string over 1150 00:56:01,079 --> 00:56:08,079 here and then let's go with year and 1151 00:56:03,680 --> 00:56:11,280 we'll set that one equal to a number and 1152 00:56:08,079 --> 00:56:15,000 if we want to set up the type annotation 1153 00:56:11,280 --> 00:56:17,280 it might look a little funky but we want 1154 00:56:15,000 --> 00:56:20,280 to do it this way we again we go with 1155 00:56:17,280 --> 00:56:23,559 colon then we go with curly braces and 1156 00:56:20,280 --> 00:56:26,599 then one by one we set up those 1157 00:56:23,559 --> 00:56:28,640 properties over here so I have a brand 1158 00:56:26,599 --> 00:56:32,760 and what is the type well it's going to 1159 00:56:28,640 --> 00:56:35,319 be string but also I have a year and 1160 00:56:32,760 --> 00:56:38,440 what is that one well that's a number so 1161 00:56:35,319 --> 00:56:40,880 again either if I try 1162 00:56:38,440 --> 00:56:44,480 to add some 1163 00:56:40,880 --> 00:56:48,720 property manually here where I'm setting 1164 00:56:44,480 --> 00:56:51,640 up the object or for example if I do it 1165 00:56:48,720 --> 00:56:55,280 dynamically by going card Dot and then 1166 00:56:51,640 --> 00:56:58,559 the property value unless this property 1167 00:56:55,280 --> 00:57:00,599 is in the type annotation it's not going 1168 00:56:58,559 --> 00:57:03,240 to work I think I'm just going to use 1169 00:57:00,599 --> 00:57:06,280 the color example so let's say I'll set 1170 00:57:03,240 --> 00:57:09,359 up the color and it's going to be equal 1171 00:57:06,280 --> 00:57:12,480 to Blue notice right away I have the 1172 00:57:09,359 --> 00:57:16,160 complaint because the property is not in 1173 00:57:12,480 --> 00:57:19,599 my type annotation and the same goes if 1174 00:57:16,160 --> 00:57:23,079 I go with car and brand yeah I can set 1175 00:57:19,599 --> 00:57:26,640 it equal to Ford since that's the 1176 00:57:23,079 --> 00:57:29,880 correct type but again if I go with car 1177 00:57:26,640 --> 00:57:33,280 and color it's not going to fly so 1178 00:57:29,880 --> 00:57:34,960 typescript will immediately complain now 1179 00:57:33,280 --> 00:57:38,480 if you're looking at it and you're 1180 00:57:34,960 --> 00:57:41,119 thinking to yourself well boy this is 1181 00:57:38,480 --> 00:57:43,200 super clunky over here I have this 1182 00:57:41,119 --> 00:57:46,000 object and then actually this is going 1183 00:57:43,200 --> 00:57:49,720 to be my type annotation don't worry 1184 00:57:46,000 --> 00:57:53,760 this is just essentially the most basic 1185 00:57:49,720 --> 00:57:56,640 setup but I wanted to cover it since I 1186 00:57:53,760 --> 00:58:00,079 wanted you to understand how everything 1187 00:57:56,640 --> 00:58:03,319 works under the hood but eventually we 1188 00:58:00,079 --> 00:58:06,640 will cover something called type aliases 1189 00:58:03,319 --> 00:58:10,960 where we will nicely be able to set up 1190 00:58:06,640 --> 00:58:14,160 this shape somewhere separately and 1191 00:58:10,960 --> 00:58:17,440 effectively just reuse the variable so 1192 00:58:14,160 --> 00:58:19,760 again you won't have this clunky code 1193 00:58:17,440 --> 00:58:22,640 most likely you won't use it but I 1194 00:58:19,760 --> 00:58:25,039 definitely wanted you to kind of see 1195 00:58:22,640 --> 00:58:27,559 this type of syntax so now once I have 1196 00:58:25,039 --> 00:58:30,319 my first car well I can create another 1197 00:58:27,559 --> 00:58:33,200 one in this case it's going to be a car 1198 00:58:30,319 --> 00:58:36,079 one since I'm already using the car 1199 00:58:33,200 --> 00:58:40,039 variable and then I'm going to go with a 1200 00:58:36,079 --> 00:58:42,960 different brand and also I'll change the 1201 00:58:40,039 --> 00:58:44,680 year and I don't think it's necessary 1202 00:58:42,960 --> 00:58:47,520 for me to repeat all of the steps 1203 00:58:44,680 --> 00:58:51,599 hopefully it's clear whatever we set up 1204 00:58:47,520 --> 00:58:54,920 in this type annotation needs to match 1205 00:58:51,599 --> 00:58:58,400 what we have in the object otherwise 1206 00:58:54,920 --> 00:59:00,960 typescript is not going to allow us to 1207 00:58:58,400 --> 00:59:03,480 compile essentially our code and now 1208 00:59:00,960 --> 00:59:07,400 let's tie two things together I want to 1209 00:59:03,480 --> 00:59:09,880 tie the arrays with the object and in 1210 00:59:07,400 --> 00:59:14,200 the process we'll also cover how we have 1211 00:59:09,880 --> 00:59:17,280 read only option with the typescript as 1212 00:59:14,200 --> 00:59:20,240 well as the optional properties so first 1213 00:59:17,280 --> 00:59:23,559 I want to create three objects I want to 1214 00:59:20,240 --> 00:59:27,559 create book pen and notebook and I want 1215 00:59:23,559 --> 00:59:29,799 to add them to the items array which by 1216 00:59:27,559 --> 00:59:32,200 default is going to be let's say empty 1217 00:59:29,799 --> 00:59:34,559 but we'll set up the types in a second 1218 00:59:32,200 --> 00:59:37,160 so first let's just go with items not 1219 00:59:34,559 --> 00:59:39,799 going to place anything inside of it and 1220 00:59:37,160 --> 00:59:44,280 then let's create those three variables 1221 00:59:39,799 --> 00:59:46,280 so in here we're looking for a book it's 1222 00:59:44,280 --> 00:59:49,920 going to have two things we're going to 1223 00:59:46,280 --> 00:59:53,760 go with title we'll set it equal to book 1224 00:59:49,920 --> 00:59:56,480 and we'll also have cost okay set it 1225 00:59:53,760 --> 00:59:58,599 equal to 20 and then I'll change this 1226 00:59:56,480 --> 01:00:00,400 one around to 1227 00:59:58,599 --> 01:00:04,200 pen 1228 01:00:00,400 --> 01:00:06,960 okay and the cost I mean again totally 1229 01:00:04,200 --> 01:00:09,400 relevant I'm going to set it equal to 10 1230 01:00:06,960 --> 01:00:12,599 I guess and then I want to go with 1231 01:00:09,400 --> 01:00:16,520 notebook but for Notebook actually want 1232 01:00:12,599 --> 01:00:19,319 to remove the cost and like I said now I 1233 01:00:16,520 --> 01:00:21,520 want to store all of these three values 1234 01:00:19,319 --> 01:00:24,000 over here in the items but here's the 1235 01:00:21,520 --> 01:00:28,520 kicker though I do want to make sure 1236 01:00:24,000 --> 01:00:30,079 that all of them have the same shape now 1237 01:00:28,520 --> 01:00:31,599 as we're looking at it we can clearly 1238 01:00:30,079 --> 01:00:35,119 see that it's not the case with the 1239 01:00:31,599 --> 01:00:37,960 notebook but let's imagine that we don't 1240 01:00:35,119 --> 01:00:41,240 know that and at this point we have two 1241 01:00:37,960 --> 01:00:45,160 options we can either repeat the same 1242 01:00:41,240 --> 01:00:47,440 steps like we did for the car so 1243 01:00:45,160 --> 01:00:51,119 effectively I can just set up this type 1244 01:00:47,440 --> 01:00:55,680 annotation for all three of them one by 1245 01:00:51,119 --> 01:00:58,240 one or since I have an array and I can 1246 01:00:55,680 --> 01:01:00,760 also set up the type for array I can do 1247 01:00:58,240 --> 01:01:02,960 it in one place where I have the items 1248 01:01:00,760 --> 01:01:06,000 so far we know that we can set up the 1249 01:01:02,960 --> 01:01:09,480 array to be equal to a string or a 1250 01:01:06,000 --> 01:01:12,400 number or a bullion correct and we also 1251 01:01:09,480 --> 01:01:15,280 cover the type Union but we can also do 1252 01:01:12,400 --> 01:01:18,359 something like this where I can say yeah 1253 01:01:15,280 --> 01:01:21,960 this is going to be an array of objects 1254 01:01:18,359 --> 01:01:23,960 and then each object needs to have this 1255 01:01:21,960 --> 01:01:27,359 particular shape now what is going to be 1256 01:01:23,960 --> 01:01:30,880 the shape I'll say title is equal to a 1257 01:01:27,359 --> 01:01:33,319 string or I'm sorry I don't know why I'm 1258 01:01:30,880 --> 01:01:36,400 placing this in the quotation marks and 1259 01:01:33,319 --> 01:01:38,119 then also I want to set up a cost so 1260 01:01:36,400 --> 01:01:41,200 we're going to go with cost and we'll 1261 01:01:38,119 --> 01:01:44,599 set it equal to a number and now one by 1262 01:01:41,200 --> 01:01:48,079 one as I'm adding these items to my 1263 01:01:44,599 --> 01:01:51,720 array once I get to a notebook check it 1264 01:01:48,079 --> 01:01:54,920 out now typescript is like Hey listen 1265 01:01:51,720 --> 01:01:58,760 this notebook that you're adding well 1266 01:01:54,920 --> 01:02:01,480 notice it only has the string it doesn't 1267 01:01:58,760 --> 01:02:04,839 have the cost so whatever type you set 1268 01:02:01,480 --> 01:02:08,720 up over here well it doesn't match so 1269 01:02:04,839 --> 01:02:10,920 again super useful where instead of 1270 01:02:08,720 --> 01:02:13,520 setting them one by one if we have an 1271 01:02:10,920 --> 01:02:16,599 array we can set it in one place because 1272 01:02:13,520 --> 01:02:19,000 we also can set up types for array now 1273 01:02:16,599 --> 01:02:22,000 what is the solution well technically we 1274 01:02:19,000 --> 01:02:25,200 can go back and add cost over here but 1275 01:02:22,000 --> 01:02:27,599 we also have this option of adding 1276 01:02:25,200 --> 01:02:30,039 optional properties now please keep in 1277 01:02:27,599 --> 01:02:33,039 mind you can use the same optional 1278 01:02:30,039 --> 01:02:36,279 property over here as well when you're 1279 01:02:33,039 --> 01:02:39,079 setting up the type annotation for an 1280 01:02:36,279 --> 01:02:41,240 object I'm just doing it here in Array 1281 01:02:39,079 --> 01:02:44,599 since this is where we're currently 1282 01:02:41,240 --> 01:02:47,920 working hopefully that is clear and now 1283 01:02:44,599 --> 01:02:50,079 I don't have the error because when I 1284 01:02:47,920 --> 01:02:52,880 set up the cost to be optional well it 1285 01:02:50,079 --> 01:02:56,720 could be there or it potentially could 1286 01:02:52,880 --> 01:02:59,319 be missing which is the case for a node 1287 01:02:56,720 --> 01:03:01,319 and what's also really interesting is 1288 01:02:59,319 --> 01:03:04,839 that with typescript we can set up the 1289 01:03:01,319 --> 01:03:06,559 properties to be read only again same 1290 01:03:04,839 --> 01:03:08,559 deal of course we can set it up over 1291 01:03:06,559 --> 01:03:12,319 here but in my case I'm going to do that 1292 01:03:08,559 --> 01:03:14,599 for the title where right before I set 1293 01:03:12,319 --> 01:03:18,799 up my property I'm going to go with read 1294 01:03:14,599 --> 01:03:21,440 only so now if I go with items and I 1295 01:03:18,799 --> 01:03:25,039 access let's say the first one here and 1296 01:03:21,440 --> 01:03:30,119 if I try to overwrite the value so dot 1297 01:03:25,039 --> 01:03:32,400 title is equal to a new book for example 1298 01:03:30,119 --> 01:03:33,880 it's not going to fly so now typescript 1299 01:03:32,400 --> 01:03:37,240 is immediately complaining that Hey 1300 01:03:33,880 --> 01:03:39,440 listen title is readon so you cannot 1301 01:03:37,240 --> 01:03:41,920 change this value over here so let me 1302 01:03:39,440 --> 01:03:45,240 comment this one out this concludes the 1303 01:03:41,920 --> 01:03:47,760 object fundamentals in typescript and 1304 01:03:45,240 --> 01:03:50,319 now we can start working on the next 1305 01:03:47,760 --> 01:03:53,039 challenge so now let's work on the 1306 01:03:50,319 --> 01:03:56,200 objects challenge where we want to set 1307 01:03:53,039 --> 01:03:58,000 up the bike we want to set up the laptop 1308 01:03:56,200 --> 01:04:00,760 then we'll create some products and 1309 01:03:58,000 --> 01:04:04,480 we're going to store it in the array so 1310 01:04:00,760 --> 01:04:08,559 let's start with our bike that will be 1311 01:04:04,480 --> 01:04:12,279 my object let me come up with some kind 1312 01:04:08,559 --> 01:04:13,640 of values over here then we also want to 1313 01:04:12,279 --> 01:04:17,400 add the 1314 01:04:13,640 --> 01:04:19,200 year and now let's set up our type 1315 01:04:17,400 --> 01:04:24,000 annotation so we're going to go with 1316 01:04:19,200 --> 01:04:26,480 colon then brand will be equal to string 1317 01:04:24,000 --> 01:04:29,640 and also we'll have the here and we'll 1318 01:04:26,480 --> 01:04:32,279 set it equal to a number that should do 1319 01:04:29,640 --> 01:04:35,960 it now we just want to change it to a 1320 01:04:32,279 --> 01:04:39,920 laptop so let me copy this 1321 01:04:35,960 --> 01:04:43,440 one change the name then brand stays 1322 01:04:39,920 --> 01:04:46,000 year stays we're just going to add a 1323 01:04:43,440 --> 01:04:50,359 different string value this case it's 1324 01:04:46,000 --> 01:04:52,799 going to be Dell and also change the 1325 01:04:50,359 --> 01:04:55,720 year and now let's see some errors so if 1326 01:04:52,799 --> 01:04:57,520 I'm going to go with bike and year and 1327 01:04:55,720 --> 01:05:00,720 set it equal to Old it's not going to 1328 01:04:57,520 --> 01:05:03,559 fly why well because year is set to be 1329 01:05:00,720 --> 01:05:05,920 equal to type number and same is going 1330 01:05:03,559 --> 01:05:09,839 to work if I'm going to create a second 1331 01:05:05,920 --> 01:05:13,039 laptop so I'm going to go with laptop 2 1332 01:05:09,839 --> 01:05:15,160 and it's only going to have a brand so 1333 01:05:13,039 --> 01:05:17,880 we're not going to have the year so if 1334 01:05:15,160 --> 01:05:21,480 I'm going to set it like this notice we 1335 01:05:17,880 --> 01:05:23,319 get our red squiggly which means that 1336 01:05:21,480 --> 01:05:26,279 there's some kind of error and now let's 1337 01:05:23,319 --> 01:05:29,720 set up those products so we're going to 1338 01:05:26,279 --> 01:05:32,400 go with product one that one is equal to 1339 01:05:29,720 --> 01:05:35,200 title let's go with 1340 01:05:32,400 --> 01:05:38,079 shirt and let's set up the 1341 01:05:35,200 --> 01:05:41,039 price that one will be equal to 1342 01:05:38,079 --> 01:05:44,160 820 then let's copy and paste we're 1343 01:05:41,039 --> 01:05:47,920 going to go with two this one will be 1344 01:05:44,160 --> 01:05:52,400 equal to a pants one why not pants are 1345 01:05:47,920 --> 01:05:54,200 awesome now we don't want the price so 1346 01:05:52,400 --> 01:05:57,240 we will need to set this one up as 1347 01:05:54,200 --> 01:06:00,440 optional and let's create the product 1348 01:05:57,240 --> 01:06:03,599 all right let's right away set up our 1349 01:06:00,440 --> 01:06:06,279 type by the way this was supposed to be 1350 01:06:03,599 --> 01:06:10,279 square brackets we want to go here with 1351 01:06:06,279 --> 01:06:13,319 title that one is equal to a string and 1352 01:06:10,279 --> 01:06:15,039 then also we want to go with price this 1353 01:06:13,319 --> 01:06:17,960 one will be optional and we'll set it 1354 01:06:15,039 --> 01:06:21,039 equal to a number so now in the array we 1355 01:06:17,960 --> 01:06:23,400 can nicely store product one and product 1356 01:06:21,039 --> 01:06:26,240 number two now it looks like typescript 1357 01:06:23,400 --> 01:06:28,559 is complaining why well because I forgot 1358 01:06:26,240 --> 01:06:29,960 the square brackets and at the very end 1359 01:06:28,559 --> 01:06:32,480 let me just showcase something 1360 01:06:29,960 --> 01:06:34,839 interesting where if I go with products 1361 01:06:32,480 --> 01:06:39,599 and then push 1362 01:06:34,839 --> 01:06:42,559 provide the object set up a title here 1363 01:06:39,599 --> 01:06:45,240 in my case I'm going to go with shes 1364 01:06:42,559 --> 01:06:48,079 even though price is optional we still 1365 01:06:45,240 --> 01:06:51,920 need to provide the correct type so 1366 01:06:48,079 --> 01:06:54,279 let's say if I don't provide anything 1367 01:06:51,920 --> 01:06:56,200 notice how typescript is not complaining 1368 01:06:54,279 --> 01:06:59,799 effectively this is optional 1369 01:06:56,200 --> 01:07:03,440 the price one now if I do provide the 1370 01:06:59,799 --> 01:07:06,760 price it has to be a number so if I go 1371 01:07:03,440 --> 01:07:10,400 here with price and set it equal to a 1372 01:07:06,760 --> 01:07:15,279 string T will immediately complain yes 1373 01:07:10,400 --> 01:07:17,720 it's optional but if I do decide to pass 1374 01:07:15,279 --> 01:07:20,559 the value the type needs to match 1375 01:07:17,720 --> 01:07:23,480 whatever I have here in the type 1376 01:07:20,559 --> 01:07:26,200 annotation beautiful we have finally 1377 01:07:23,480 --> 01:07:28,359 arrived at the functions in typescript 1378 01:07:26,200 --> 01:07:31,680 and I'm super excited to cover this 1379 01:07:28,359 --> 01:07:34,079 topic for a few reasons first our 1380 01:07:31,680 --> 01:07:36,079 examples are going to get more complex 1381 01:07:34,079 --> 01:07:39,000 because we'll start tying together all 1382 01:07:36,079 --> 01:07:41,319 of the concepts we covered previously 1383 01:07:39,000 --> 01:07:44,240 and also once we start working with 1384 01:07:41,319 --> 01:07:46,319 functions we absolutely have to start 1385 01:07:44,240 --> 01:07:49,480 using typescript and you'll see what I 1386 01:07:46,319 --> 01:07:52,839 mean in a second so let's say I have a 1387 01:07:49,480 --> 01:07:55,640 function of say hi why not let's set it 1388 01:07:52,839 --> 01:07:58,039 up say hi for now we're not going to 1389 01:07:55,640 --> 01:08:00,640 pass any parameters and as far as the 1390 01:07:58,039 --> 01:08:04,200 function logic we'll just leave it empty 1391 01:08:00,640 --> 01:08:06,680 now say high is looking for what well 1392 01:08:04,200 --> 01:08:09,799 probably some kind of name correct and 1393 01:08:06,680 --> 01:08:12,319 in a function body we can nicely log it 1394 01:08:09,799 --> 01:08:14,880 so the moment I type over here any 1395 01:08:12,319 --> 01:08:18,440 parameter in my case name notice right 1396 01:08:14,880 --> 01:08:20,400 away I'm getting these red squidly lines 1397 01:08:18,440 --> 01:08:23,440 and this is what I'm talking about at 1398 01:08:20,400 --> 01:08:26,359 this point we absolutely have to start 1399 01:08:23,440 --> 01:08:29,359 using typescript otherwise typescript 1400 01:08:26,359 --> 01:08:33,199 won't compile our code so if we hover 1401 01:08:29,359 --> 01:08:36,440 over we notice parameter name implicitly 1402 01:08:33,199 --> 01:08:39,120 has any type and essentially typescript 1403 01:08:36,440 --> 01:08:41,960 complains about it so that's not allowed 1404 01:08:39,120 --> 01:08:45,600 yes by default implicitly it's a set up 1405 01:08:41,960 --> 01:08:48,880 as any but we need to fix it in order 1406 01:08:45,600 --> 01:08:51,719 for typescript to compile our code and 1407 01:08:48,880 --> 01:08:55,040 at this point essentially we have three 1408 01:08:51,719 --> 01:08:57,199 options first we could set typ any 1409 01:08:55,040 --> 01:09:00,799 ourselves elves that's definitely one 1410 01:08:57,199 --> 01:09:03,679 option also and you know what let me 1411 01:09:00,799 --> 01:09:06,920 just comment this one out also we can 1412 01:09:03,679 --> 01:09:09,199 navigate to DS config and we can 1413 01:09:06,920 --> 01:09:11,239 actually change the configuration 1414 01:09:09,199 --> 01:09:16,080 something I'm going to Showcase in a 1415 01:09:11,239 --> 01:09:19,679 second or the best scenario is for us to 1416 01:09:16,080 --> 01:09:22,480 actually set this one up with a proper 1417 01:09:19,679 --> 01:09:25,359 type annotation so in this case I'm 1418 01:09:22,480 --> 01:09:29,400 looking for a string most likely so I'm 1419 01:09:25,359 --> 01:09:32,239 I'm going to say type okay so let's try 1420 01:09:29,400 --> 01:09:35,040 it out here with any what's the issue 1421 01:09:32,239 --> 01:09:38,239 here well just like we covered before 1422 01:09:35,040 --> 01:09:39,839 any is anything so everything that we 1423 01:09:38,239 --> 01:09:43,560 can do in JavaScript pretty much will be 1424 01:09:39,839 --> 01:09:47,000 able to do with the name here and 1425 01:09:43,560 --> 01:09:49,359 actually I believe in two videos I'll 1426 01:09:47,000 --> 01:09:52,040 showcase the any GOA that we need to be 1427 01:09:49,359 --> 01:09:54,120 aware of so this is not going to be 1428 01:09:52,040 --> 01:09:56,159 approach yes technically you can do it 1429 01:09:54,120 --> 01:09:59,159 but I mean at at this point we kind of 1430 01:09:56,159 --> 01:10:01,239 lose the benefits of typescript now 1431 01:09:59,159 --> 01:10:04,440 second option we can navigate over here 1432 01:10:01,239 --> 01:10:06,480 to the package Json or I'm sorry why am 1433 01:10:04,440 --> 01:10:10,080 I going to package Json we're going to 1434 01:10:06,480 --> 01:10:12,880 DS config and notice over here this 1435 01:10:10,080 --> 01:10:15,679 strict rule technically we can change it 1436 01:10:12,880 --> 01:10:17,880 around I can set it equal to false and 1437 01:10:15,679 --> 01:10:20,800 at that point typescript won't complain 1438 01:10:17,880 --> 01:10:23,800 about the fact that parameter by default 1439 01:10:20,800 --> 01:10:25,600 is left as any so you can definitely try 1440 01:10:23,800 --> 01:10:27,719 it out you can set it equal TOA and 1441 01:10:25,600 --> 01:10:29,560 you'll see that actually code Works 1442 01:10:27,719 --> 01:10:30,840 everything's fine but in my case I'm 1443 01:10:29,560 --> 01:10:33,400 going to skip that I'm just letting you 1444 01:10:30,840 --> 01:10:36,280 know that technically this is an option 1445 01:10:33,400 --> 01:10:39,239 so we have finally arrived at the option 1446 01:10:36,280 --> 01:10:42,679 we're going to use and that is setting 1447 01:10:39,239 --> 01:10:45,560 name explicitly to the type we're going 1448 01:10:42,679 --> 01:10:47,840 to use and just like before we simply 1449 01:10:45,560 --> 01:10:49,800 want to go with colon and we want to 1450 01:10:47,840 --> 01:10:52,080 come up with the type my case that is 1451 01:10:49,800 --> 01:10:54,440 going to be string and again I know 1452 01:10:52,080 --> 01:10:56,440 you're probably sick of this method but 1453 01:10:54,440 --> 01:10:59,199 I'm just going to use two uppercase 1454 01:10:56,440 --> 01:11:01,800 essentially I want to set up over here a 1455 01:10:59,199 --> 01:11:04,159 log then I'm going to go with template 1456 01:11:01,800 --> 01:11:07,840 string and we're going to go with hello 1457 01:11:04,159 --> 01:11:10,520 there over here and now I'm going to 1458 01:11:07,840 --> 01:11:14,840 access the name so the moment here we 1459 01:11:10,520 --> 01:11:17,760 set up a proper type let's say string 1460 01:11:14,840 --> 01:11:20,600 number or whatever two things are going 1461 01:11:17,760 --> 01:11:23,640 to happen first when we call this 1462 01:11:20,600 --> 01:11:26,360 function every time we'll pass in a 1463 01:11:23,640 --> 01:11:29,320 wrong type typescript is going to yell 1464 01:11:26,360 --> 01:11:32,480 at us and second within this function 1465 01:11:29,320 --> 01:11:36,280 body we can only perform functionality 1466 01:11:32,480 --> 01:11:40,120 that is allowed on this specific type so 1467 01:11:36,280 --> 01:11:45,000 in here yes let's go to uppercase let's 1468 01:11:40,120 --> 01:11:48,400 invoke it and let's try invoking say hi 1469 01:11:45,000 --> 01:11:50,880 and let's say I'm going to pass in John 1470 01:11:48,400 --> 01:11:53,280 and also in the next line I'm going to 1471 01:11:50,880 --> 01:11:56,560 pass in three so if everything is 1472 01:11:53,280 --> 01:11:59,080 correct we should get an error over here 1473 01:11:56,560 --> 01:12:00,719 we can nicely see that we can only grab 1474 01:11:59,080 --> 01:12:03,120 the string Methods inside of the 1475 01:12:00,719 --> 01:12:06,320 function body and if we inspect the 1476 01:12:03,120 --> 01:12:08,239 console we should see John over there 1477 01:12:06,320 --> 01:12:10,960 and lastly if we try to pass more 1478 01:12:08,239 --> 01:12:12,960 parameters than what we have specified 1479 01:12:10,960 --> 01:12:15,760 we'll also get an error and typescript 1480 01:12:12,960 --> 01:12:17,560 won't compile our code and with this in 1481 01:12:15,760 --> 01:12:20,040 place now we can move on to the next 1482 01:12:17,560 --> 01:12:22,560 topic okay and up next let's talk about 1483 01:12:20,040 --> 01:12:24,920 function returns so first I'm going to 1484 01:12:22,560 --> 01:12:26,960 create a function and I'm going to call 1485 01:12:24,920 --> 01:12:28,960 this 1486 01:12:26,960 --> 01:12:31,480 calculate 1487 01:12:28,960 --> 01:12:34,480 discount discount over here it's going 1488 01:12:31,480 --> 01:12:37,239 to be looking for price and I know that 1489 01:12:34,480 --> 01:12:39,040 that type is going to be number for now 1490 01:12:37,239 --> 01:12:41,199 inside of the function body I simply 1491 01:12:39,040 --> 01:12:43,600 want to go with the return and we're 1492 01:12:41,199 --> 01:12:46,400 going to go with price again since this 1493 01:12:43,600 --> 01:12:48,679 is a number once I press do notice I'm 1494 01:12:46,400 --> 01:12:51,320 only getting the methods that are 1495 01:12:48,679 --> 01:12:54,040 associated with the type number and if 1496 01:12:51,320 --> 01:12:56,000 you're sick of me hearing this I'm sorry 1497 01:12:54,040 --> 01:12:59,679 but I'm going to keep on repeating that 1498 01:12:56,000 --> 01:13:02,080 because again I find this truly helpful 1499 01:12:59,679 --> 01:13:03,199 and let's say I'm going to multiply this 1500 01:13:02,080 --> 01:13:06,239 by 1501 01:13:03,199 --> 01:13:08,960 0.9 so I'm going to go over here 0.9 1502 01:13:06,239 --> 01:13:11,760 okay good and then I'm going to set up a 1503 01:13:08,960 --> 01:13:15,480 variable so I'm going to go with cons 1504 01:13:11,760 --> 01:13:19,920 final price and I'm going to set it 1505 01:13:15,480 --> 01:13:23,840 equal to calculate discount I'm going to 1506 01:13:19,920 --> 01:13:25,639 pass in 200 and I don't think I need to 1507 01:13:23,840 --> 01:13:28,840 log the value I I don't think it's 1508 01:13:25,639 --> 01:13:31,360 necessary so if I hover over the final 1509 01:13:28,840 --> 01:13:34,400 price I can see that it's a number now 1510 01:13:31,360 --> 01:13:37,760 why is that a case because again 1511 01:13:34,400 --> 01:13:40,480 typescript is inferring the type 1512 01:13:37,760 --> 01:13:44,760 typescript says okay so this is a number 1513 01:13:40,480 --> 01:13:48,000 here we're returning a number multiplied 1514 01:13:44,760 --> 01:13:50,840 by something so whenever we hover over 1515 01:13:48,000 --> 01:13:55,239 this calculate discount we can nicely 1516 01:13:50,840 --> 01:13:58,639 see another set of colon and a number 1517 01:13:55,239 --> 01:14:01,560 over there and I know that it might look 1518 01:13:58,639 --> 01:14:04,080 a little funky over here but yes 1519 01:14:01,560 --> 01:14:07,400 essentially the syntax is following in 1520 01:14:04,080 --> 01:14:09,880 order for us to set up the type for the 1521 01:14:07,400 --> 01:14:12,440 function return we need to go after 1522 01:14:09,880 --> 01:14:15,239 parenthesis we need to set up the colon 1523 01:14:12,440 --> 01:14:18,000 and then we explicitly need to go with 1524 01:14:15,239 --> 01:14:20,679 the type now again you can make a case 1525 01:14:18,000 --> 01:14:24,000 well why do we need to set up type if 1526 01:14:20,679 --> 01:14:26,440 typescript is already inferring the type 1527 01:14:24,000 --> 01:14:31,040 well for essentially two reasons first 1528 01:14:26,440 --> 01:14:34,719 of all if I set up over here a number 1529 01:14:31,040 --> 01:14:39,199 type explicitly as I'm working in this 1530 01:14:34,719 --> 01:14:43,560 function if for some reason I forget to 1531 01:14:39,199 --> 01:14:46,320 set up the return or if I set up a 1532 01:14:43,560 --> 01:14:49,679 return which doesn't match what 1533 01:14:46,320 --> 01:14:52,520 typescript is expecting well I'm going 1534 01:14:49,679 --> 01:14:56,040 to get an error so it's going to be 1535 01:14:52,520 --> 01:15:00,560 super helpful let's Imagine This I have 1536 01:14:56,040 --> 01:15:04,199 some kind of has discount variable over 1537 01:15:00,560 --> 01:15:07,520 here and I'm going to just set up has 1538 01:15:04,199 --> 01:15:10,080 discount so essentially some kind of 1539 01:15:07,520 --> 01:15:13,600 condition and in here instead of 1540 01:15:10,080 --> 01:15:17,719 returning a number I'm going to go with 1541 01:15:13,600 --> 01:15:20,320 discount and applied notice right away 1542 01:15:17,719 --> 01:15:23,440 typescript is complaining type string is 1543 01:15:20,320 --> 01:15:25,840 not assignable to type number as we're 1544 01:15:23,440 --> 01:15:29,239 working in the function typescript is 1545 01:15:25,840 --> 01:15:31,920 kind of watching over us and if I return 1546 01:15:29,239 --> 01:15:33,360 something that I'm not expecting well 1547 01:15:31,920 --> 01:15:36,480 essentially typescript just starts 1548 01:15:33,360 --> 01:15:39,800 yelling at me because again yeah it's 1549 01:15:36,480 --> 01:15:43,800 trying to infer the type but once I 1550 01:15:39,800 --> 01:15:47,840 remove my logic well essentially now it 1551 01:15:43,800 --> 01:15:51,120 could be a string or it could be also a 1552 01:15:47,840 --> 01:15:54,560 number correct so now if we hover over 1553 01:15:51,120 --> 01:15:56,760 this notice we either have a number or 1554 01:15:54,560 --> 01:15:59,440 it's going to be discount applied and 1555 01:15:56,760 --> 01:16:03,239 the same goes over here either it 1556 01:15:59,440 --> 01:16:05,360 returns a number or discount applied so 1557 01:16:03,239 --> 01:16:08,159 even though it's technically optional 1558 01:16:05,360 --> 01:16:10,800 it's a good practice to set up the 1559 01:16:08,159 --> 01:16:13,480 return types explicitly since it helps 1560 01:16:10,800 --> 01:16:15,679 us tremendously as we're setting up the 1561 01:16:13,480 --> 01:16:17,679 functionality so now let me common this 1562 01:16:15,679 --> 01:16:20,199 one out and I'm just going to go with 1563 01:16:17,679 --> 01:16:22,719 price like so and with this in place now 1564 01:16:20,199 --> 01:16:25,199 we can move on to the next topic and 1565 01:16:22,719 --> 01:16:28,639 next I just want to show you why we want 1566 01:16:25,199 --> 01:16:30,199 to be careful with type any but this is 1567 01:16:28,639 --> 01:16:33,080 one of those videos where you don't have 1568 01:16:30,199 --> 01:16:35,719 to type along if you're not interested 1569 01:16:33,080 --> 01:16:40,600 essentially you can just sit back and 1570 01:16:35,719 --> 01:16:44,800 relax so let's imagine I have a function 1571 01:16:40,600 --> 01:16:46,920 and the name is add three so it's 1572 01:16:44,800 --> 01:16:49,760 looking for one parameter which is a 1573 01:16:46,920 --> 01:16:52,679 number and don't ask me why but I'm just 1574 01:16:49,760 --> 01:16:55,080 going to set it equal to any so I'm not 1575 01:16:52,679 --> 01:16:58,080 sure what would be the use case but 1576 01:16:55,080 --> 01:17:00,880 let's just go with it then inside of the 1577 01:16:58,080 --> 01:17:03,760 function body I'm going to create 1578 01:17:00,880 --> 01:17:06,400 another number in this case type will be 1579 01:17:03,760 --> 01:17:09,520 a number so I'm going to be explicit and 1580 01:17:06,400 --> 01:17:14,000 then as far as the return let's just go 1581 01:17:09,520 --> 01:17:16,639 with a return number plus another number 1582 01:17:14,000 --> 01:17:19,360 why not and then let's set up two 1583 01:17:16,639 --> 01:17:22,080 variables one is going to be result and 1584 01:17:19,360 --> 01:17:23,800 then we'll also set up some value so 1585 01:17:22,080 --> 01:17:26,760 let's try it out I'm going to go over 1586 01:17:23,800 --> 01:17:30,000 here with result that one is equal to 1587 01:17:26,760 --> 01:17:33,199 add three so as far as the value you 1588 01:17:30,000 --> 01:17:37,159 know what let me just pass in three so 3 1589 01:17:33,199 --> 01:17:40,040 + 3 and then also we want to set up some 1590 01:17:37,159 --> 01:17:44,400 value so that one will be equal to a 1591 01:17:40,040 --> 01:17:46,320 result so now M Dar question and now I'm 1592 01:17:44,400 --> 01:17:48,639 not asking what is going to be the 1593 01:17:46,320 --> 01:17:51,440 result I'm more interested well what is 1594 01:17:48,639 --> 01:17:53,840 going to be the type so if we have our 1595 01:17:51,440 --> 01:17:55,480 over some value notice something 1596 01:17:53,840 --> 01:17:59,800 interesting 1597 01:17:55,480 --> 01:18:04,159 how essentially it's any and what that 1598 01:17:59,800 --> 01:18:06,480 means well go with some value and my 1599 01:18:04,159 --> 01:18:08,679 method and technically it's going to be 1600 01:18:06,480 --> 01:18:12,040 valid as I'm typing the code 1601 01:18:08,679 --> 01:18:14,960 but hopefully it's clear that it will 1602 01:18:12,040 --> 01:18:18,920 lead to some errors since there is no 1603 01:18:14,960 --> 01:18:22,159 method My Method and as you can see 1604 01:18:18,920 --> 01:18:24,880 everything starts with this innocent any 1605 01:18:22,159 --> 01:18:28,120 and notice how it trickles down so we 1606 01:18:24,880 --> 01:18:30,320 pass number that is type any and even 1607 01:18:28,120 --> 01:18:32,480 though this is a number and technically 1608 01:18:30,320 --> 01:18:35,400 this is a math operation typescript 1609 01:18:32,480 --> 01:18:38,120 should infer that we're returning a 1610 01:18:35,400 --> 01:18:41,000 number however if we hover over notice 1611 01:18:38,120 --> 01:18:44,400 over here we're expecting any we are 1612 01:18:41,000 --> 01:18:47,120 returning any so result is any then Su 1613 01:18:44,400 --> 01:18:49,639 value is any and as I mentioned before 1614 01:18:47,120 --> 01:18:53,120 be careful when you introduce any 1615 01:18:49,639 --> 01:18:55,760 because it can slowly trickle down or 1616 01:18:53,120 --> 01:18:59,440 spread like wildfire whichever term you 1617 01:18:55,760 --> 01:19:01,920 want to use and next thing you know it 1618 01:18:59,440 --> 01:19:04,280 you're going to be unintentionally or 1619 01:19:01,920 --> 01:19:05,880 intentionally running some methods or 1620 01:19:04,280 --> 01:19:08,560 setting up some kind of functionality 1621 01:19:05,880 --> 01:19:11,960 which doesn't make sense and at that 1622 01:19:08,560 --> 01:19:15,000 point you'll again need to start chasing 1623 01:19:11,960 --> 01:19:17,239 those bugs at runtime not as you're 1624 01:19:15,000 --> 01:19:19,760 writing the application so now if I 1625 01:19:17,239 --> 01:19:22,600 navigate to the browser notice over here 1626 01:19:19,760 --> 01:19:26,120 some value that my method is not a 1627 01:19:22,600 --> 01:19:29,560 function an error which which we can 1628 01:19:26,120 --> 01:19:32,040 expect since there is no such method on 1629 01:19:29,560 --> 01:19:35,199 some value and with this in place now we 1630 01:19:32,040 --> 01:19:37,600 can work on our next challenge beautiful 1631 01:19:35,199 --> 01:19:39,679 and once we're familiar with the 1632 01:19:37,600 --> 01:19:41,239 fundamentals let's work on our next 1633 01:19:39,679 --> 01:19:43,600 challenge and you know what I actually 1634 01:19:41,239 --> 01:19:46,719 changed my mind I will list out the 1635 01:19:43,600 --> 01:19:50,080 steps for a few reasons first the 1636 01:19:46,719 --> 01:19:53,320 challenges are getting more complex and 1637 01:19:50,080 --> 01:19:55,159 also I want everyone to be on the same 1638 01:19:53,320 --> 01:19:57,600 page let's say someone one is not 1639 01:19:55,159 --> 01:20:01,440 reading the readme or utilizing the 1640 01:19:57,600 --> 01:20:04,960 readme I still want them to kind of have 1641 01:20:01,440 --> 01:20:08,080 an idea what I'm trying to build so 1642 01:20:04,960 --> 01:20:10,760 effectively I want to create the array 1643 01:20:08,080 --> 01:20:15,080 names and then I want to write a 1644 01:20:10,760 --> 01:20:17,960 function that checks the name whether 1645 01:20:15,080 --> 01:20:20,719 it's in the array so this function 1646 01:20:17,960 --> 01:20:24,239 should take name as parameter and it 1647 01:20:20,719 --> 01:20:27,120 should return a Boolean whether the name 1648 01:20:24,239 --> 01:20:31,679 is in the array or not and then we want 1649 01:20:27,120 --> 01:20:34,239 to set up one or few checks and 1650 01:20:31,679 --> 01:20:36,719 essentially just test out some names so 1651 01:20:34,239 --> 01:20:40,400 let me go to my file first I want to 1652 01:20:36,719 --> 01:20:42,480 come up with a value and I'm going to 1653 01:20:40,400 --> 01:20:45,440 say that it's going to be an array of 1654 01:20:42,480 --> 01:20:47,120 strings and let's just provide some 1655 01:20:45,440 --> 01:20:50,520 random values so I'm going to go here 1656 01:20:47,120 --> 01:20:55,199 with John then I also want to go with 1657 01:20:50,520 --> 01:20:57,920 Jane let's set up Chim and also will 1658 01:20:55,199 --> 01:21:01,360 place a gill over there I don't know why 1659 01:20:57,920 --> 01:21:03,159 I said all of them starting with J must 1660 01:21:01,360 --> 01:21:05,320 have been a bad day in the office or 1661 01:21:03,159 --> 01:21:08,239 something like that then let's come up 1662 01:21:05,320 --> 01:21:12,639 with our function so in my case I'm 1663 01:21:08,239 --> 01:21:14,080 going to go with is name in list and 1664 01:21:12,639 --> 01:21:16,840 this function is going to be looking for 1665 01:21:14,080 --> 01:21:20,719 a parameter the type is going to be 1666 01:21:16,840 --> 01:21:23,280 string and I'll right away set up that I 1667 01:21:20,719 --> 01:21:25,120 want to return from this function a 1668 01:21:23,280 --> 01:21:28,080 Boolean so what 1669 01:21:25,120 --> 01:21:29,760 the name is in the array or not 1670 01:21:28,080 --> 01:21:33,719 therefore I'm going to set up my type 1671 01:21:29,760 --> 01:21:36,440 over here and then right away let's set 1672 01:21:33,719 --> 01:21:38,400 up the return I'm going to go with names 1673 01:21:36,440 --> 01:21:41,360 so that's the array and I'm just going 1674 01:21:38,400 --> 01:21:44,320 to use the includes method and in here 1675 01:21:41,360 --> 01:21:46,360 I'll pass in the name so I pass the 1676 01:21:44,320 --> 01:21:49,679 string over here and then I pass the 1677 01:21:46,360 --> 01:21:52,960 same one into includes and essentially 1678 01:21:49,679 --> 01:21:56,960 this is just going to return true or 1679 01:21:52,960 --> 01:22:00,320 false then let me set up a variable name 1680 01:21:56,960 --> 01:22:03,639 to check now technically I mean we don't 1681 01:22:00,320 --> 01:22:05,840 have to do it we can just pass it in but 1682 01:22:03,639 --> 01:22:09,199 you know what just to make things more 1683 01:22:05,840 --> 01:22:11,480 interesting I'm going to go with name to 1684 01:22:09,199 --> 01:22:13,719 check um in this case you know what I 1685 01:22:11,480 --> 01:22:17,199 will rely on the fact 1686 01:22:13,719 --> 01:22:20,639 that typescript will infer this type let 1687 01:22:17,199 --> 01:22:23,600 me type over here Jane and let me set up 1688 01:22:20,639 --> 01:22:26,520 if and else so I'm going to go with his 1689 01:22:23,600 --> 01:22:29,320 name in a list and I'm going to pass in 1690 01:22:26,520 --> 01:22:33,360 name to check that's the case I'll have 1691 01:22:29,320 --> 01:22:36,080 one log if not I'll have something else 1692 01:22:33,360 --> 01:22:40,760 so now let's set up those logs so in 1693 01:22:36,080 --> 01:22:43,639 here we're looking for a name to check 1694 01:22:40,760 --> 01:22:45,800 whatever it might be so now let me 1695 01:22:43,639 --> 01:22:50,280 access this as a variable so name to 1696 01:22:45,800 --> 01:22:51,880 check and then let's say in the list and 1697 01:22:50,280 --> 01:22:53,239 now pretty much let me just take the 1698 01:22:51,880 --> 01:22:56,920 same console 1699 01:22:53,239 --> 01:22:59,840 log and and add not in 1700 01:22:56,920 --> 01:23:03,159 list okay good and now let me save the 1701 01:22:59,840 --> 01:23:05,719 file let me navigate to the browser and 1702 01:23:03,159 --> 01:23:08,440 if everything is correct Jane is in the 1703 01:23:05,719 --> 01:23:11,360 list so if I'm going to set it up over 1704 01:23:08,440 --> 01:23:14,760 here a value of Peter now it's going to 1705 01:23:11,360 --> 01:23:15,880 say Peter is not on the list and with 1706 01:23:14,760 --> 01:23:18,199 this in place we're done with the 1707 01:23:15,880 --> 01:23:21,639 challenge and we're ready to move on to 1708 01:23:18,199 --> 01:23:25,000 our next topic and next let's discuss 1709 01:23:21,639 --> 01:23:26,560 optional and default parameters in 1710 01:23:25,000 --> 01:23:30,040 functions 1711 01:23:26,560 --> 01:23:31,560 essentially how we set them up from the 1712 01:23:30,040 --> 01:23:33,560 typescript 1713 01:23:31,560 --> 01:23:36,400 perspective I'm going to start by 1714 01:23:33,560 --> 01:23:38,239 creating a function 1715 01:23:36,400 --> 01:23:41,560 calculate 1716 01:23:38,239 --> 01:23:44,639 price it's going to look for two 1717 01:23:41,560 --> 01:23:48,239 variables price which is going to be 1718 01:23:44,639 --> 01:23:52,120 number and for now I'll set 1719 01:23:48,239 --> 01:23:54,800 discount as a normal parameter so I'm 1720 01:23:52,120 --> 01:23:58,040 not going to set it as default or 1721 01:23:54,800 --> 01:24:00,800 optional so let me go with discount let 1722 01:23:58,040 --> 01:24:03,719 me set it up as number then inside of 1723 01:24:00,800 --> 01:24:06,840 the function body I'm going to go with 1724 01:24:03,719 --> 01:24:11,040 return and we're going to look for Price 1725 01:24:06,840 --> 01:24:13,480 minus the discount okay good makes sense 1726 01:24:11,040 --> 01:24:16,280 I mean if we want we can set up over 1727 01:24:13,480 --> 01:24:18,760 here a number as a return as well that's 1728 01:24:16,280 --> 01:24:21,639 definitely an option and then let's go 1729 01:24:18,760 --> 01:24:21,639 with price after 1730 01:24:22,920 --> 01:24:27,480 discount and now will be equal to 1731 01:24:25,480 --> 01:24:29,440 calculate price and I'm going to pass in 1732 01:24:27,480 --> 01:24:32,760 some even numbers just so we don't need 1733 01:24:29,440 --> 01:24:36,040 to log it so essentially I pass in 1734 01:24:32,760 --> 01:24:39,000 100 as a price and20 as a discount and 1735 01:24:36,040 --> 01:24:41,480 then the result should be 80 all right 1736 01:24:39,000 --> 01:24:43,840 but what if I want to make this one 1737 01:24:41,480 --> 01:24:46,320 optional what if sometimes I'm going to 1738 01:24:43,840 --> 01:24:48,520 provide the value and sometimes well 1739 01:24:46,320 --> 01:24:50,440 it's not going to be available well the 1740 01:24:48,520 --> 01:24:52,480 syex is following we want to go to the 1741 01:24:50,440 --> 01:24:55,000 parameter and we need to add the 1742 01:24:52,480 --> 01:24:57,080 question mark but notice right away 1743 01:24:55,000 --> 01:24:59,320 typescript is 1744 01:24:57,080 --> 01:25:02,440 complaining and the reason for that is 1745 01:24:59,320 --> 01:25:04,520 because possibly discount can be 1746 01:25:02,440 --> 01:25:07,159 undefined and in our case we already 1747 01:25:04,520 --> 01:25:09,600 have a math operation where we take the 1748 01:25:07,159 --> 01:25:12,040 price and then we subtract a discount 1749 01:25:09,600 --> 01:25:15,360 but if this is undefined I mean it 1750 01:25:12,040 --> 01:25:18,679 doesn't make sense so the key thing to 1751 01:25:15,360 --> 01:25:21,119 remember about optional parameters is 1752 01:25:18,679 --> 01:25:25,040 that a function with optional parameters 1753 01:25:21,119 --> 01:25:26,520 must work when they're not supplied and 1754 01:25:25,040 --> 01:25:30,760 as far as the effects well we just need 1755 01:25:26,520 --> 01:25:33,560 to provide the fallback value so if this 1756 01:25:30,760 --> 01:25:36,119 is undefined the discount one well then 1757 01:25:33,560 --> 01:25:39,080 we'll have some kind of number value and 1758 01:25:36,119 --> 01:25:41,119 the way we do that we just set up the or 1759 01:25:39,080 --> 01:25:44,719 operator and in this case I'm going to 1760 01:25:41,119 --> 01:25:48,199 go with zero that's how we can work with 1761 01:25:44,719 --> 01:25:50,639 optional parameters in typescript and 1762 01:25:48,199 --> 01:25:54,040 while we're still on a topic let's also 1763 01:25:50,639 --> 01:25:56,000 discuss the default parameter now for 1764 01:25:54,040 --> 01:25:58,280 this one I think I'm going to create 1765 01:25:56,000 --> 01:26:00,760 another function why not let's just keep 1766 01:25:58,280 --> 01:26:03,440 on practicing this one is not going to 1767 01:26:00,760 --> 01:26:06,480 be calculate the price though we're 1768 01:26:03,440 --> 01:26:10,080 going to go with calculate score we're 1769 01:26:06,480 --> 01:26:13,800 going to pass in the initial score which 1770 01:26:10,080 --> 01:26:17,639 will be a number and also we're going to 1771 01:26:13,800 --> 01:26:20,040 go with penalty points and same deal for 1772 01:26:17,639 --> 01:26:22,320 now we're just going to set it that it's 1773 01:26:20,040 --> 01:26:25,320 actually a number I mean it will be a 1774 01:26:22,320 --> 01:26:27,880 number but we'll just set it as a 1775 01:26:25,320 --> 01:26:30,760 default eventually now as far as the 1776 01:26:27,880 --> 01:26:34,119 return again same deal we're going to 1777 01:26:30,760 --> 01:26:36,679 set it equal to a number and when it 1778 01:26:34,119 --> 01:26:40,199 comes to logic we want to go with return 1779 01:26:36,679 --> 01:26:41,679 initial score minus the penalty points 1780 01:26:40,199 --> 01:26:44,360 and once I have the function in place 1781 01:26:41,679 --> 01:26:46,080 now let me set up two variables so we're 1782 01:26:44,360 --> 01:26:50,679 looking for 1783 01:26:46,080 --> 01:26:53,760 score after penalty and I'll provide 1784 01:26:50,679 --> 01:26:55,119 over here calculate score and again I'm 1785 01:26:53,760 --> 01:26:58,600 going to go with with some even values 1786 01:26:55,119 --> 01:27:01,360 so I'm going to go with 12 and now I 1787 01:26:58,600 --> 01:27:05,239 want to set up another variable only in 1788 01:27:01,360 --> 01:27:08,080 this case I'm going to go with without 1789 01:27:05,239 --> 01:27:11,600 penalty and I'll just provide the first 1790 01:27:08,080 --> 01:27:14,280 one so that will be my initial score and 1791 01:27:11,600 --> 01:27:18,840 notice right away typescript is going to 1792 01:27:14,280 --> 01:27:22,000 complain so here's the deal if we set up 1793 01:27:18,840 --> 01:27:24,679 a parameter and we don't provide it or 1794 01:27:22,000 --> 01:27:26,880 we provide one too many 1795 01:27:24,679 --> 01:27:28,719 well then typescript will complain so 1796 01:27:26,880 --> 01:27:31,159 this is something to keep in mind and 1797 01:27:28,719 --> 01:27:35,239 how we can fix it well we can either go 1798 01:27:31,159 --> 01:27:38,040 back to this optional parameter which we 1799 01:27:35,239 --> 01:27:40,480 discussed in the previous video or we 1800 01:27:38,040 --> 01:27:43,920 provide some default for this one so we 1801 01:27:40,480 --> 01:27:46,040 make penalty points as a default 1802 01:27:43,920 --> 01:27:48,159 parameter and the way we do that just 1803 01:27:46,040 --> 01:27:50,239 like with JavaScript we just add the 1804 01:27:48,159 --> 01:27:53,040 equal sign over here and come up with 1805 01:27:50,239 --> 01:27:54,800 the value so in my case it's going to be 1806 01:27:53,040 --> 01:27:58,080 zero so now 1807 01:27:54,800 --> 01:28:02,600 if it's provided awesome we're going to 1808 01:27:58,080 --> 01:28:05,719 use the penalty points value if not well 1809 01:28:02,600 --> 01:28:08,400 then we'll just revert back to the zero 1810 01:28:05,719 --> 01:28:10,719 because that one is the default one and 1811 01:28:08,400 --> 01:28:13,199 here's the key difference if we use 1812 01:28:10,719 --> 01:28:15,520 optional parameter we do need to set up 1813 01:28:13,199 --> 01:28:17,199 this logic if we have a default one it's 1814 01:28:15,520 --> 01:28:20,520 not the case then we'll just use 1815 01:28:17,199 --> 01:28:22,920 whatever value is provided okay and up 1816 01:28:20,520 --> 01:28:24,840 next let's discuss how we can work with 1817 01:28:22,920 --> 01:28:27,400 rest parameters 1818 01:28:24,840 --> 01:28:30,199 in typescript and also let me showcase 1819 01:28:27,400 --> 01:28:34,080 one more time how smart the typescript 1820 01:28:30,199 --> 01:28:36,440 is let's say we have a function and the 1821 01:28:34,080 --> 01:28:38,800 name is going to be sum now for now I'm 1822 01:28:36,440 --> 01:28:42,320 not going to pass any parameters I'm not 1823 01:28:38,800 --> 01:28:45,000 going to set up any return type simply 1824 01:28:42,320 --> 01:28:48,000 want to have my function and I'm just 1825 01:28:45,000 --> 01:28:50,880 going to invoke it and show you what 1826 01:28:48,000 --> 01:28:54,280 arguments I'm going to provide so this 1827 01:28:50,880 --> 01:28:57,320 one will be equal to result and in here 1828 01:28:54,280 --> 01:29:00,480 I just invoke the sum and effectively in 1829 01:28:57,320 --> 01:29:04,400 here I want to provide two arguments so 1830 01:29:00,480 --> 01:29:06,920 I want to provide a string and a list of 1831 01:29:04,400 --> 01:29:10,639 numbers so string is going to say well 1832 01:29:06,920 --> 01:29:14,159 the total is and then whatever numbers I 1833 01:29:10,639 --> 01:29:16,560 provide they're going to be added 1834 01:29:14,159 --> 01:29:20,840 together and that will be my sum so in 1835 01:29:16,560 --> 01:29:22,760 here let me go with the string and let's 1836 01:29:20,840 --> 01:29:25,239 say 1837 01:29:22,760 --> 01:29:27,159 total is 1838 01:29:25,239 --> 01:29:29,400 then we're going to go with the colon 1839 01:29:27,159 --> 01:29:31,719 and then we have list of parameters so 1840 01:29:29,400 --> 01:29:35,719 this is where we'll need to implement 1841 01:29:31,719 --> 01:29:39,159 the rest parameter otherwise I mean one 1842 01:29:35,719 --> 01:29:41,600 case I might add five numbers in the 1843 01:29:39,159 --> 01:29:43,000 next instance it might be only three and 1844 01:29:41,600 --> 01:29:45,119 hopefully you see where I'm going with 1845 01:29:43,000 --> 01:29:47,400 this so it's not always going to be one 1846 01:29:45,119 --> 01:29:49,280 or two numbers it's going to be a list 1847 01:29:47,400 --> 01:29:54,400 of numbers so in this case I'm going to 1848 01:29:49,280 --> 01:29:57,639 go with one 2 3 four and five now 1849 01:29:54,400 --> 01:30:00,360 typescript is complaining since we 1850 01:29:57,639 --> 01:30:03,000 haven't set up those parameters over 1851 01:30:00,360 --> 01:30:05,560 here so let's start with a message in 1852 01:30:03,000 --> 01:30:09,440 this case it's going to be a string and 1853 01:30:05,560 --> 01:30:12,639 now let's see how it can work with such 1854 01:30:09,440 --> 01:30:15,480 list and essentially the syntax is 1855 01:30:12,639 --> 01:30:17,800 similar to JavaScript the only 1856 01:30:15,480 --> 01:30:20,719 difference is that we need to provide 1857 01:30:17,800 --> 01:30:24,000 well what type of array it's going to be 1858 01:30:20,719 --> 01:30:26,639 so in JavaScript we use this syntax 1859 01:30:24,000 --> 01:30:28,719 three dots then we come up with a name 1860 01:30:26,639 --> 01:30:31,119 in this case I'm going to go with 1861 01:30:28,719 --> 01:30:34,199 numbers over here and this is the rest 1862 01:30:31,119 --> 01:30:37,600 parameter and essentially JavaScript 1863 01:30:34,199 --> 01:30:41,159 treats this as an array so in here we 1864 01:30:37,600 --> 01:30:42,920 want to go with number because I need to 1865 01:30:41,159 --> 01:30:45,880 let typescript know what kind of type it 1866 01:30:42,920 --> 01:30:50,280 is and then the array now please keep in 1867 01:30:45,880 --> 01:30:53,119 mind if you have strings here as well 1868 01:30:50,280 --> 01:30:56,719 essentially you just go with number then 1869 01:30:53,119 --> 01:30:59,199 use the type Union and essentially set 1870 01:30:56,719 --> 01:31:02,440 up a string there as well and now pretty 1871 01:30:59,199 --> 01:31:05,119 much all of the arguments that are pass 1872 01:31:02,440 --> 01:31:08,119 the string are going to be collected in 1873 01:31:05,119 --> 01:31:09,760 this numbers array now I also right away 1874 01:31:08,119 --> 01:31:12,199 want to set up what are we returning 1875 01:31:09,760 --> 01:31:15,520 from this function and essentially it's 1876 01:31:12,199 --> 01:31:18,080 going to be a string now before we set 1877 01:31:15,520 --> 01:31:20,840 up the logic which we're going to do 1878 01:31:18,080 --> 01:31:23,520 with reduce I also want to showcase how 1879 01:31:20,840 --> 01:31:24,560 smart typescript is so I'm just going to 1880 01:31:23,520 --> 01:31:27,840 create 1881 01:31:24,560 --> 01:31:29,440 the doubled variable and I'm going to 1882 01:31:27,840 --> 01:31:32,760 set it equal to numbers so that's my 1883 01:31:29,440 --> 01:31:34,520 array then let's invoke map then we're 1884 01:31:32,760 --> 01:31:39,679 going to go with 1885 01:31:34,520 --> 01:31:42,920 num and let's just return num * 2 and 1886 01:31:39,679 --> 01:31:44,600 again let me just log I guess here just 1887 01:31:42,920 --> 01:31:48,080 so we can see that the functionality 1888 01:31:44,600 --> 01:31:51,239 works but one I want to showcase though 1889 01:31:48,080 --> 01:31:54,320 is this notice as we hover over 1890 01:31:51,239 --> 01:31:57,639 typescript immediately knows that it's a 1891 01:31:54,320 --> 01:32:00,159 number so let's say you have some kind 1892 01:31:57,639 --> 01:32:04,639 of call back function that you pass into 1893 01:32:00,159 --> 01:32:07,199 the map into the reduce and all that 1894 01:32:04,639 --> 01:32:10,040 that's is going to see what kind of type 1895 01:32:07,199 --> 01:32:13,119 array it is if you have supplied those 1896 01:32:10,040 --> 01:32:16,280 values and it will immediately know that 1897 01:32:13,119 --> 01:32:20,080 as we're iterating over this is a number 1898 01:32:16,280 --> 01:32:22,360 now you can explicitly provide the value 1899 01:32:20,080 --> 01:32:25,639 I mean no one is stopping you to go here 1900 01:32:22,360 --> 01:32:28,560 and say number but in general typescript 1901 01:32:25,639 --> 01:32:32,000 is super helpful and we also have right 1902 01:32:28,560 --> 01:32:35,000 away this useful error where the type 1903 01:32:32,000 --> 01:32:37,000 should be string as far as my return but 1904 01:32:35,000 --> 01:32:38,840 at the moment well I'm not returning 1905 01:32:37,000 --> 01:32:40,679 anything so let me set up that 1906 01:32:38,840 --> 01:32:44,840 functionality as well and this is the 1907 01:32:40,679 --> 01:32:46,719 case where I will use the reduce and 1908 01:32:44,840 --> 01:32:49,360 then I'm just going to return the 1909 01:32:46,719 --> 01:32:52,000 message the total is and then I'll sum 1910 01:32:49,360 --> 01:32:55,760 up the values so I'm going to go with 1911 01:32:52,000 --> 01:32:58,960 let total is equal two numbers then 1912 01:32:55,760 --> 01:33:02,119 let's provide reduce over here I mean we 1913 01:32:58,960 --> 01:33:04,480 can call this accumulator and total or 1914 01:33:02,119 --> 01:33:06,800 current however you want to do it in my 1915 01:33:04,480 --> 01:33:12,159 case I think I'm going to go with 1916 01:33:06,800 --> 01:33:14,880 previous over here and the current 1917 01:33:12,159 --> 01:33:18,000 one and then as far as the 1918 01:33:14,880 --> 01:33:21,440 logic side of the function let's just 1919 01:33:18,000 --> 01:33:24,320 return previous plus and then the 1920 01:33:21,440 --> 01:33:27,679 current one and the same deal typ knows 1921 01:33:24,320 --> 01:33:30,960 that it's a number since I have array of 1922 01:33:27,679 --> 01:33:32,920 numbers now since I'm using reduce I 1923 01:33:30,960 --> 01:33:35,040 also want to provide the default value 1924 01:33:32,920 --> 01:33:38,320 which is going to be null and then from 1925 01:33:35,040 --> 01:33:39,800 this function I want to return a string 1926 01:33:38,320 --> 01:33:42,800 so I'm going to set up the template 1927 01:33:39,800 --> 01:33:45,560 literal we'll provide the message and 1928 01:33:42,800 --> 01:33:49,280 we'll also provide the total over here 1929 01:33:45,560 --> 01:33:53,840 so now let me just see what the result 1930 01:33:49,280 --> 01:33:55,560 is I guess so let me just log the result 1931 01:33:53,840 --> 01:33:58,320 and also we should see that doubled one 1932 01:33:55,560 --> 01:34:01,840 in console so notice this functionality 1933 01:33:58,320 --> 01:34:04,360 works over here and the total is 15 and 1934 01:34:01,840 --> 01:34:06,440 in the process we cover two important 1935 01:34:04,360 --> 01:34:09,080 things how we can work with rest 1936 01:34:06,440 --> 01:34:12,040 parameter which essentially collects the 1937 01:34:09,080 --> 01:34:14,760 list of values if we don't want to 1938 01:34:12,040 --> 01:34:17,840 access them one by one and also how 1939 01:34:14,760 --> 01:34:20,880 smart typescript is in the Callback 1940 01:34:17,840 --> 01:34:23,239 function we don't need to explicitly say 1941 01:34:20,880 --> 01:34:25,880 well what type it's going to be 1942 01:34:23,239 --> 01:34:29,320 typescript already knows and up next 1943 01:34:25,880 --> 01:34:31,679 let's discuss what type we should return 1944 01:34:29,320 --> 01:34:34,480 if we're not returning anything from the 1945 01:34:31,679 --> 01:34:37,480 function and a good old example is just 1946 01:34:34,480 --> 01:34:41,080 having a console log so let me go with 1947 01:34:37,480 --> 01:34:43,320 function log message as far as the 1948 01:34:41,080 --> 01:34:46,679 parameter we're looking for a 1949 01:34:43,320 --> 01:34:50,320 message and that one will be equal to a 1950 01:34:46,679 --> 01:34:53,880 string and then in here let's use our 1951 01:34:50,320 --> 01:34:56,920 good old console log and let's just log 1952 01:34:53,880 --> 01:34:59,400 the message now I do want to invoke it 1953 01:34:56,920 --> 01:35:01,159 and I want to provide some kind of value 1954 01:34:59,400 --> 01:35:04,360 so in this case I'm going to go with 1955 01:35:01,159 --> 01:35:07,679 hello and typescript and since we have 1956 01:35:04,360 --> 01:35:11,080 nice suggestions if we just hover over 1957 01:35:07,679 --> 01:35:14,840 the log message we can see that 1958 01:35:11,080 --> 01:35:18,400 typescript inferred the return type as 1959 01:35:14,840 --> 01:35:22,159 void and void is a special type that 1960 01:35:18,400 --> 01:35:24,960 represents the absence of a value and 1961 01:35:22,159 --> 01:35:27,400 when we use it has a function return 1962 01:35:24,960 --> 01:35:31,920 type void indicates that the function 1963 01:35:27,400 --> 01:35:34,320 does not return a value now milon dollar 1964 01:35:31,920 --> 01:35:36,320 question do you always have to set it up 1965 01:35:34,320 --> 01:35:38,960 I mean it really depends on your 1966 01:35:36,320 --> 01:35:41,800 approach but it is super helpful 1967 01:35:38,960 --> 01:35:44,800 especially if you consider this once we 1968 01:35:41,800 --> 01:35:47,679 add this void over here even if I set up 1969 01:35:44,800 --> 01:35:50,320 some kind of return and I'm going to go 1970 01:35:47,679 --> 01:35:52,960 with hello world notice how typescript 1971 01:35:50,320 --> 01:35:57,440 right away says well you cannot assign 1972 01:35:52,960 --> 01:35:59,480 string to a void so if we decide that 1973 01:35:57,440 --> 01:36:02,440 this function is not going to return 1974 01:35:59,480 --> 01:36:05,280 anything and we set up the keyword even 1975 01:36:02,440 --> 01:36:07,159 if later on we mess up something and 1976 01:36:05,280 --> 01:36:10,600 start returning some values from the 1977 01:36:07,159 --> 01:36:13,119 function typescript is going to stop us 1978 01:36:10,600 --> 01:36:16,000 from doing that and up next let's 1979 01:36:13,119 --> 01:36:19,679 discuss Union types as function 1980 01:36:16,000 --> 01:36:21,960 parameters and what the typ guards are 1981 01:36:19,679 --> 01:36:25,159 now I did set up this topic as a 1982 01:36:21,960 --> 01:36:28,560 challenge just so you can see firsthand 1983 01:36:25,159 --> 01:36:32,040 the type of issues we will face so 1984 01:36:28,560 --> 01:36:36,119 here's the task I want you to create a 1985 01:36:32,040 --> 01:36:41,040 function called process input that 1986 01:36:36,119 --> 01:36:45,560 accepts a parameter of Union type so it 1987 01:36:41,040 --> 01:36:48,119 can either be a string or a number and 1988 01:36:45,560 --> 01:36:50,840 when it comes to 1989 01:36:48,119 --> 01:36:54,159 functionality if the input type is 1990 01:36:50,840 --> 01:36:56,800 number we simply want to multiply it and 1991 01:36:54,159 --> 01:36:59,440 log it in the console and if it's going 1992 01:36:56,800 --> 01:37:02,040 to be a string just use one of the 1993 01:36:59,440 --> 01:37:05,400 methods so in my case since my favorite 1994 01:37:02,040 --> 01:37:07,480 method is the uppercase one most likely 1995 01:37:05,400 --> 01:37:09,560 I'll use that one but it really comes 1996 01:37:07,480 --> 01:37:12,520 down to the preference so I'm going to 1997 01:37:09,560 --> 01:37:14,280 navigate to my tutorial and I want to 1998 01:37:12,520 --> 01:37:17,880 come up with a function so I'm going to 1999 01:37:14,280 --> 01:37:20,040 go with process and then input and I 2000 01:37:17,880 --> 01:37:22,520 believe in one of the previous videos 2001 01:37:20,040 --> 01:37:24,520 when we covered the rest parameter I 2002 01:37:22,520 --> 01:37:27,400 mentioned that you can still use the 2003 01:37:24,520 --> 01:37:29,600 union type I mean it's not a big deal 2004 01:37:27,400 --> 01:37:32,760 here I can go with input and then just 2005 01:37:29,600 --> 01:37:35,159 say string or the 2006 01:37:32,760 --> 01:37:38,480 number however once we set up the 2007 01:37:35,159 --> 01:37:42,320 functionality this is where we'll add 2008 01:37:38,480 --> 01:37:45,159 additional logic since now we have two 2009 01:37:42,320 --> 01:37:48,440 potential values correct so first let's 2010 01:37:45,159 --> 01:37:51,760 go with process input and pass in then 2011 01:37:48,440 --> 01:37:56,159 this should work and also let's pass in 2012 01:37:51,760 --> 01:38:00,000 the hello now what about the logic if we 2013 01:37:56,159 --> 01:38:04,080 simply log we should be good to go so if 2014 01:38:00,000 --> 01:38:06,360 I type input notice there are no issues 2015 01:38:04,080 --> 01:38:09,719 but now what if I want to multiply this 2016 01:38:06,360 --> 01:38:13,599 what if I want to say times two notice 2017 01:38:09,719 --> 01:38:18,080 immediately typescript is not happy why 2018 01:38:13,599 --> 01:38:21,560 well we have a type 2019 01:38:18,080 --> 01:38:23,280 string and we're multiplying this by two 2020 01:38:21,560 --> 01:38:25,880 now typescript doesn't know that in here 2021 01:38:23,280 --> 01:38:28,840 we passing in 10 and in here we have the 2022 01:38:25,880 --> 01:38:33,239 hello so we need to set up something 2023 01:38:28,840 --> 01:38:37,239 called type guard where before we do any 2024 01:38:33,239 --> 01:38:41,159 functionality we actually make sure that 2025 01:38:37,239 --> 01:38:42,960 the type is correct essentially if I 2026 01:38:41,159 --> 01:38:45,199 want to run a string method I want to 2027 01:38:42,960 --> 01:38:46,960 make sure that it's a string if I want 2028 01:38:45,199 --> 01:38:50,440 to multiply I want to make sure that 2029 01:38:46,960 --> 01:38:53,920 it's a number and again the fancy name 2030 01:38:50,440 --> 01:38:58,639 for that is typ guard now we you'll look 2031 01:38:53,920 --> 01:39:02,239 at multiple type guards the most 2032 01:38:58,639 --> 01:39:05,119 straightforward one is just type off 2033 01:39:02,239 --> 01:39:07,599 keyword and then we look for specific 2034 01:39:05,119 --> 01:39:10,639 value so inside of the function I do 2035 01:39:07,599 --> 01:39:12,360 want to set up a check so there's going 2036 01:39:10,639 --> 01:39:16,280 to be a condition and I'm going to go 2037 01:39:12,360 --> 01:39:18,880 with type off then I'm looking for my 2038 01:39:16,280 --> 01:39:21,760 parameter and I'm going to say if it's 2039 01:39:18,880 --> 01:39:24,520 equal to a number and notice how 2040 01:39:21,760 --> 01:39:27,280 immediately typescript was helpful and 2041 01:39:24,520 --> 01:39:30,520 provided bunch of options if you didn't 2042 01:39:27,280 --> 01:39:32,719 let me go back notice these are the 2043 01:39:30,520 --> 01:39:35,199 values that we can use and in this case 2044 01:39:32,719 --> 01:39:37,480 we're looking for the number if that's 2045 01:39:35,199 --> 01:39:41,239 the case what do I want to do well I'm 2046 01:39:37,480 --> 01:39:44,040 going to log times two over here now if 2047 01:39:41,239 --> 01:39:48,040 it's not the case typescript is also 2048 01:39:44,040 --> 01:39:50,520 smart it knows that listen if it's a 2049 01:39:48,040 --> 01:39:53,159 number we're going to do this 2050 01:39:50,520 --> 01:39:55,920 functionality what is the alternative 2051 01:39:53,159 --> 01:39:58,320 well the alternative it's a string 2052 01:39:55,920 --> 01:40:00,800 correct so in here we simply can go with 2053 01:39:58,320 --> 01:40:02,440 else then again we're going to go with 2054 01:40:00,800 --> 01:40:05,199 log 2055 01:40:02,440 --> 01:40:07,199 input and you know what just so people 2056 01:40:05,199 --> 01:40:08,880 are not upset with me I'm just going to 2057 01:40:07,199 --> 01:40:11,159 go with two 2058 01:40:08,880 --> 01:40:14,480 lowercase again the official name for 2059 01:40:11,159 --> 01:40:18,080 this one is type guard and we use it to 2060 01:40:14,480 --> 01:40:21,000 check the parameter at the runtime and 2061 01:40:18,080 --> 01:40:23,840 while we're still on a roll let's also 2062 01:40:21,000 --> 01:40:27,280 see how we can work with objects 2063 01:40:23,840 --> 01:40:30,040 as parameters in our function so I'm 2064 01:40:27,280 --> 01:40:31,280 going to set up a new function let me 2065 01:40:30,040 --> 01:40:33,840 call this 2066 01:40:31,280 --> 01:40:37,239 employ and in here I'll pass in the 2067 01:40:33,840 --> 01:40:38,280 object with the ID property and as a 2068 01:40:37,239 --> 01:40:41,320 side 2069 01:40:38,280 --> 01:40:43,760 note the second example which we're 2070 01:40:41,320 --> 01:40:47,599 going to cover is going to use a little 2071 01:40:43,760 --> 01:40:50,239 different syntax so in here essentially 2072 01:40:47,599 --> 01:40:52,520 I'm right away D structuring the ID I'm 2073 01:40:50,239 --> 01:40:55,119 saying yeah there's going to be an 2074 01:40:52,520 --> 01:40:59,040 object with the ID property and I right 2075 01:40:55,119 --> 01:41:01,880 away want to access it now as far as the 2076 01:40:59,040 --> 01:41:03,960 typescript same deal just like with 2077 01:41:01,880 --> 01:41:07,239 regular objects we want to set up this 2078 01:41:03,960 --> 01:41:11,520 type annotation and we want to be 2079 01:41:07,239 --> 01:41:14,800 specific well what are we're going to be 2080 01:41:11,520 --> 01:41:16,760 getting here so is this a number string 2081 01:41:14,800 --> 01:41:20,119 or whatever and in this case I'm going 2082 01:41:16,760 --> 01:41:24,400 to set it equal to a number as a side 2083 01:41:20,119 --> 01:41:27,520 note yes in like I believe three videos 2084 01:41:24,400 --> 01:41:29,480 we will cover typee aliases so it's not 2085 01:41:27,520 --> 01:41:31,599 going to be as clunky because at the 2086 01:41:29,480 --> 01:41:34,840 moment I fully understand if you're 2087 01:41:31,599 --> 01:41:36,840 looking at it and you're like whoa so I 2088 01:41:34,840 --> 01:41:38,679 understand this part but does that mean 2089 01:41:36,840 --> 01:41:41,000 that every time I'll have object with 2090 01:41:38,679 --> 01:41:44,400 like 10 properties we have to set it up 2091 01:41:41,000 --> 01:41:48,080 over here no there is a better solution 2092 01:41:44,400 --> 01:41:50,920 and it's coming up and also just like 2093 01:41:48,080 --> 01:41:53,400 with parameters can also set up the 2094 01:41:50,920 --> 01:41:57,119 return type as an object out of does 2095 01:41:53,400 --> 01:42:02,599 that look like well same deal I just say 2096 01:41:57,119 --> 01:42:05,080 colon then I provide the curries and 2097 01:42:02,599 --> 01:42:06,639 what are we returning from this function 2098 01:42:05,080 --> 01:42:08,360 I'm going to go with ID which again is 2099 01:42:06,639 --> 01:42:12,000 going to be a number and also there's 2100 01:42:08,360 --> 01:42:15,520 going to be is active in this object and 2101 01:42:12,000 --> 01:42:16,520 that one will be a Boolean and again we 2102 01:42:15,520 --> 01:42:20,639 set up the 2103 01:42:16,520 --> 01:42:23,440 Cur so we have two sets of curries over 2104 01:42:20,639 --> 01:42:25,719 here and we also have two sets over here 2105 01:42:23,440 --> 01:42:27,280 because we have one for return and one 2106 01:42:25,719 --> 01:42:30,520 for 2107 01:42:27,280 --> 01:42:32,639 functionality and as far as the logic I 2108 01:42:30,520 --> 01:42:34,960 just want to return an object from this 2109 01:42:32,639 --> 01:42:37,599 function and I need to make sure I 2110 01:42:34,960 --> 01:42:40,840 follow this pattern correct I need to 2111 01:42:37,599 --> 01:42:43,199 make sure that I return a number and a 2112 01:42:40,840 --> 01:42:46,800 Boolean so in this case I'll just right 2113 01:42:43,199 --> 01:42:49,239 away grab whatever I have over here and 2114 01:42:46,800 --> 01:42:51,840 also as far as the is active I'm just 2115 01:42:49,239 --> 01:42:54,360 going to do some basic logic I'm going 2116 01:42:51,840 --> 01:42:58,080 to say ID and then I'm going to use the 2117 01:42:54,360 --> 01:43:00,840 modulus operator essentially if ID 2118 01:42:58,080 --> 01:43:03,639 modulus 2 is equal to zero then it's 2119 01:43:00,840 --> 01:43:06,360 going to be true which means essentially 2120 01:43:03,639 --> 01:43:08,560 that it's an even number if not then 2121 01:43:06,360 --> 01:43:11,520 it's going to be false so now let's try 2122 01:43:08,560 --> 01:43:15,280 it out we're going to go with const 2123 01:43:11,520 --> 01:43:17,320 first set it equal to create employee 2124 01:43:15,280 --> 01:43:19,599 and then let's pass in our object so in 2125 01:43:17,320 --> 01:43:23,760 this case it's going to be one then copy 2126 01:43:19,599 --> 01:43:26,400 and paste this will be equal to a second 2127 01:43:23,760 --> 01:43:30,159 over here and then we want to log both 2128 01:43:26,400 --> 01:43:33,800 of these results so let's look for first 2129 01:43:30,159 --> 01:43:35,199 and second let me navigate to a console 2130 01:43:33,800 --> 01:43:38,360 and for some 2131 01:43:35,199 --> 01:43:40,280 reason says false because yep I didn't 2132 01:43:38,360 --> 01:43:43,239 change this one this should be equal to 2133 01:43:40,280 --> 01:43:46,000 two so now the first one is active is 2134 01:43:43,239 --> 01:43:48,320 false and then the second one is active 2135 01:43:46,000 --> 01:43:50,560 is true again we really don't care about 2136 01:43:48,320 --> 01:43:52,320 the functionality I mean we can 2137 01:43:50,560 --> 01:43:54,800 definitely hardcode over here doesn't 2138 01:43:52,320 --> 01:43:57,440 really matter so hopefully it's clear if 2139 01:43:54,800 --> 01:44:00,880 we have an object we nicely set up right 2140 01:43:57,440 --> 01:44:03,440 away The annotation at least with the 2141 01:44:00,880 --> 01:44:06,520 current knowledge we have now also we 2142 01:44:03,440 --> 01:44:09,000 have alternative approach where we can 2143 01:44:06,520 --> 01:44:12,000 give some kind of name to this object 2144 01:44:09,000 --> 01:44:14,159 and then we can just use it inside of 2145 01:44:12,000 --> 01:44:15,719 the function body again please keep in 2146 01:44:14,159 --> 01:44:17,880 mind the general idea as far as 2147 01:44:15,719 --> 01:44:20,320 typescript doesn't change but I do want 2148 01:44:17,880 --> 01:44:22,840 to make sure that we understand that we 2149 01:44:20,320 --> 01:44:23,639 have multiple approaches so let me call 2150 01:44:22,840 --> 01:44:26,480 this 2151 01:44:23,639 --> 01:44:29,599 alternative over here then we will go 2152 01:44:26,480 --> 01:44:32,199 with function create student in this 2153 01:44:29,599 --> 01:44:33,199 case and I know that I'm going to get 2154 01:44:32,199 --> 01:44:35,280 the 2155 01:44:33,199 --> 01:44:38,119 object and I'm just going to give it a 2156 01:44:35,280 --> 01:44:41,080 name of student so this still will be 2157 01:44:38,119 --> 01:44:44,520 object but I'll just access it with 2158 01:44:41,080 --> 01:44:47,159 student. student do and same deal I want 2159 01:44:44,520 --> 01:44:50,080 to go with my annotation I'll say that 2160 01:44:47,159 --> 01:44:53,400 ID will be number and name will be equal 2161 01:44:50,080 --> 01:44:55,639 to a string over here and and then as 2162 01:44:53,400 --> 01:44:57,639 far as D function body I'm actually 2163 01:44:55,639 --> 01:45:00,080 going to go with void so we're not going 2164 01:44:57,639 --> 01:45:02,480 to return anything from this function 2165 01:45:00,080 --> 01:45:04,639 and we're just going to log stuff so let 2166 01:45:02,480 --> 01:45:08,159 me scroll down a little bit let's set up 2167 01:45:04,639 --> 01:45:09,599 the template literal welcome to the 2168 01:45:08,159 --> 01:45:12,280 course I 2169 01:45:09,599 --> 01:45:15,599 guess and let's access the student where 2170 01:45:12,280 --> 01:45:19,599 we go with student and notice we right 2171 01:45:15,599 --> 01:45:22,159 away get the suggestion which properties 2172 01:45:19,599 --> 01:45:23,639 are on this object since this is what we 2173 01:45:22,159 --> 01:45:25,960 specified 2174 01:45:23,639 --> 01:45:29,599 in our type annotation and in this case 2175 01:45:25,960 --> 01:45:32,159 I want to go with name and then to 2176 01:45:29,599 --> 01:45:35,639 uppercase also please keep in mind that 2177 01:45:32,159 --> 01:45:38,840 yes we can the structure right here in a 2178 01:45:35,639 --> 01:45:41,719 function body as well that's totally an 2179 01:45:38,840 --> 01:45:45,679 option so now let me call 2180 01:45:41,719 --> 01:45:48,159 it with the new student so in this case 2181 01:45:45,679 --> 01:45:50,840 just to again showcase that we have 2182 01:45:48,159 --> 01:45:53,520 multiple approaches we want to go with 2183 01:45:50,840 --> 01:45:55,800 student we want to set it equal to an 2184 01:45:53,520 --> 01:45:58,239 object I'm going to go with five over 2185 01:45:55,800 --> 01:46:00,960 here and name is going to be Anna and 2186 01:45:58,239 --> 01:46:03,719 I'm just going to rely on typescript to 2187 01:46:00,960 --> 01:46:06,679 infer the type and then we'll go with 2188 01:46:03,719 --> 01:46:08,960 create student pass in the new student 2189 01:46:06,679 --> 01:46:12,159 and if everything is correct we should 2190 01:46:08,960 --> 01:46:14,679 have the value in the console and with 2191 01:46:12,159 --> 01:46:17,840 this in place we can move on to the next 2192 01:46:14,679 --> 01:46:19,719 topic okay and before we move on and 2193 01:46:17,840 --> 01:46:23,840 start working on our next challenge 2194 01:46:19,719 --> 01:46:28,080 let's also cover the access property 2195 01:46:23,840 --> 01:46:31,480 check gotcha and for this one actually 2196 01:46:28,080 --> 01:46:33,599 we don't need to create too much code 2197 01:46:31,480 --> 01:46:37,159 from the scratch I'm just going to 2198 01:46:33,599 --> 01:46:40,320 remove my first function from the 2199 01:46:37,159 --> 01:46:44,800 previous video I'll leave create student 2200 01:46:40,320 --> 01:46:45,800 as it is and I simply want to invoke it 2201 01:46:44,800 --> 01:46:48,400 right 2202 01:46:45,800 --> 01:46:52,320 below and in this case I will pass 2203 01:46:48,400 --> 01:46:55,159 everything in line so unlike previously 2204 01:46:52,320 --> 01:46:57,840 where we set the object now I'll provide 2205 01:46:55,159 --> 01:47:00,679 all of these properties one by one let's 2206 01:46:57,840 --> 01:47:04,199 start over here with ID I'll set it 2207 01:47:00,679 --> 01:47:06,119 equal to one as note it's totally 2208 01:47:04,199 --> 01:47:08,440 irrelevant to what we pass in here 2209 01:47:06,119 --> 01:47:12,960 that's not the gotcha the gotcha is 2210 01:47:08,440 --> 01:47:15,040 following if we add here extra property 2211 01:47:12,960 --> 01:47:18,840 what do you think is going to happen 2212 01:47:15,040 --> 01:47:23,560 well we have a type annotation for ID in 2213 01:47:18,840 --> 01:47:27,639 string correct so I mean logic tells us 2214 01:47:23,560 --> 01:47:32,599 that if we add here email and we set it 2215 01:47:27,639 --> 01:47:35,599 equal to Bobo at gmail.com typescript is 2216 01:47:32,599 --> 01:47:38,239 going to complain and you're totally 2217 01:47:35,599 --> 01:47:39,679 correct essentially it says Hey listen 2218 01:47:38,239 --> 01:47:43,280 you didn't specify that there's going to 2219 01:47:39,679 --> 01:47:46,199 be an email while you're adding this to 2220 01:47:43,280 --> 01:47:49,480 the object so this is something we 2221 01:47:46,199 --> 01:47:52,159 expect however notice this interesting 2222 01:47:49,480 --> 01:47:53,520 Behavior where I can go here with an 2223 01:47:52,159 --> 01:47:56,440 email 2224 01:47:53,520 --> 01:47:59,560 and essentially pass in some kind of 2225 01:47:56,440 --> 01:48:02,599 value and you know what it's going to 2226 01:47:59,560 --> 01:48:04,239 work typescript is not going to complain 2227 01:48:02,599 --> 01:48:05,719 so yeah this is this interesting 2228 01:48:04,239 --> 01:48:09,480 Behavior 2229 01:48:05,719 --> 01:48:13,320 where when we pass in object properties 2230 01:48:09,480 --> 01:48:15,639 in line typescript expects to match this 2231 01:48:13,320 --> 01:48:18,840 exactly so if we'll add some extra 2232 01:48:15,639 --> 01:48:21,400 properties TPT will immediately complain 2233 01:48:18,840 --> 01:48:24,840 because in typescript mind we have a 2234 01:48:21,400 --> 01:48:27,440 full control of what we're adding in 2235 01:48:24,840 --> 01:48:30,520 line however if we're just referencing 2236 01:48:27,440 --> 01:48:33,480 the object and there's an extra property 2237 01:48:30,520 --> 01:48:35,719 the only thing typescript cares about 2238 01:48:33,480 --> 01:48:38,920 are the properties that are in the type 2239 01:48:35,719 --> 01:48:41,400 notation so if there's 120 extra 2240 01:48:38,920 --> 01:48:44,040 properties typescript is like okay do 2241 01:48:41,400 --> 01:48:46,920 you have an ID and name if yes we're 2242 01:48:44,040 --> 01:48:49,040 good to go if not then of course there's 2243 01:48:46,920 --> 01:48:52,880 still going to be an issue hopefully 2244 01:48:49,040 --> 01:48:55,000 it's clear just remember this gotcha and 2245 01:48:52,880 --> 01:48:58,080 you'll be good to go all right I think 2246 01:48:55,000 --> 01:48:59,520 we're ready for our next challenge and 2247 01:48:58,080 --> 01:49:02,840 at least in my opinion this one is going 2248 01:48:59,520 --> 01:49:05,360 to be super interesting because we'll 2249 01:49:02,840 --> 01:49:08,760 combine multiple things from the 2250 01:49:05,360 --> 01:49:11,880 previous videos so we want to create a 2251 01:49:08,760 --> 01:49:14,080 process data function which is going to 2252 01:49:11,880 --> 01:49:17,199 accept two parameters first one is going 2253 01:49:14,080 --> 01:49:20,880 to be input parameter which can be a 2254 01:49:17,199 --> 01:49:24,360 string or a number and second one will 2255 01:49:20,880 --> 01:49:27,679 be a config object and instead of that 2256 01:49:24,360 --> 01:49:31,520 config object we'll have the reverse 2257 01:49:27,679 --> 01:49:35,040 property which is a Boolean so if we 2258 01:49:31,520 --> 01:49:38,880 pass in a number we just want to 2259 01:49:35,040 --> 01:49:41,639 multiply the number with its own value 2260 01:49:38,880 --> 01:49:46,440 essentially input multiplied by the 2261 01:49:41,639 --> 01:49:50,159 input however if the input is the string 2262 01:49:46,440 --> 01:49:54,040 our second option and if the reverse is 2263 01:49:50,159 --> 01:49:58,000 true we want to reverse the string 2264 01:49:54,040 --> 01:50:01,920 and set it uppercase now if we provide a 2265 01:49:58,000 --> 01:50:05,639 string but the reverse is set to false 2266 01:50:01,920 --> 01:50:08,000 well in that case we'll simply set up 2267 01:50:05,639 --> 01:50:11,000 the string to be uppercase and just 2268 01:50:08,000 --> 01:50:14,320 return it so let me navigate to my 2269 01:50:11,000 --> 01:50:17,520 tutorial I think I'll just remove all of 2270 01:50:14,320 --> 01:50:19,480 this logic and you know what for stars I 2271 01:50:17,520 --> 01:50:22,800 want to set up a 2272 01:50:19,480 --> 01:50:24,239 function in here I'm going to call the 2273 01:50:22,800 --> 01:50:27,119 this 2274 01:50:24,239 --> 01:50:30,040 process data I'm not going to set up any 2275 01:50:27,119 --> 01:50:34,040 parameters yet same goes for 2276 01:50:30,040 --> 01:50:36,679 functionality and I simply want to log 2277 01:50:34,040 --> 01:50:39,760 it in three places essentially invoke it 2278 01:50:36,679 --> 01:50:42,320 and then log it so in the first case I'm 2279 01:50:39,760 --> 01:50:46,679 going to pass in 10 and 2280 01:50:42,320 --> 01:50:48,239 then I'll set up two more hellos so 2281 01:50:46,679 --> 01:50:51,760 these are going to be instances where we 2282 01:50:48,239 --> 01:50:54,560 pass in the string and now as far as the 2283 01:50:51,760 --> 01:50:57,679 process data like I said we have input 2284 01:50:54,560 --> 01:50:59,679 over here but we want to use the type 2285 01:50:57,679 --> 01:51:03,520 Union so either it's going to be a 2286 01:50:59,679 --> 01:51:06,880 string or it will be a number so that 2287 01:51:03,520 --> 01:51:10,440 will be our first parameter now the 2288 01:51:06,880 --> 01:51:13,159 second one is the config one if we pass 2289 01:51:10,440 --> 01:51:16,040 this config in there there's going to be 2290 01:51:13,159 --> 01:51:18,800 a reverse property and as a sign note I 2291 01:51:16,040 --> 01:51:22,000 actually forgot to do that so first one 2292 01:51:18,800 --> 01:51:23,159 is hello and then we want to go with our 2293 01:51:22,000 --> 01:51:26,639 config 2294 01:51:23,159 --> 01:51:30,560 object and I will set the reverse equals 2295 01:51:26,639 --> 01:51:32,719 to true so let's navigate to our 2296 01:51:30,560 --> 01:51:35,719 parameters and let's set up the second 2297 01:51:32,719 --> 01:51:38,159 one so I'm going to call this config 2298 01:51:35,719 --> 01:51:41,960 it's an object and in there I'll have 2299 01:51:38,159 --> 01:51:46,079 the reverse property so I'll set it 2300 01:51:41,960 --> 01:51:50,480 equal to a Boolean now here's the kicker 2301 01:51:46,079 --> 01:51:53,040 what if I pass in the number do I really 2302 01:51:50,480 --> 01:51:55,239 care about that reverse one 2303 01:51:53,040 --> 01:51:58,400 and the answer is no I only care about 2304 01:51:55,239 --> 01:52:02,119 it if it's a string correct only then 2305 01:51:58,400 --> 01:52:04,320 we'll check but if it's a number I mean 2306 01:52:02,119 --> 01:52:07,360 doesn't really matter and therefore I 2307 01:52:04,320 --> 01:52:09,560 can nicely set this as default reverse 2308 01:52:07,360 --> 01:52:11,920 false so I'm not going to have to do 2309 01:52:09,560 --> 01:52:15,280 that additional check basically Once I 2310 01:52:11,920 --> 01:52:18,880 pass in the number I don't have to pass 2311 01:52:15,280 --> 01:52:21,679 this additional config object so in here 2312 01:52:18,880 --> 01:52:25,599 let's go with default one and set it 2313 01:52:21,679 --> 01:52:27,440 equal to verse and false now let's also 2314 01:52:25,599 --> 01:52:29,920 set up what we're returning from this 2315 01:52:27,440 --> 01:52:33,320 function so either it's going to be a 2316 01:52:29,920 --> 01:52:36,719 string or a number and now when it comes 2317 01:52:33,320 --> 01:52:41,800 to logic well we have two options 2318 01:52:36,719 --> 01:52:44,400 correct so we can have a string or a 2319 01:52:41,800 --> 01:52:46,760 number so we do need to set up a typ 2320 01:52:44,400 --> 01:52:48,719 guard if we want to multiply it I want 2321 01:52:46,760 --> 01:52:52,760 to make sure that it's actually a number 2322 01:52:48,719 --> 01:52:56,639 so let's go with if then type off if the 2323 01:52:52,760 --> 01:52:59,199 input is equal to a number well that's 2324 01:52:56,639 --> 01:53:04,079 when we want to multiply and return so 2325 01:52:59,199 --> 01:53:06,480 let's go with return that input times 2326 01:53:04,079 --> 01:53:09,440 input and it looks like I have a tiny 2327 01:53:06,480 --> 01:53:12,480 typ over here this should be return and 2328 01:53:09,440 --> 01:53:14,599 also we want to set up the else and in 2329 01:53:12,480 --> 01:53:18,119 here we're going to go with the return 2330 01:53:14,599 --> 01:53:22,760 then we'll check the config reverse so 2331 01:53:18,119 --> 01:53:24,960 by default it's going to be false so if 2332 01:53:22,760 --> 01:53:28,560 it's true then we want to set it to 2333 01:53:24,960 --> 01:53:31,599 uppercase split reverse and join if not 2334 01:53:28,560 --> 01:53:34,599 well then we'll simply return it with 2335 01:53:31,599 --> 01:53:37,800 the uppercase so let's set up over here 2336 01:53:34,599 --> 01:53:41,480 a dinary operator we're going to go with 2337 01:53:37,800 --> 01:53:44,480 input then to uppercase let's invoke 2338 01:53:41,480 --> 01:53:47,000 that then we're going to split it then 2339 01:53:44,480 --> 01:53:51,000 we'll call a reverse on 2340 01:53:47,000 --> 01:53:55,520 it over here and then lastly let's use 2341 01:53:51,000 --> 01:53:57,679 join method let's do this and if it's 2342 01:53:55,520 --> 01:54:00,599 false which is the default one we're 2343 01:53:57,679 --> 01:54:03,840 going to go with input then 2344 01:54:00,599 --> 01:54:06,320 two uppercase and we're going to invoke 2345 01:54:03,840 --> 01:54:09,960 it so now let me navigate to the browser 2346 01:54:06,320 --> 01:54:12,400 and let's see so I have 100 then I have 2347 01:54:09,960 --> 01:54:15,880 straight up hello just to uppercase and 2348 01:54:12,400 --> 01:54:18,560 then I have the reverse value again in 2349 01:54:15,880 --> 01:54:21,079 my opinion the biggest gotcha is over 2350 01:54:18,560 --> 01:54:24,239 here you see if you're not going to 2351 01:54:21,079 --> 01:54:26,480 provide this default value over here 2352 01:54:24,239 --> 01:54:29,000 then pretty much every time you'll 2353 01:54:26,480 --> 01:54:31,599 invoke this processed 2354 01:54:29,000 --> 01:54:33,639 data it's also expecting this object 2355 01:54:31,599 --> 01:54:36,320 over here and if that's the 2356 01:54:33,639 --> 01:54:38,679 functionality you want to go for it's 2357 01:54:36,320 --> 01:54:41,360 definitely an option but in my opinion 2358 01:54:38,679 --> 01:54:43,800 it's super useful to add this default 2359 01:54:41,360 --> 01:54:47,639 one because in that case well if it's a 2360 01:54:43,800 --> 01:54:50,239 number or if I don't want to actually 2361 01:54:47,639 --> 01:54:52,400 reverse it well I simply don't provide 2362 01:54:50,239 --> 01:54:54,280 that argument at least that was my line 2363 01:54:52,400 --> 01:54:56,440 magic and with this in place we're done 2364 01:54:54,280 --> 01:54:59,440 with the challenge and we can move on to 2365 01:54:56,440 --> 01:55:03,400 the next topic all right and we have 2366 01:54:59,440 --> 01:55:04,960 finally arrived at the type alas at 2367 01:55:03,400 --> 01:55:07,639 least in my opinion one of the most 2368 01:55:04,960 --> 01:55:10,079 exciting topics because once I learned 2369 01:55:07,639 --> 01:55:14,040 the topic I was super super excited 2370 01:55:10,079 --> 01:55:17,000 since it just eliminated bunch of clunky 2371 01:55:14,040 --> 01:55:20,520 code and for this one actually want to 2372 01:55:17,000 --> 01:55:23,000 start with the code 2373 01:55:20,520 --> 01:55:25,360 already present because I want to cover 2374 01:55:23,000 --> 01:55:27,159 some stuff on I highly encourage you to 2375 01:55:25,360 --> 01:55:30,079 do pretty much the same there's really 2376 01:55:27,159 --> 01:55:32,239 no need to type this from the scratch so 2377 01:55:30,079 --> 01:55:36,560 grab it from the readme and then let's 2378 01:55:32,239 --> 01:55:39,920 go line by line so we have two objects 2379 01:55:36,560 --> 01:55:43,119 and we nicely set up over here the type 2380 01:55:39,920 --> 01:55:47,679 annotations where ID is a number name is 2381 01:55:43,119 --> 01:55:49,639 a string and is active is a buan so if 2382 01:55:47,679 --> 01:55:52,040 I'm setting up some wrong values 2383 01:55:49,639 --> 01:55:55,840 typescript is going to be super helpful 2384 01:55:52,040 --> 01:56:01,679 and then we have a function which again 2385 01:55:55,840 --> 01:56:04,639 is looking for a user and that user must 2386 01:56:01,679 --> 01:56:08,520 have this particular shape ID is a 2387 01:56:04,639 --> 01:56:12,800 number name is a string and is active is 2388 01:56:08,520 --> 01:56:15,360 a buing okay beautiful and not only that 2389 01:56:12,800 --> 01:56:18,159 but from this function for some weird 2390 01:56:15,360 --> 01:56:21,800 reason we return the same thing we 2391 01:56:18,159 --> 01:56:25,360 return the same shape we return ID name 2392 01:56:21,800 --> 01:56:27,079 and is active and so is the 2393 01:56:25,360 --> 01:56:30,560 functionality we just set it to 2394 01:56:27,079 --> 01:56:31,719 uppercase and then return the user and I 2395 01:56:30,560 --> 01:56:33,239 can tell you right away that there's 2396 01:56:31,719 --> 01:56:37,320 nothing wrong with this code as far as 2397 01:56:33,239 --> 01:56:40,679 functionality all of this works what is 2398 01:56:37,320 --> 01:56:43,719 the biggest issue I mean we have code 2399 01:56:40,679 --> 01:56:47,040 over here same code over here same code 2400 01:56:43,719 --> 01:56:49,079 over here and then we have the return 2401 01:56:47,040 --> 01:56:51,679 and this is just one file what do we 2402 01:56:49,079 --> 01:56:52,880 have like 20 lines of code and pretty 2403 01:56:51,679 --> 01:56:55,239 much we just 2404 01:56:52,880 --> 01:56:58,239 constantly keep repeating this so 2405 01:56:55,239 --> 01:57:01,639 without further Ado I want to introduce 2406 01:56:58,239 --> 01:57:05,960 you to a type areas in typescript which 2407 01:57:01,639 --> 01:57:08,239 again in my opinion is extremely helpful 2408 01:57:05,960 --> 01:57:10,079 feature now before we continue let me 2409 01:57:08,239 --> 01:57:15,520 just make sure that we are all on the 2410 01:57:10,079 --> 01:57:19,719 same page you see a type Alias is just a 2411 01:57:15,520 --> 01:57:21,639 new name or a shorthand for existing 2412 01:57:19,719 --> 01:57:23,800 type and in this case we're talking 2413 01:57:21,639 --> 01:57:26,199 about this type over here this type 2414 01:57:23,800 --> 01:57:30,320 annotation so nothing is actually going 2415 01:57:26,199 --> 01:57:33,920 to change as far as this syntax is 2416 01:57:30,320 --> 01:57:37,679 concerned however with type Alias it's 2417 01:57:33,920 --> 01:57:39,199 going to be easier to reuse this one so 2418 01:57:37,679 --> 01:57:42,760 again please keep in mind we're not 2419 01:57:39,199 --> 01:57:46,280 creating a new type it's just an Alas 2420 01:57:42,760 --> 01:57:49,280 and all of the same rules apply to the 2421 01:57:46,280 --> 01:57:53,199 alas one that's why I 2422 01:57:49,280 --> 01:57:56,520 wanted initially to cover how we set up 2423 01:57:53,199 --> 01:57:59,079 such inline type annotation because 2424 01:57:56,520 --> 01:58:01,480 nothing is going to change as far as our 2425 01:57:59,079 --> 01:58:04,800 functionality including the optional 2426 01:58:01,480 --> 01:58:06,360 property and read only modifier the only 2427 01:58:04,800 --> 01:58:09,719 difference well now the code is going to 2428 01:58:06,360 --> 01:58:12,119 be in one place so if we ever want to 2429 01:58:09,719 --> 01:58:16,239 change something we will only need to do 2430 01:58:12,119 --> 01:58:17,920 that in one specific place and also our 2431 01:58:16,239 --> 01:58:20,480 code is going to be less clunky because 2432 01:58:17,920 --> 01:58:23,440 we'll just use that name and once I'm 2433 01:58:20,480 --> 01:58:25,840 done with my long and B Bing monologue 2434 01:58:23,440 --> 01:58:27,920 why don't we set it up and essentially 2435 01:58:25,840 --> 01:58:30,520 the syntax is following we want to go 2436 01:58:27,920 --> 01:58:34,679 with type that's the keyword now the 2437 01:58:30,520 --> 01:58:36,360 convention is to go with the uppercase 2438 01:58:34,679 --> 01:58:38,480 but you don't have to I mean you can 2439 01:58:36,360 --> 01:58:41,360 call this also like this but in my case 2440 01:58:38,480 --> 01:58:44,880 I will go with user and like I said 2441 01:58:41,360 --> 01:58:48,119 before the syntax is the same so I 2442 01:58:44,880 --> 01:58:51,599 simply want to take my first instance 2443 01:58:48,119 --> 01:58:52,880 and set it equal to my user so now all 2444 01:58:51,599 --> 01:58:56,560 throughout 2445 01:58:52,880 --> 01:58:59,400 my file document project whatever I can 2446 01:58:56,560 --> 01:59:02,639 use this user and this will mean the 2447 01:58:59,400 --> 01:59:05,400 same exact thing so in here I can go 2448 01:59:02,639 --> 01:59:08,480 with yeah John is going to be object and 2449 01:59:05,400 --> 01:59:10,719 a type is actually going to be user same 2450 01:59:08,480 --> 01:59:14,159 is going to be over here Susan is going 2451 01:59:10,719 --> 01:59:16,719 to be a user now when we're creating a 2452 01:59:14,159 --> 01:59:19,719 user what is the type well it's a user 2453 01:59:16,719 --> 01:59:22,400 type and you can probably already guess 2454 01:59:19,719 --> 01:59:26,000 same is going to be over here so we can 2455 01:59:22,400 --> 01:59:30,880 go with user and now me question which 2456 01:59:26,000 --> 01:59:33,480 one is easier to manage and 2457 01:59:30,880 --> 01:59:36,560 read and if you said the second one 2458 01:59:33,480 --> 01:59:38,400 you're absolutely correct so I can mess 2459 01:59:36,560 --> 01:59:40,960 with you and say that you know what no 2460 01:59:38,400 --> 01:59:43,599 actually this one was easier to work 2461 01:59:40,960 --> 01:59:46,679 with but in reality we can clearly see 2462 01:59:43,599 --> 01:59:49,239 that this is definitely easier to work 2463 01:59:46,679 --> 01:59:51,840 with and I'm skipping a little bit ahead 2464 01:59:49,239 --> 01:59:54,040 but we can actually also export this one 2465 01:59:51,840 --> 01:59:56,960 as well as well so it's definitely an 2466 01:59:54,040 --> 01:59:58,800 option so let's say we set it up in one 2467 01:59:56,960 --> 02:00:01,840 file and then if we ever want to use 2468 01:59:58,800 --> 02:00:04,719 this type alas somewhere else we can 2469 02:00:01,840 --> 02:00:07,880 nicely export it import it in the file 2470 02:00:04,719 --> 02:00:10,239 and we're pretty much good to go okay um 2471 02:00:07,880 --> 02:00:12,679 before we move on and start working on 2472 02:00:10,239 --> 02:00:16,199 our next challenge I also want to 2473 02:00:12,679 --> 02:00:20,119 quickly showcase that we're not limited 2474 02:00:16,199 --> 02:00:21,719 to using type Alias for object so it's 2475 02:00:20,119 --> 02:00:23,360 definitely an option it's definitely 2476 02:00:21,719 --> 02:00:26,000 something thing we're going to do but 2477 02:00:23,360 --> 02:00:29,280 you can also do it for our previous 2478 02:00:26,000 --> 02:00:31,920 examples as well for example I can go 2479 02:00:29,280 --> 02:00:34,880 here with type I'm going to call this 2480 02:00:31,920 --> 02:00:38,040 string or number and I'll set it equal 2481 02:00:34,880 --> 02:00:42,119 to string then use the union type and 2482 02:00:38,040 --> 02:00:44,599 set it equal to a number so now anywhere 2483 02:00:42,119 --> 02:00:47,320 in my project where I want to use the 2484 02:00:44,599 --> 02:00:51,159 union string or number I can use my type 2485 02:00:47,320 --> 02:00:53,679 Alias instead of this hardcoded value so 2486 02:00:51,159 --> 02:00:56,639 let's say that there's a value this one 2487 02:00:53,679 --> 02:00:59,280 will have a type string or number and 2488 02:00:56,639 --> 02:01:03,719 then we can go with value hello and also 2489 02:00:59,280 --> 02:01:05,360 we can set the value equal to 1 two 3 2490 02:01:03,719 --> 02:01:06,880 now is this something you're going to 2491 02:01:05,360 --> 02:01:10,599 see 2492 02:01:06,880 --> 02:01:13,040 often uh most likely not but still I 2493 02:01:10,599 --> 02:01:16,360 definitely want to make sure that you 2494 02:01:13,040 --> 02:01:19,520 know that it is a possibility now 2495 02:01:16,360 --> 02:01:22,480 something that you will see quite often 2496 02:01:19,520 --> 02:01:25,280 at least in my code is this I'm going to 2497 02:01:22,480 --> 02:01:27,760 set up an alas but in this case I'm 2498 02:01:25,280 --> 02:01:30,079 doing that for the literal type so I'm 2499 02:01:27,760 --> 02:01:33,760 going to go with light or dark so this 2500 02:01:30,079 --> 02:01:37,639 will be my tee and those are the values 2501 02:01:33,760 --> 02:01:39,760 so it can be only light or dark and now 2502 02:01:37,639 --> 02:01:43,920 if I have a theme 2503 02:01:39,760 --> 02:01:46,960 variable I can go type theme then let me 2504 02:01:43,920 --> 02:01:48,840 just change the values here notice how I 2505 02:01:46,960 --> 02:01:51,360 right away have only these two options 2506 02:01:48,840 --> 02:01:53,040 something I have discussed already quite 2507 02:01:51,360 --> 02:01:54,520 a few times 2508 02:01:53,040 --> 02:01:57,480 and in this case I'm going to go with 2509 02:01:54,520 --> 02:02:00,320 light and what's also super super 2510 02:01:57,480 --> 02:02:03,760 awesome that I can use the same theme 2511 02:02:00,320 --> 02:02:05,560 one in the function so if I go here with 2512 02:02:03,760 --> 02:02:08,079 set 2513 02:02:05,560 --> 02:02:12,560 theme it's going to be looking for a 2514 02:02:08,079 --> 02:02:14,320 parameter by the name of T and the type 2515 02:02:12,560 --> 02:02:17,480 over here is actually going to be a 2516 02:02:14,320 --> 02:02:20,079 theme and now I want to set my theme 2517 02:02:17,480 --> 02:02:23,840 variable equal to 2518 02:02:20,079 --> 02:02:27,000 whatever parameter gets passed in so now 2519 02:02:23,840 --> 02:02:30,560 when we invoke the theme same deal over 2520 02:02:27,000 --> 02:02:34,159 here notice how we can only use these 2521 02:02:30,560 --> 02:02:37,920 two values again super super helpful 2522 02:02:34,159 --> 02:02:40,360 especially as your project grows bigger 2523 02:02:37,920 --> 02:02:44,280 so we can nicely set up this Alias and 2524 02:02:40,360 --> 02:02:48,079 then use it when we set up the property 2525 02:02:44,280 --> 02:02:50,400 or when we pass some parameter in the 2526 02:02:48,079 --> 02:02:52,360 function and up next let's work on 2527 02:02:50,400 --> 02:02:54,440 another challenge and and during the 2528 02:02:52,360 --> 02:02:56,199 challenge we'll cover a few interesting 2529 02:02:54,440 --> 02:02:59,280 things we'll cover how we can set up 2530 02:02:56,199 --> 02:03:02,599 type Alias with the array how we can set 2531 02:02:59,280 --> 02:03:05,719 up type alas with type Union and how to 2532 02:03:02,599 --> 02:03:09,040 set up a typ guard if we're working with 2533 02:03:05,719 --> 02:03:11,440 the object so here's the challenge we 2534 02:03:09,040 --> 02:03:14,239 want to set up two types one is going to 2535 02:03:11,440 --> 02:03:17,560 be the employee type and second one will 2536 02:03:14,239 --> 02:03:21,639 be a manager type now for employee 2537 02:03:17,560 --> 02:03:24,800 will'll set up two instances and then I 2538 02:03:21,639 --> 02:03:27,360 guess manager will set up only one now 2539 02:03:24,800 --> 02:03:30,800 the key difference is that for a manager 2540 02:03:27,360 --> 02:03:34,079 there's going to be employees property 2541 02:03:30,800 --> 02:03:38,239 and it's actually going to be an array 2542 02:03:34,079 --> 02:03:41,400 of employee types so this is where we'll 2543 02:03:38,239 --> 02:03:44,040 combine the Alias with the array and 2544 02:03:41,400 --> 02:03:48,599 then we want to set up a function which 2545 02:03:44,040 --> 02:03:51,440 actually is going to accept both types 2546 02:03:48,599 --> 02:03:52,760 whether that is the employee or the 2547 02:03:51,440 --> 02:03:55,559 manager 2548 02:03:52,760 --> 02:03:57,719 and if the instance is the manager then 2549 02:03:55,559 --> 02:04:00,320 we just want to showcase how many 2550 02:03:57,719 --> 02:04:02,520 employees they have and if it's not the 2551 02:04:00,320 --> 02:04:05,639 case then we'll just log in which 2552 02:04:02,520 --> 02:04:07,159 department the employee is working so 2553 02:04:05,639 --> 02:04:10,159 let's try to set this one 2554 02:04:07,159 --> 02:04:12,880 up I'm going to go with my alas first 2555 02:04:10,159 --> 02:04:15,199 and I'm looking for 2556 02:04:12,880 --> 02:04:18,280 employee so this one will set it equal 2557 02:04:15,199 --> 02:04:20,800 to an object there's going to be an ID 2558 02:04:18,280 --> 02:04:23,400 property which is a number then we'll go 2559 02:04:20,800 --> 02:04:26,559 with name we'll set that one equal to a 2560 02:04:23,400 --> 02:04:29,119 string and then also there's going to be 2561 02:04:26,559 --> 02:04:32,800 a department and for some 2562 02:04:29,119 --> 02:04:35,000 reason I have a hinch that this is not 2563 02:04:32,800 --> 02:04:37,079 correct typing so let me fix that so 2564 02:04:35,000 --> 02:04:39,760 we're looking over here for Department 2565 02:04:37,079 --> 02:04:42,360 which is a string 2566 02:04:39,760 --> 02:04:43,400 now there's also going to be an alias 2567 02:04:42,360 --> 02:04:48,880 for 2568 02:04:43,400 --> 02:04:52,079 manager for this one same deal ID number 2569 02:04:48,880 --> 02:04:53,840 name String so let me just copy them in 2570 02:04:52,079 --> 02:04:55,320 order to make it faster and now I want 2571 02:04:53,840 --> 02:04:58,719 to set up the 2572 02:04:55,320 --> 02:05:01,079 employees which is going to be an array 2573 02:04:58,719 --> 02:05:04,960 but an array of what it's not going to 2574 02:05:01,079 --> 02:05:06,400 be an array of strings or numbers and if 2575 02:05:04,960 --> 02:05:08,679 you remember in one of the previous 2576 02:05:06,400 --> 02:05:12,000 examples we actually covered that we can 2577 02:05:08,679 --> 02:05:14,639 set up type to be an object only in this 2578 02:05:12,000 --> 02:05:16,719 case we already have the type Alias for 2579 02:05:14,639 --> 02:05:18,880 that so I can simply say yeah it's going 2580 02:05:16,719 --> 02:05:22,960 to be an array but it's going to be an 2581 02:05:18,880 --> 02:05:24,400 array of employees okay good now let's 2582 02:05:22,960 --> 02:05:26,719 set up those instances and then we'll 2583 02:05:24,400 --> 02:05:29,960 work on a function so for starters 2584 02:05:26,719 --> 02:05:32,040 there's going to be first employee we'll 2585 02:05:29,960 --> 02:05:34,199 right away set up the type just so we 2586 02:05:32,040 --> 02:05:36,239 don't mess up anything 2587 02:05:34,199 --> 02:05:41,040 ed1 then 2588 02:05:36,239 --> 02:05:43,639 name let's set it up and also let's come 2589 02:05:41,040 --> 02:05:46,199 up with the department and in my case 2590 02:05:43,639 --> 02:05:48,440 I'm going to go with sales let me copy 2591 02:05:46,199 --> 02:05:52,360 and paste now I want to change the 2592 02:05:48,440 --> 02:05:55,800 variable name so in here we'll set up as 2593 02:05:52,360 --> 02:05:59,760 Steve same deal over here and then as 2594 02:05:55,800 --> 02:06:03,239 far as the department let's go with HR 2595 02:05:59,760 --> 02:06:06,320 then let's set up our manager as well so 2596 02:06:03,239 --> 02:06:12,920 this will be Bob the type will be 2597 02:06:06,320 --> 02:06:16,079 manager ID one name Bob or Bobo even 2598 02:06:12,920 --> 02:06:19,079 better and then we'll go with employees 2599 02:06:16,079 --> 02:06:24,040 and this is our array correct so now I 2600 02:06:19,079 --> 02:06:26,639 want to grab Alice and deep so let's set 2601 02:06:24,040 --> 02:06:30,679 these two up and now let's invoke the 2602 02:06:26,639 --> 02:06:32,760 function the print staff details but of 2603 02:06:30,679 --> 02:06:36,760 course before I can invoke 2604 02:06:32,760 --> 02:06:38,800 it it would be useful if we would have 2605 02:06:36,760 --> 02:06:41,480 this function in the first place correct 2606 02:06:38,800 --> 02:06:45,559 so let me come up with the function I'm 2607 02:06:41,480 --> 02:06:48,920 going to call this print staff and then 2608 02:06:45,559 --> 02:06:50,480 details but here's the kicker how are we 2609 02:06:48,920 --> 02:06:52,760 going to pass this in because at the 2610 02:06:50,480 --> 02:06:55,760 moment we have two 2611 02:06:52,760 --> 02:06:57,559 types so are we going to pass in just 2612 02:06:55,760 --> 02:07:01,239 employee and then set up another 2613 02:06:57,559 --> 02:07:04,079 function for the manager or we can 2614 02:07:01,239 --> 02:07:06,880 somehow some way combine the two and the 2615 02:07:04,079 --> 02:07:09,840 answer is well we can use the type Union 2616 02:07:06,880 --> 02:07:13,559 so essentially I can go with type and 2617 02:07:09,840 --> 02:07:17,000 then staff and set it equal to employee 2618 02:07:13,559 --> 02:07:20,119 or it's going to be a manager and now 2619 02:07:17,000 --> 02:07:22,880 this is our type so now I can nicely go 2620 02:07:20,119 --> 02:07:25,360 to print staff detail details and say 2621 02:07:22,880 --> 02:07:27,400 okay there's going to be an object and 2622 02:07:25,360 --> 02:07:31,000 as far as the type actually going to be 2623 02:07:27,400 --> 02:07:33,239 equal to a stat now I do want to set up 2624 02:07:31,000 --> 02:07:36,199 the void over here since we're not going 2625 02:07:33,239 --> 02:07:39,920 to return anything and now another 2626 02:07:36,199 --> 02:07:45,280 million doll question well how we can 2627 02:07:39,920 --> 02:07:46,719 check whether the staff object is the 2628 02:07:45,280 --> 02:07:49,559 employee or a 2629 02:07:46,719 --> 02:07:52,760 manager because if it's a manager then 2630 02:07:49,559 --> 02:07:55,920 we do have an array but if it's the 2631 02:07:52,760 --> 02:07:59,239 employee that's not the case and we 2632 02:07:55,920 --> 02:08:01,320 cannot go with well is type off equal to 2633 02:07:59,239 --> 02:08:04,679 an object well both of them are equal to 2634 02:08:01,320 --> 02:08:06,559 an object so that's not going to make 2635 02:08:04,679 --> 02:08:09,520 much difference here's what we want to 2636 02:08:06,559 --> 02:08:12,960 do so we want to go with our condition 2637 02:08:09,520 --> 02:08:16,119 and then we need to pick a property 2638 02:08:12,960 --> 02:08:19,719 which is specific to the object so in 2639 02:08:16,119 --> 02:08:22,040 this case that's the employees correct 2640 02:08:19,719 --> 02:08:25,840 so we go here with the property and we 2641 02:08:22,040 --> 02:08:28,360 say in and then our object over here so 2642 02:08:25,840 --> 02:08:31,440 that's the staff if it's the case well 2643 02:08:28,360 --> 02:08:34,079 then we know that it's a manager if not 2644 02:08:31,440 --> 02:08:37,040 then we know it's a staff one so let's 2645 02:08:34,079 --> 02:08:41,360 go and log something here we're going to 2646 02:08:37,040 --> 02:08:42,920 go with staff name so let me access this 2647 02:08:41,360 --> 02:08:46,599 one 2648 02:08:42,920 --> 02:08:49,800 staff name and then we'll say is an 2649 02:08:46,599 --> 02:08:52,000 employee in the specific department so 2650 02:08:49,800 --> 02:08:54,639 in here we want to access again the 2651 02:08:52,000 --> 02:08:57,520 staff one and now we're looking for 2652 02:08:54,639 --> 02:08:59,920 Department again a property which is not 2653 02:08:57,520 --> 02:09:03,440 on a manager and again typescript is 2654 02:08:59,920 --> 02:09:05,119 smart it knows that if there's an 2655 02:09:03,440 --> 02:09:07,719 employees this is going to be a manager 2656 02:09:05,119 --> 02:09:10,960 if not well that's the staff then and I 2657 02:09:07,719 --> 02:09:13,000 can access the department so let me add 2658 02:09:10,960 --> 02:09:14,840 this one and now I want to just grab 2659 02:09:13,000 --> 02:09:18,199 this console log and we'll just change a 2660 02:09:14,840 --> 02:09:20,719 few things around still going to be a 2661 02:09:18,199 --> 02:09:23,119 console log but we're going to go with 2662 02:09:20,719 --> 02:09:26,199 manager and notice how right away 2663 02:09:23,119 --> 02:09:28,719 typescript is correcting us essentially 2664 02:09:26,199 --> 02:09:31,320 it says well department does not exist 2665 02:09:28,719 --> 02:09:34,040 on type manager so now what do we want 2666 02:09:31,320 --> 02:09:35,639 to do well we'll remove this we're going 2667 02:09:34,040 --> 02:09:39,119 to go with employees and we're looking 2668 02:09:35,639 --> 02:09:43,199 for length and let's just type 2669 02:09:39,119 --> 02:09:46,239 employees okay good let's try it out 2670 02:09:43,199 --> 02:09:48,040 going to navigate to the bottom and 2671 02:09:46,239 --> 02:09:51,440 we're going to go with print staff 2672 02:09:48,040 --> 02:09:54,000 details let's pass in the first one and 2673 02:09:51,440 --> 02:09:55,840 and let's also pass in the Bob once we 2674 02:09:54,000 --> 02:09:58,800 navigate to the browser we see that 2675 02:09:55,840 --> 02:10:00,960 Alice is the employee and Bob is the 2676 02:09:58,800 --> 02:10:02,960 manager so this is how we can combine 2677 02:10:00,960 --> 02:10:07,199 multiple features this is how we can set 2678 02:10:02,960 --> 02:10:09,960 up type Alias with the type Union this 2679 02:10:07,199 --> 02:10:13,199 is how we can start using type aliases 2680 02:10:09,960 --> 02:10:17,440 with the array and this is how we can 2681 02:10:13,199 --> 02:10:20,440 set up a typ guard if we are working 2682 02:10:17,440 --> 02:10:24,400 with the object uh next let's discuss 2683 02:10:20,440 --> 02:10:28,119 the intersection type in Union which is 2684 02:10:24,400 --> 02:10:32,119 a way for us to combine multiple types 2685 02:10:28,119 --> 02:10:37,159 into one let's imagine this scenario we 2686 02:10:32,119 --> 02:10:40,199 have a type book it is equal to a number 2687 02:10:37,159 --> 02:10:42,960 name is equal to a string and there's 2688 02:10:40,199 --> 02:10:46,119 also a price so we set up this number 2689 02:10:42,960 --> 02:10:49,360 over here and we're good to go then 2690 02:10:46,119 --> 02:10:53,159 let's create let's say two instances I'm 2691 02:10:49,360 --> 02:10:56,360 going to call this book one is equal to 2692 02:10:53,159 --> 02:11:00,199 an object and let's right away set up 2693 02:10:56,360 --> 02:11:02,960 our type our book type and then in here 2694 02:11:00,199 --> 02:11:06,400 let's provide some dummy values ID will 2695 02:11:02,960 --> 02:11:09,760 be equal to one name will be how to cook 2696 02:11:06,400 --> 02:11:11,880 a dragon because why not and also let's 2697 02:11:09,760 --> 02:11:14,559 set up some kind of price so in this 2698 02:11:11,880 --> 02:11:17,520 case it's going to be some random 2699 02:11:14,559 --> 02:11:20,760 15 then let me copy and 2700 02:11:17,520 --> 02:11:23,360 paste and instead of Po one this is 2701 02:11:20,760 --> 02:11:27,280 going to be b 2 now the type will stay 2702 02:11:23,360 --> 02:11:28,480 the same the ID will be two as far as 2703 02:11:27,280 --> 02:11:31,199 the name we're going to go with the 2704 02:11:28,480 --> 02:11:34,040 secret life of unicorns and we're going 2705 02:11:31,199 --> 02:11:36,920 to set the price equal to 2706 02:11:34,040 --> 02:11:40,000 18 so all this is good but what if I 2707 02:11:36,920 --> 02:11:42,239 have a discounted book where not only I 2708 02:11:40,000 --> 02:11:45,679 have these three properties but there's 2709 02:11:42,239 --> 02:11:47,639 also a discount so first let's set it up 2710 02:11:45,679 --> 02:11:50,079 and you know what I think we can speed 2711 02:11:47,639 --> 02:11:52,000 this up a little bit where it's not 2712 02:11:50,079 --> 02:11:54,079 going to be a book we're going to go 2713 02:11:52,000 --> 02:11:58,159 with 2714 02:11:54,079 --> 02:12:01,719 discounted book okay good now for now 2715 02:11:58,159 --> 02:12:03,679 type will be book but clearly we'll have 2716 02:12:01,719 --> 02:12:06,239 some issues I can right away tell you 2717 02:12:03,679 --> 02:12:08,000 that then let's come up with the name in 2718 02:12:06,239 --> 02:12:12,040 my case I'm going to go with this one 2719 02:12:08,000 --> 02:12:14,719 price will be equal to 25 and we also 2720 02:12:12,040 --> 02:12:19,079 want to add this discount property and 2721 02:12:14,719 --> 02:12:22,599 we'll set this one equal to 2722 02:12:19,079 --> 02:12:25,119 0.15 and notice the moment we do that 2723 02:12:22,599 --> 02:12:29,000 typescript complains why well because 2724 02:12:25,119 --> 02:12:31,400 it's not in our types so there is no 2725 02:12:29,000 --> 02:12:33,920 discon anywhere here so what's the 2726 02:12:31,400 --> 02:12:36,000 solution well we could create another 2727 02:12:33,920 --> 02:12:39,719 type correct we could just copy and 2728 02:12:36,000 --> 02:12:41,840 paste and set up over here a discounted 2729 02:12:39,719 --> 02:12:43,719 book type and then just add that 2730 02:12:41,840 --> 02:12:47,920 discount property it's definitely an 2731 02:12:43,719 --> 02:12:50,400 option or we can nicely utilize the 2732 02:12:47,920 --> 02:12:53,679 intersection type how's that going to 2733 02:12:50,400 --> 02:12:55,559 look like well we have multiple ways how 2734 02:12:53,679 --> 02:12:58,119 we can set this up you can definitely do 2735 02:12:55,559 --> 02:13:03,719 it over here in line where you go with 2736 02:12:58,119 --> 02:13:06,199 and operator and you just add what more 2737 02:13:03,719 --> 02:13:08,880 types you want to have for this 2738 02:13:06,199 --> 02:13:11,960 particular instance so in this case I 2739 02:13:08,880 --> 02:13:14,599 can go with discount and I can set it 2740 02:13:11,960 --> 02:13:16,599 equal to a number and notice typescript 2741 02:13:14,599 --> 02:13:18,800 doesn't complain anymore so now 2742 02:13:16,599 --> 02:13:21,559 essentially it uses all of the 2743 02:13:18,800 --> 02:13:24,719 properties from the book type already 2744 02:13:21,559 --> 02:13:27,639 but we just added nicely the discount as 2745 02:13:24,719 --> 02:13:30,719 well so that's one option or we can set 2746 02:13:27,639 --> 02:13:33,000 up another alas now the logic here is 2747 02:13:30,719 --> 02:13:37,760 not going to change so I can just cut it 2748 02:13:33,000 --> 02:13:40,040 out and go type then discounted book and 2749 02:13:37,760 --> 02:13:42,719 we're going to set it equal to this one 2750 02:13:40,040 --> 02:13:47,360 so now for my discounted book I can use 2751 02:13:42,719 --> 02:13:49,040 the discounted book type Alas and again 2752 02:13:47,360 --> 02:13:51,599 the difference with Union type it's 2753 02:13:49,040 --> 02:13:55,000 either going to be this type or the type 2754 02:13:51,599 --> 02:13:58,119 on the other side of the pipe however 2755 02:13:55,000 --> 02:14:00,159 with the intersection type now we 2756 02:13:58,119 --> 02:14:01,800 combine them now we say yeah it's going 2757 02:14:00,159 --> 02:14:04,559 to have all of the properties that we 2758 02:14:01,800 --> 02:14:07,360 have in the book type and also there's 2759 02:14:04,559 --> 02:14:10,119 going to be this discount one which is a 2760 02:14:07,360 --> 02:14:12,440 type number all right and at the end of 2761 02:14:10,119 --> 02:14:15,960 the type Alias section I quickly want to 2762 02:14:12,440 --> 02:14:19,639 mention that type Alias also supports 2763 02:14:15,960 --> 02:14:23,040 computed properties so we we don't 2764 02:14:19,639 --> 02:14:26,800 always have to hardore code for example 2765 02:14:23,040 --> 02:14:28,320 if I have a tiger object and eventually 2766 02:14:26,800 --> 02:14:31,840 I'll set up the type you know what for 2767 02:14:28,320 --> 02:14:33,960 now let's just set up the object in here 2768 02:14:31,840 --> 02:14:38,760 I can go with age and set it equal to 2769 02:14:33,960 --> 02:14:42,719 five so nothing new but we can also set 2770 02:14:38,760 --> 02:14:44,800 up a variable in there we'll have a 2771 02:14:42,719 --> 02:14:48,000 string and we'll actually use that 2772 02:14:44,800 --> 02:14:50,040 string as a property name so straight up 2773 02:14:48,000 --> 02:14:52,880 JavaScript let's say we're going to go 2774 02:14:50,040 --> 02:14:56,559 with const prop name and we're going to 2775 02:14:52,880 --> 02:15:00,639 set it equal to age and now instead of 2776 02:14:56,559 --> 02:15:03,239 hard coding I can just go with prop name 2777 02:15:00,639 --> 02:15:05,480 so if I'm going to change the string 2778 02:15:03,239 --> 02:15:08,480 value now my property is also going to 2779 02:15:05,480 --> 02:15:11,119 change over here in a tiger and we can 2780 02:15:08,480 --> 02:15:13,440 do the same thing in type Alias I can go 2781 02:15:11,119 --> 02:15:18,000 with type 2782 02:15:13,440 --> 02:15:19,960 animal let's set it equal to our object 2783 02:15:18,000 --> 02:15:21,960 and then instead of hard coding we're 2784 02:15:19,960 --> 02:15:25,280 going to go with prop name and set it 2785 02:15:21,960 --> 02:15:27,400 equal to a number I just need to follow 2786 02:15:25,280 --> 02:15:30,199 the proper syntax over here and now 2787 02:15:27,400 --> 02:15:32,800 let's set up the type for a tiger and 2788 02:15:30,199 --> 02:15:35,440 we're good to go so just like in regular 2789 02:15:32,800 --> 02:15:39,400 JavaScript we can use computed 2790 02:15:35,440 --> 02:15:42,280 properties in type alas okay and up next 2791 02:15:39,400 --> 02:15:45,920 let's cover interface type which is used 2792 02:15:42,280 --> 02:15:48,840 to describe the shape of an object as a 2793 02:15:45,920 --> 02:15:52,520 side note we are purposely covering 2794 02:15:48,840 --> 02:15:55,520 interface type right after type Alias 2795 02:15:52,520 --> 02:15:59,040 simply because they are very similar and 2796 02:15:55,520 --> 02:16:01,520 yes before you ask quite often you can 2797 02:15:59,040 --> 02:16:05,119 use either one of them and the result is 2798 02:16:01,520 --> 02:16:08,239 going to be the same now what do 2799 02:16:05,119 --> 02:16:11,960 programmers do when the same thing can 2800 02:16:08,239 --> 02:16:14,760 be achieved in multiple ways well we 2801 02:16:11,960 --> 02:16:17,040 argue about it on Twitter and try to 2802 02:16:14,760 --> 02:16:19,079 convince everyone else that our solution 2803 02:16:17,040 --> 02:16:22,239 is somehow some way 2804 02:16:19,079 --> 02:16:26,199 better why because apparently that's the 2805 02:16:22,239 --> 02:16:28,559 best use of our time and remember if all 2806 02:16:26,199 --> 02:16:31,520 the other arguments don't work you can 2807 02:16:28,559 --> 02:16:34,120 always say it's just a better practice 2808 02:16:31,520 --> 02:16:37,519 to do it apparently that one always 2809 02:16:34,120 --> 02:16:40,800 works jokes aside I do personally prefer 2810 02:16:37,519 --> 02:16:43,760 type Alias over interface and therefore 2811 02:16:40,800 --> 02:16:46,960 during the course most likely you'll see 2812 02:16:43,760 --> 02:16:49,319 type Alias syntax more often but just 2813 02:16:46,960 --> 02:16:52,120 like I mentioned before it really comes 2814 02:16:49,319 --> 02:16:55,840 down to your preference despite what yet 2815 02:16:52,120 --> 02:17:00,160 another Twitter Guru says okay hopefully 2816 02:16:55,840 --> 02:17:03,120 I did not scare you away with my joke SL 2817 02:17:00,160 --> 02:17:07,120 rant and if you're still here let's set 2818 02:17:03,120 --> 02:17:08,679 up a book object so I'm going to 2819 02:17:07,120 --> 02:17:10,760 navigate to my 2820 02:17:08,679 --> 02:17:13,399 tutorial I'm going to call this deep 2821 02:17:10,760 --> 02:17:17,200 work and you'll see a second why so 2822 02:17:13,399 --> 02:17:20,519 we're going to go here with deep work if 2823 02:17:17,200 --> 02:17:22,679 I only would know how to spell this is 2824 02:17:20,519 --> 02:17:25,040 going to be my object we're going to go 2825 02:17:22,679 --> 02:17:28,160 over here with this 2826 02:17:25,040 --> 02:17:30,840 ISBN which essentially is just used for 2827 02:17:28,160 --> 02:17:33,000 books as the ID so in my case I'm just 2828 02:17:30,840 --> 02:17:35,160 going to go with one two three then 2829 02:17:33,000 --> 02:17:37,120 we're going to come up with title I'm 2830 02:17:35,160 --> 02:17:39,760 going to call this deep work as a sign 2831 02:17:37,120 --> 02:17:44,599 of this is an awesome book again I'm by 2832 02:17:39,760 --> 02:17:46,240 any means not being sponsored or I'm not 2833 02:17:44,599 --> 02:17:49,399 trying to sell you this book but this is 2834 02:17:46,240 --> 02:17:52,960 an awesome book uh you won't regret 2835 02:17:49,399 --> 02:17:54,880 whatever dollar it costs so we're going 2836 02:17:52,960 --> 02:17:58,359 to go over here with author that is 2837 02:17:54,880 --> 02:18:01,080 going to be Cal 2838 02:17:58,359 --> 02:18:05,080 Newport awesome and then let's also set 2839 02:18:01,080 --> 02:18:08,679 up the genre and I believe it's 2840 02:18:05,080 --> 02:18:11,519 selfhelp self and we're looking for help 2841 02:18:08,679 --> 02:18:13,359 now I do need to add over here a comma 2842 02:18:11,519 --> 02:18:15,880 and we already know how to set up ttype 2843 02:18:13,359 --> 02:18:19,160 alas so probably this is something we're 2844 02:18:15,880 --> 02:18:21,359 going to set up at the very end but we 2845 02:18:19,160 --> 02:18:24,920 also can use the interface again this is 2846 02:18:21,359 --> 02:18:29,120 something that we use if we want to 2847 02:18:24,920 --> 02:18:31,760 describe the shape of the object now the 2848 02:18:29,120 --> 02:18:33,719 keyword is interface then we come up 2849 02:18:31,760 --> 02:18:35,639 with a name same deal lowercase 2850 02:18:33,719 --> 02:18:38,760 uppercase totally up to you in my case 2851 02:18:35,639 --> 02:18:40,800 I'm going to go with the uppercase and 2852 02:18:38,760 --> 02:18:44,280 then instead of the equal sign we just 2853 02:18:40,800 --> 02:18:47,760 want to set up over here the curlies and 2854 02:18:44,280 --> 02:18:50,920 then same deal we go with the property 2855 02:18:47,760 --> 02:18:54,639 name and then the type and I write away 2856 02:18:50,920 --> 02:18:57,240 want to mention that the 2857 02:18:54,639 --> 02:19:00,319 readon modifier which we covered with 2858 02:18:57,240 --> 02:19:03,719 type alas as well as the optional 2859 02:19:00,319 --> 02:19:06,439 property is also allowed and therefore 2860 02:19:03,719 --> 02:19:09,280 I'll go right away with read only then 2861 02:19:06,439 --> 02:19:12,040 I'll Target basically this book ID and 2862 02:19:09,280 --> 02:19:14,399 I'll set it equal to a number remember 2863 02:19:12,040 --> 02:19:16,840 if we'll try to modify this value later 2864 02:19:14,399 --> 02:19:19,359 it's not going to work then also let's 2865 02:19:16,840 --> 02:19:22,960 set up a string okay beautiful that's 2866 02:19:19,359 --> 02:19:24,599 going to be 40 title then the same deal 2867 02:19:22,960 --> 02:19:26,960 we're going to do with author we're 2868 02:19:24,599 --> 02:19:28,960 going to set it equal to a string and 2869 02:19:26,960 --> 02:19:31,559 the same is going to be for genre but 2870 02:19:28,960 --> 02:19:33,719 I'm going to set it as optional again 2871 02:19:31,559 --> 02:19:36,319 just so we get this one out of the way I 2872 02:19:33,719 --> 02:19:38,439 don't think we need to spend a separate 2873 02:19:36,319 --> 02:19:41,599 video on these two things since we have 2874 02:19:38,439 --> 02:19:44,479 covered them before and effectively if I 2875 02:19:41,599 --> 02:19:48,399 go here where is my deep work and if I 2876 02:19:44,479 --> 02:19:52,640 set it equal to my book I'm going to go 2877 02:19:48,399 --> 02:19:56,200 so if for some reason I go with a deep 2878 02:19:52,640 --> 02:19:58,439 work and try to modify this property 2879 02:19:56,200 --> 02:20:01,399 which is read only let's say I'm going 2880 02:19:58,439 --> 02:20:03,800 to go with some value notice typescript 2881 02:20:01,399 --> 02:20:07,120 is going to complain same deal is going 2882 02:20:03,800 --> 02:20:09,479 to be if I'll try to add property that 2883 02:20:07,120 --> 02:20:12,160 doesn't exist let's say price and I'm 2884 02:20:09,479 --> 02:20:15,920 going to set it equal to 2885 02:20:12,160 --> 02:20:19,240 800 that is not going to work and then 2886 02:20:15,920 --> 02:20:22,280 let's say for genre since it's optional 2887 02:20:19,240 --> 02:20:24,560 property I can just remove it basically 2888 02:20:22,280 --> 02:20:27,800 comment out and my code is still going 2889 02:20:24,560 --> 02:20:31,880 to work and if you enjoy working on the 2890 02:20:27,800 --> 02:20:34,960 challenges you can set up a type Alias 2891 02:20:31,880 --> 02:20:38,479 for this deep work object and you'll see 2892 02:20:34,960 --> 02:20:40,960 that in this scenario you can use either 2893 02:20:38,479 --> 02:20:44,280 of those approaches and the result is 2894 02:20:40,960 --> 02:20:47,439 going to be exactly the same okay and up 2895 02:20:44,280 --> 02:20:50,399 next let's discuss methods in 2896 02:20:47,439 --> 02:20:54,359 interfaces and we're going to spend few 2897 02:20:50,399 --> 02:20:58,160 video videos on it just because it's not 2898 02:20:54,359 --> 02:21:00,520 in my opinion a complex topic but it can 2899 02:20:58,160 --> 02:21:04,640 be a little bit confusing because you 2900 02:21:00,520 --> 02:21:07,479 can set up methods as properties as well 2901 02:21:04,640 --> 02:21:09,760 so essentially you can mix and match and 2902 02:21:07,479 --> 02:21:13,359 at that point it might look a little 2903 02:21:09,760 --> 02:21:16,160 confusing so start with just straight up 2904 02:21:13,359 --> 02:21:19,840 approach which is my preference and then 2905 02:21:16,160 --> 02:21:23,160 I'll show you the alternative code that 2906 02:21:19,840 --> 02:21:26,439 you might see somewhere in the wild so 2907 02:21:23,160 --> 02:21:30,479 effectively if this book has for example 2908 02:21:26,439 --> 02:21:32,800 a method of print author I just go 2909 02:21:30,479 --> 02:21:35,520 directly to my interface in this case 2910 02:21:32,800 --> 02:21:37,760 I'm going to add a comment and I'm going 2911 02:21:35,520 --> 02:21:40,359 to come up with that method now please 2912 02:21:37,760 --> 02:21:42,399 keep in mind here we're not typing any 2913 02:21:40,359 --> 02:21:46,840 logic I'm just saying yes there's going 2914 02:21:42,399 --> 02:21:49,560 to be a print method or you know what 2915 02:21:46,840 --> 02:21:51,200 should be print author for some reason I 2916 02:21:49,560 --> 02:21:52,359 went with print method 2917 02:21:51,200 --> 02:21:55,479 it's not going to look for any 2918 02:21:52,359 --> 02:21:57,040 parameters and then as far as the return 2919 02:21:55,479 --> 02:21:59,280 it's not going to return anything it's 2920 02:21:57,040 --> 02:22:02,319 just going to log so the moment I do 2921 02:21:59,280 --> 02:22:04,399 that now notice I have typescript 2922 02:22:02,319 --> 02:22:07,720 complaining because I haven't set up 2923 02:22:04,399 --> 02:22:09,560 that method over here you know what just 2924 02:22:07,720 --> 02:22:13,040 so I have 2925 02:22:09,560 --> 02:22:15,359 less code I'm just going to remove those 2926 02:22:13,040 --> 02:22:18,520 comments over here so now let's set up 2927 02:22:15,359 --> 02:22:21,600 that method again this is going to be my 2928 02:22:18,520 --> 02:22:24,760 Approach where I go print auth 2929 02:22:21,600 --> 02:22:27,319 setup over here my method and in this 2930 02:22:24,760 --> 02:22:29,359 case I'm not looking for any prams we'll 2931 02:22:27,319 --> 02:22:33,240 cover that in a second and then we just 2932 02:22:29,359 --> 02:22:37,640 go with log and then since I'm not using 2933 02:22:33,240 --> 02:22:39,920 the arrow function syntax I can access 2934 02:22:37,640 --> 02:22:43,160 any of these properties over here by 2935 02:22:39,920 --> 02:22:45,640 using this again we're going to cover 2936 02:22:43,160 --> 02:22:48,240 the arrow syntax approach a little later 2937 02:22:45,640 --> 02:22:51,479 for now we just want to log the author 2938 02:22:48,240 --> 02:22:54,319 that's it so now let's go go right after 2939 02:22:51,479 --> 02:22:57,720 the object we're going to go deep work 2940 02:22:54,319 --> 02:23:00,640 then we're going to invoke print author 2941 02:22:57,720 --> 02:23:03,399 and if everything is correct we should 2942 02:23:00,640 --> 02:23:06,479 see in a console the author so let me 2943 02:23:03,399 --> 02:23:11,160 take a look at the browser and yep I 2944 02:23:06,479 --> 02:23:14,840 have my Cal Newport so my logic works 2945 02:23:11,160 --> 02:23:17,720 now what is the syntax if there's going 2946 02:23:14,840 --> 02:23:19,399 to be a parameter well let's try it out 2947 02:23:17,720 --> 02:23:21,920 in this case I'm going to go with print 2948 02:23:19,399 --> 02:23:24,800 title and I'm just going add some 2949 02:23:21,920 --> 02:23:26,520 message after the title I'll say this 2950 02:23:24,800 --> 02:23:30,040 book is awesome or something along those 2951 02:23:26,520 --> 02:23:34,000 lines so let's go here with 2952 02:23:30,040 --> 02:23:37,439 title we set up the parameter as a sign 2953 02:23:34,000 --> 02:23:40,439 note this name doesn't need to match 2954 02:23:37,439 --> 02:23:43,760 whatever we set up over here in the 2955 02:23:40,439 --> 02:23:45,000 instance so in here I'm just saying yes 2956 02:23:43,760 --> 02:23:47,200 there's going to be some kind of 2957 02:23:45,000 --> 02:23:50,520 parameter and I'm going to reference 2958 02:23:47,200 --> 02:23:53,800 this as message now type is going to to 2959 02:23:50,520 --> 02:23:56,399 be string and Just for kicks let's also 2960 02:23:53,800 --> 02:23:58,399 return from this function again we're 2961 02:23:56,399 --> 02:24:00,160 not setting up any kind of logic over 2962 02:23:58,399 --> 02:24:02,840 here I'm just saying that there's going 2963 02:24:00,160 --> 02:24:06,080 to be a method by the name of print 2964 02:24:02,840 --> 02:24:09,080 title so now we want to scroll down and 2965 02:24:06,080 --> 02:24:11,840 same deal we want to set up our function 2966 02:24:09,080 --> 02:24:13,120 so I'm going to go with print title and 2967 02:24:11,840 --> 02:24:15,120 I'm just going to call this value just 2968 02:24:13,120 --> 02:24:18,720 to Showcase again they don't need to 2969 02:24:15,120 --> 02:24:21,800 match now is my preference to set them 2970 02:24:18,720 --> 02:24:24,240 up the same yes but again that is just 2971 02:24:21,800 --> 02:24:27,279 my preference and then inside of the 2972 02:24:24,240 --> 02:24:29,840 function same deal in this case I do 2973 02:24:27,279 --> 02:24:32,319 want to return and notice how typescript 2974 02:24:29,840 --> 02:24:34,319 essentially is complaining that hey you 2975 02:24:32,319 --> 02:24:36,840 said that it's going to be a string over 2976 02:24:34,319 --> 02:24:39,080 here and at the moment it's still void 2977 02:24:36,840 --> 02:24:42,200 what's up with that so let me just 2978 02:24:39,080 --> 02:24:44,439 access again this dot since we're not 2979 02:24:42,200 --> 02:24:46,920 using the arrow function then we can 2980 02:24:44,439 --> 02:24:50,560 access nicely title and I'm going to 2981 02:24:46,920 --> 02:24:52,479 provide over here whatever message is 2982 02:24:50,560 --> 02:24:55,120 going to be passed in and then let's 2983 02:24:52,479 --> 02:24:56,920 keep on moving I guess right after the 2984 02:24:55,120 --> 02:24:59,279 print 2985 02:24:56,920 --> 02:25:01,160 author either we can log it right away 2986 02:24:59,279 --> 02:25:03,840 or in my case I think I'm just going to 2987 02:25:01,160 --> 02:25:08,720 store it in a result so let's go deep 2988 02:25:03,840 --> 02:25:10,800 work then print uh nope it's not the one 2989 02:25:08,720 --> 02:25:11,920 we want to go with print title and then 2990 02:25:10,800 --> 02:25:15,080 let's say 2991 02:25:11,920 --> 02:25:17,880 is awesome book so this will return a 2992 02:25:15,080 --> 02:25:21,920 string so what do we do with string 2993 02:25:17,880 --> 02:25:25,520 value well we log it and yes it looks 2994 02:25:21,920 --> 02:25:29,160 like everything is correct but text is 2995 02:25:25,520 --> 02:25:33,359 not so let me set it up properly or here 2996 02:25:29,160 --> 02:25:34,880 again if we have a method in here we 2997 02:25:33,359 --> 02:25:37,359 just set up the 2998 02:25:34,880 --> 02:25:40,200 blueprint so what is going to be the 2999 02:25:37,359 --> 02:25:43,399 method name whether we're going to 3000 02:25:40,200 --> 02:25:46,479 accept any parameters if we do then it's 3001 02:25:43,399 --> 02:25:48,160 useful to set up right away a type and 3002 02:25:46,479 --> 02:25:51,520 also whether we're going to return 3003 02:25:48,160 --> 02:25:53,880 something from this function or or no 3004 02:25:51,520 --> 02:25:57,920 and then inside of the instance we set 3005 02:25:53,880 --> 02:26:00,840 up the method and we just need to follow 3006 02:25:57,920 --> 02:26:02,160 our blueprint so if we're not returning 3007 02:26:00,840 --> 02:26:04,040 anything from this 3008 02:26:02,160 --> 02:26:05,720 function we just set up the 3009 02:26:04,040 --> 02:26:08,960 functionality if we are returning we 3010 02:26:05,720 --> 02:26:12,479 need to make sure that the types match 3011 02:26:08,960 --> 02:26:16,399 and with this syntax we can access any 3012 02:26:12,479 --> 02:26:18,800 Properties by using this keyword okay 3013 02:26:16,399 --> 02:26:21,880 and up next let's cover an alternative 3014 02:26:18,800 --> 02:26:25,359 way how we can set up method me in 3015 02:26:21,880 --> 02:26:28,200 interfaces now let me stress something 3016 02:26:25,359 --> 02:26:29,880 technically this video is optional I 3017 02:26:28,200 --> 02:26:32,800 just wanted to cover this because you 3018 02:26:29,880 --> 02:26:36,279 might encounter such syntax somewhere in 3019 02:26:32,800 --> 02:26:38,040 the wild but I personally tend to stick 3020 02:26:36,279 --> 02:26:41,120 with the approach we covered in the 3021 02:26:38,040 --> 02:26:42,439 previous video simply because I find it 3022 02:26:41,120 --> 02:26:44,840 way more 3023 02:26:42,439 --> 02:26:48,479 straightforward alternatively we can set 3024 02:26:44,840 --> 02:26:50,520 up the method in the interface as a 3025 02:26:48,479 --> 02:26:53,680 property how's that going to look look 3026 02:26:50,520 --> 02:26:56,359 like well we can go here with print 3027 02:26:53,680 --> 02:26:58,479 something and again the example is going 3028 02:26:56,359 --> 02:27:01,120 to be pretty straightforward and 3029 02:26:58,479 --> 02:27:03,200 essentially we set it here equal to a 3030 02:27:01,120 --> 02:27:05,840 function but please keep in mind this is 3031 02:27:03,200 --> 02:27:07,840 a syntax we're not setting up over here 3032 02:27:05,840 --> 02:27:10,920 the arrow function this is just the 3033 02:27:07,840 --> 02:27:13,880 syntax that we're using so we say that 3034 02:27:10,920 --> 02:27:17,800 it's going to return a number and then 3035 02:27:13,880 --> 02:27:20,399 we set it equal to some value and pass 3036 02:27:17,800 --> 02:27:24,319 in the number and now with this proper 3037 02:27:20,399 --> 02:27:27,840 in place we want to go to a deep work 3038 02:27:24,319 --> 02:27:31,520 object and here we have quite a few 3039 02:27:27,840 --> 02:27:33,040 options to set up this function so I'm 3040 02:27:31,520 --> 02:27:36,520 going to add these comments I'm going to 3041 02:27:33,040 --> 02:27:40,240 say First Option then there's going to 3042 02:27:36,520 --> 02:27:42,319 be a second option and I believe there's 3043 02:27:40,240 --> 02:27:45,520 going to be a third one as well but 3044 02:27:42,319 --> 02:27:49,399 we'll see most likely I will improvise 3045 02:27:45,520 --> 02:27:50,640 as we go so first option well we can use 3046 02:27:49,399 --> 02:27:53,040 the good old 3047 02:27:50,640 --> 02:27:54,640 function keyword so I'm going to go with 3048 02:27:53,040 --> 02:27:56,840 print something that's going to be my 3049 02:27:54,640 --> 02:27:59,600 property right now then I go with 3050 02:27:56,840 --> 02:28:01,640 function same deal it's looking for some 3051 02:27:59,600 --> 02:28:05,359 value now what is going to be the value 3052 02:28:01,640 --> 02:28:08,200 that will be a number correct and then 3053 02:28:05,359 --> 02:28:10,240 as far as the function body just so I 3054 02:28:08,200 --> 02:28:13,000 return a number essentially I'm just 3055 02:28:10,240 --> 02:28:16,200 going to go with some value now let me 3056 02:28:13,000 --> 02:28:18,399 scroll down a little bit I mean most 3057 02:28:16,200 --> 02:28:20,880 likely we can just remove these values 3058 02:28:18,399 --> 02:28:24,160 just so our console is less busy and I 3059 02:28:20,880 --> 02:28:25,279 can go with my deep work then let's go 3060 02:28:24,160 --> 02:28:28,720 with print 3061 02:28:25,279 --> 02:28:30,000 something we want to pass in some number 3062 02:28:28,720 --> 02:28:33,479 why not 3063 02:28:30,000 --> 02:28:37,279 34 and then let me set up the log and 3064 02:28:33,479 --> 02:28:40,720 let me move this sucker inside then let 3065 02:28:37,279 --> 02:28:45,040 me remove my semicolon let's navigate 3066 02:28:40,720 --> 02:28:47,439 here and notice I have 34 so again 3067 02:28:45,040 --> 02:28:51,160 instead of setting up the straight up 3068 02:28:47,439 --> 02:28:52,840 method now I set up the proper 3069 02:28:51,160 --> 02:28:55,399 which is equal to a 3070 02:28:52,840 --> 02:28:58,200 function in here if I have some 3071 02:28:55,399 --> 02:29:00,760 parameters then I set up the type and I 3072 02:28:58,200 --> 02:29:02,920 also say well what is this function 3073 02:29:00,760 --> 02:29:04,359 returning again you don't have to return 3074 02:29:02,920 --> 02:29:06,840 something from this function just 3075 02:29:04,359 --> 02:29:08,560 because it's a property right now so 3076 02:29:06,840 --> 02:29:11,359 that is going to be our first option 3077 02:29:08,560 --> 02:29:13,319 okay let's common this one out and our 3078 02:29:11,359 --> 02:29:16,319 second option is going to be Arrow 3079 02:29:13,319 --> 02:29:17,920 function so we can go here with print 3080 02:29:16,319 --> 02:29:20,720 something 3081 02:29:17,920 --> 02:29:24,080 remember we want to access the some 3082 02:29:20,720 --> 02:29:26,399 value that is going to be our parameter 3083 02:29:24,080 --> 02:29:28,399 and then as far as the logic I'm going 3084 02:29:26,399 --> 02:29:30,880 to go with some value now there's a 3085 02:29:28,399 --> 02:29:33,520 reason why I'm setting up the curlies 3086 02:29:30,880 --> 02:29:36,760 I'm fully aware that we can set up the 3087 02:29:33,520 --> 02:29:39,160 implicit return the reason for that is 3088 02:29:36,760 --> 02:29:42,359 because I wanted to Showcase 3089 02:29:39,160 --> 02:29:46,080 that there's a gacha with this and arrow 3090 02:29:42,359 --> 02:29:48,600 functions so Arrow function is going to 3091 02:29:46,080 --> 02:29:50,040 capture the global this please keep that 3092 02:29:48,600 --> 02:29:52,760 in mind if you're using Arrow function 3093 02:29:50,040 --> 02:29:55,600 functions unlike the good old regular 3094 02:29:52,760 --> 02:29:59,760 function where we right away have access 3095 02:29:55,600 --> 02:30:02,720 to this which is scoped to this instance 3096 02:29:59,760 --> 02:30:04,600 it's not going to be the case with the 3097 02:30:02,720 --> 02:30:06,760 arrow function so for example if you 3098 02:30:04,600 --> 02:30:09,160 want to access any of the properties in 3099 02:30:06,760 --> 02:30:11,680 my case I'm going to go with author 3100 02:30:09,160 --> 02:30:13,840 we'll have to go with deep work and then 3101 02:30:11,680 --> 02:30:16,600 the author now let's navigate over here 3102 02:30:13,840 --> 02:30:19,680 notice we have both we have the 34 and 3103 02:30:16,600 --> 02:30:24,000 we also have the author so this will be 3104 02:30:19,680 --> 02:30:25,120 our second option and I guess what was 3105 02:30:24,000 --> 02:30:27,920 somewhat 3106 02:30:25,120 --> 02:30:30,520 confusing is the fact that you can also 3107 02:30:27,920 --> 02:30:34,319 do another 3108 02:30:30,520 --> 02:30:37,160 way where I can go with print something 3109 02:30:34,319 --> 02:30:40,120 and effectively just use the syntax we 3110 02:30:37,160 --> 02:30:43,960 already used before so this also is 3111 02:30:40,120 --> 02:30:46,920 going to work and that's why again my 3112 02:30:43,960 --> 02:30:50,200 preference is just to use this approach 3113 02:30:46,920 --> 02:30:54,080 where I set up the method and I clearly 3114 02:30:50,200 --> 02:30:57,200 do the same thing inside of my instance 3115 02:30:54,080 --> 02:31:00,960 so this will also work so we can go here 3116 02:30:57,200 --> 02:31:03,000 with return and then some value print 3117 02:31:00,960 --> 02:31:07,160 something and notice once we navigate 3118 02:31:03,000 --> 02:31:10,160 back to the console this also works and 3119 02:31:07,160 --> 02:31:14,040 lastly I'm not trying to confuse you too 3120 02:31:10,160 --> 02:31:16,200 much but this also is going to work so 3121 02:31:14,040 --> 02:31:19,439 notice we have over here this print 3122 02:31:16,200 --> 02:31:22,560 author which is set up as a method but 3123 02:31:19,439 --> 02:31:25,960 we can set this one up as a property so 3124 02:31:22,560 --> 02:31:27,880 we can go here with print author now I 3125 02:31:25,960 --> 02:31:32,200 believe typescript is going to complain 3126 02:31:27,880 --> 02:31:34,920 so I'll uncomment the first one my 3127 02:31:32,200 --> 02:31:37,319 Approach I guess if I have a property 3128 02:31:34,920 --> 02:31:40,760 then we're going to go print 3129 02:31:37,319 --> 02:31:42,760 author and we will set this up as a 3130 02:31:40,760 --> 02:31:45,319 property and we'll set it equal to an 3131 02:31:42,760 --> 02:31:47,920 arrow function again not trying to 3132 02:31:45,319 --> 02:31:50,439 confuse you too much but just showing 3133 02:31:47,920 --> 02:31:52,319 you alternative syntax that you might 3134 02:31:50,439 --> 02:31:55,040 see somewhere again 3135 02:31:52,319 --> 02:31:58,600 remember we don't want to use this we 3136 02:31:55,040 --> 02:32:01,319 actually want to go with deep work and 3137 02:31:58,600 --> 02:32:03,760 then we're going to look for author now 3138 02:32:01,319 --> 02:32:06,920 tespt will complain since I already 3139 02:32:03,760 --> 02:32:09,080 have my first approach but again if you 3140 02:32:06,920 --> 02:32:12,120 navigate to the console you'll see that 3141 02:32:09,080 --> 02:32:14,600 the result is there now I'm not calling 3142 02:32:12,120 --> 02:32:17,439 print author that's the reason why I 3143 02:32:14,600 --> 02:32:20,640 don't have it let me go here let me 3144 02:32:17,439 --> 02:32:23,160 invoke it and you'll see that as far as 3145 02:32:20,640 --> 02:32:25,640 functionality it works again what's 3146 02:32:23,160 --> 02:32:29,080 confusing is that you can set the 3147 02:32:25,640 --> 02:32:30,960 property equal to a function so instead 3148 02:32:29,080 --> 02:32:33,359 of straight up method you set up a 3149 02:32:30,960 --> 02:32:36,279 property which is actually equal to a 3150 02:32:33,359 --> 02:32:39,319 function and then you can start doing 3151 02:32:36,279 --> 02:32:40,960 this mix and match where yeah it was a 3152 02:32:39,319 --> 02:32:43,240 property but then actually in here it's 3153 02:32:40,960 --> 02:32:46,800 going to be a method and then the other 3154 02:32:43,240 --> 02:32:50,319 way that's why my preference is to set 3155 02:32:46,800 --> 02:32:53,640 up methods with this syntax and and the 3156 02:32:50,319 --> 02:32:56,319 same in my instance just set up straight 3157 02:32:53,640 --> 02:32:58,840 up methods simply because in my opinion 3158 02:32:56,319 --> 02:33:01,640 it's just less confusing hopefully I 3159 02:32:58,840 --> 02:33:03,840 didn't lose too many of you and with 3160 02:33:01,640 --> 02:33:06,880 this in place now we can start working 3161 02:33:03,840 --> 02:33:10,120 on our next task okay and once we're 3162 02:33:06,880 --> 02:33:12,760 familiar with the interface fundamentals 3163 02:33:10,120 --> 02:33:16,080 let's work on a small challenge where we 3164 02:33:12,760 --> 02:33:18,760 want to set up a computer interface 3165 02:33:16,080 --> 02:33:21,600 inside of it we want to set up few 3166 02:33:18,760 --> 02:33:23,840 properties one is going to be read only 3167 02:33:21,600 --> 02:33:26,680 which is going to be for object then 3168 02:33:23,840 --> 02:33:31,240 brand equals to string Ram is going to 3169 02:33:26,680 --> 02:33:33,279 be number upgrade ram will be a 3170 02:33:31,240 --> 02:33:35,760 method and in there we're going to be 3171 02:33:33,279 --> 02:33:38,200 looking for number and we'll return a 3172 02:33:35,760 --> 02:33:41,080 number and also there's going to be a 3173 02:33:38,200 --> 02:33:44,439 storage property which is going to be 3174 02:33:41,080 --> 02:33:48,279 optional but the type is going to be 3175 02:33:44,439 --> 02:33:52,399 number also once we have the 3176 02:33:48,279 --> 02:33:54,399 interface we will set up the instance 3177 02:33:52,399 --> 02:33:59,439 and we'll provide the properties apart 3178 02:33:54,399 --> 02:34:04,760 from the optional one and then finally 3179 02:33:59,439 --> 02:34:08,600 we'll also invoke the upgrade ram method 3180 02:34:04,760 --> 02:34:12,120 so let's try it out let me set up my 3181 02:34:08,600 --> 02:34:14,880 interface name will be a 3182 02:34:12,120 --> 02:34:17,840 computer let's go line by line this one 3183 02:34:14,880 --> 02:34:23,240 will be read only so I'll set it equal 3184 02:34:17,840 --> 02:34:25,520 to my ID then brand equals to A String 3185 02:34:23,240 --> 02:34:30,040 after that let's go with the ram that 3186 02:34:25,520 --> 02:34:34,000 will be number okay then upgrade 3187 02:34:30,040 --> 02:34:38,319 ram this one will be a 3188 02:34:34,000 --> 02:34:40,600 method so we'll look for 3189 02:34:38,319 --> 02:34:43,520 increase and this actually is going to 3190 02:34:40,600 --> 02:34:46,720 be a number now from this function we 3191 02:34:43,520 --> 02:34:49,240 want to return a number and then also 3192 02:34:46,720 --> 02:34:51,760 let's set up the storage which will be 3193 02:34:49,240 --> 02:34:54,319 optional and we'll set it equal to a 3194 02:34:51,760 --> 02:34:56,760 number so now let's create our instance 3195 02:34:54,319 --> 02:35:00,840 in my case it's going to be a 3196 02:34:56,760 --> 02:35:02,399 laptop so set it equal to our object and 3197 02:35:00,840 --> 02:35:05,840 we right away want to set up our 3198 02:35:02,399 --> 02:35:08,359 interface type now let's pass in those 3199 02:35:05,840 --> 02:35:13,399 values there will be an 3200 02:35:08,359 --> 02:35:16,880 ID then a brand is going to be a random 3201 02:35:13,399 --> 02:35:21,760 brand after that we have a ram we'll set 3202 02:35:16,880 --> 02:35:25,200 it equal to 8 I guess 8 gigs and let's 3203 02:35:21,760 --> 02:35:27,439 also set up our method upgrade ram as 3204 02:35:25,200 --> 02:35:28,960 far as the parameter I'm going to go 3205 02:35:27,439 --> 02:35:31,160 with different name just so you can see 3206 02:35:28,960 --> 02:35:34,319 that for sure everything works even 3207 02:35:31,160 --> 02:35:37,439 though the names don't match and in here 3208 02:35:34,319 --> 02:35:41,319 let's just set up functionality where we 3209 02:35:37,439 --> 02:35:45,520 grab the current Ram value and we just 3210 02:35:41,319 --> 02:35:47,920 add the amount which is past in and then 3211 02:35:45,520 --> 02:35:51,040 since I want to return a number I'm just 3212 02:35:47,920 --> 02:35:52,399 going to go with this Dot and RAM this 3213 02:35:51,040 --> 02:35:54,960 is what we're 3214 02:35:52,399 --> 02:35:57,840 returning since the storage is optional 3215 02:35:54,960 --> 02:36:02,120 I'm not going to pass it in let's just 3216 02:35:57,840 --> 02:36:04,000 set up our instance then storage and now 3217 02:36:02,120 --> 02:36:07,600 I'm going to set it equal to 3218 02:36:04,000 --> 02:36:14,040 256 we can log it so let's go over here 3219 02:36:07,600 --> 02:36:19,000 with our laptop let's see yep I have my 3220 02:36:14,040 --> 02:36:21,600 object a brand RAM and everything up to 3221 02:36:19,000 --> 02:36:24,439 the upgrade RAM and also let's invoke 3222 02:36:21,600 --> 02:36:27,040 the upgrade ram let's pass in some value 3223 02:36:24,439 --> 02:36:29,439 and let's keep our console locked so 3224 02:36:27,040 --> 02:36:31,600 we're going to go over here with another 3225 02:36:29,439 --> 02:36:35,040 one I guess since we're returning the 3226 02:36:31,600 --> 02:36:38,160 value so let's go with laptop upgrade 3227 02:36:35,040 --> 02:36:40,880 ram let's pass in some kind of value in 3228 02:36:38,160 --> 02:36:43,160 my case I'm going to go with four and 3229 02:36:40,880 --> 02:36:45,279 then if everything is correct this is 3230 02:36:43,160 --> 02:36:49,160 what we're getting back from the method 3231 02:36:45,279 --> 02:36:52,880 and also our Ram instead of eight is 3232 02:36:49,160 --> 02:36:56,720 actually equal to 12 okay and up next 3233 02:36:52,880 --> 02:37:00,240 let's see how we can perform interface 3234 02:36:56,720 --> 02:37:02,880 merging how we can extend the interface 3235 02:37:00,240 --> 02:37:05,120 and then we'll work on another challenge 3236 02:37:02,880 --> 02:37:08,800 and during that challenge we'll see how 3237 02:37:05,120 --> 02:37:11,080 we can use typu with interface for 3238 02:37:08,800 --> 02:37:15,520 starters I want to create two interfaces 3239 02:37:11,080 --> 02:37:18,439 one for person and one for dog owner now 3240 02:37:15,520 --> 02:37:19,760 both of them will have two properties I 3241 02:37:18,439 --> 02:37:24,279 guess well one is going to be be a 3242 02:37:19,760 --> 02:37:25,439 method name and let's say details so 3243 02:37:24,279 --> 02:37:27,960 let's go with 3244 02:37:25,439 --> 02:37:30,160 interface I'm going to go with person 3245 02:37:27,960 --> 02:37:32,880 I'm looking for name I'll set it equal 3246 02:37:30,160 --> 02:37:37,600 to a string and also there's going to be 3247 02:37:32,880 --> 02:37:40,240 a get details method which will return a 3248 02:37:37,600 --> 02:37:44,359 string so that's my person one and I 3249 02:37:40,240 --> 02:37:47,840 also want to set up one for dog owner so 3250 02:37:44,359 --> 02:37:52,160 let's just go here dog 3251 02:37:47,840 --> 02:37:54,520 owner so that one will have a dog name I 3252 02:37:52,160 --> 02:37:56,840 guess that's going to be my property 3253 02:37:54,520 --> 02:37:58,680 name which is going to be string and 3254 02:37:56,840 --> 02:38:00,399 then instead of get details we're going 3255 02:37:58,680 --> 02:38:03,920 to go get 3256 02:38:00,399 --> 02:38:07,120 dog details let's invoke it and again 3257 02:38:03,920 --> 02:38:10,040 we're looking for a string over here 3258 02:38:07,120 --> 02:38:13,720 okay beautiful and first let's see how 3259 02:38:10,040 --> 02:38:16,240 we can merge the interfaces so once I 3260 02:38:13,720 --> 02:38:19,240 have my person one I want to create my 3261 02:38:16,240 --> 02:38:21,920 object I'm going to call this person and 3262 02:38:19,240 --> 02:38:24,720 the type is going to be equal to my 3263 02:38:21,920 --> 02:38:26,880 person interface now what are the two 3264 02:38:24,720 --> 02:38:30,240 values we need to provide first we need 3265 02:38:26,880 --> 02:38:32,960 to go with John and as I not I'm missing 3266 02:38:30,240 --> 02:38:36,760 here the equal sign and the second will 3267 02:38:32,960 --> 02:38:39,279 be the get details one so let's just go 3268 02:38:36,760 --> 02:38:41,840 with get 3269 02:38:39,279 --> 02:38:44,120 details that's the 3270 02:38:41,840 --> 02:38:46,240 function and we're going 3271 02:38:44,120 --> 02:38:48,880 to set up the 3272 02:38:46,240 --> 02:38:50,319 functionality with the template string 3273 02:38:48,880 --> 02:38:55,160 name 3274 02:38:50,319 --> 02:38:59,439 colon then let me access the this dot so 3275 02:38:55,160 --> 02:39:02,439 this do name and also let's add 3276 02:38:59,439 --> 02:39:05,279 age so this one will also be equal to 3277 02:39:02,439 --> 02:39:08,920 this Dot and then the 3278 02:39:05,279 --> 02:39:13,240 age now notice over here how we don't 3279 02:39:08,920 --> 02:39:17,000 have the age correct and one way is to 3280 02:39:13,240 --> 02:39:20,319 go back and set it up over here in this 3281 02:39:17,000 --> 02:39:22,800 person but there's another way how we 3282 02:39:20,319 --> 02:39:25,000 can add this property and that is by 3283 02:39:22,800 --> 02:39:27,720 merging the 3284 02:39:25,000 --> 02:39:30,080 interfaces and I know that this looks a 3285 02:39:27,720 --> 02:39:31,560 little funky in the beginning please 3286 02:39:30,080 --> 02:39:33,200 keep in mind that most likely you're not 3287 02:39:31,560 --> 02:39:34,960 going to do that in the same file but 3288 02:39:33,200 --> 02:39:37,560 let's say if you're getting the 3289 02:39:34,960 --> 02:39:40,720 interface from somewhere if you know the 3290 02:39:37,560 --> 02:39:43,399 name you can do something like this and 3291 02:39:40,720 --> 02:39:46,880 as a said note yes essentially I'm using 3292 02:39:43,399 --> 02:39:50,160 the same name I go with interface person 3293 02:39:46,880 --> 02:39:53,600 and then I just go with age now number 3294 02:39:50,160 --> 02:39:56,760 so I add that extra property over here 3295 02:39:53,600 --> 02:39:58,479 now this also needs to be in my instance 3296 02:39:56,760 --> 02:40:01,720 correct so in here I need to come up 3297 02:39:58,479 --> 02:40:05,479 with some kind of value and now the get 3298 02:40:01,720 --> 02:40:09,840 details is correct I can access both the 3299 02:40:05,479 --> 02:40:11,920 name and age so I have the first one and 3300 02:40:09,840 --> 02:40:15,080 then I just merge so whatever properties 3301 02:40:11,920 --> 02:40:17,640 I'm going to set it up over here yes 3302 02:40:15,080 --> 02:40:20,399 essentially if I create this instance 3303 02:40:17,640 --> 02:40:22,560 now all of these properties 3304 02:40:20,399 --> 02:40:26,160 will have to be there hopefully that is 3305 02:40:22,560 --> 02:40:28,800 clear now just a showcase let me go with 3306 02:40:26,160 --> 02:40:32,240 person Dot and then get details let's 3307 02:40:28,800 --> 02:40:34,960 invoke it and for some reason I don't 3308 02:40:32,240 --> 02:40:37,399 see anything in the console and of 3309 02:40:34,960 --> 02:40:41,279 course because I'm returning so what am 3310 02:40:37,399 --> 02:40:43,520 I doing here let me go with my log me 3311 02:40:41,279 --> 02:40:46,520 move this sucker 3312 02:40:43,520 --> 02:40:49,880 up and now let's try it out yep I have 3313 02:40:46,520 --> 02:40:51,840 name John and age is 30 3314 02:40:49,880 --> 02:40:54,120 and up next let's see how we can extend 3315 02:40:51,840 --> 02:40:55,520 the interface which is a way for us to 3316 02:40:54,120 --> 02:40:59,399 create a new 3317 02:40:55,520 --> 02:41:01,960 interface that inherits or essentially 3318 02:40:59,399 --> 02:41:05,479 just uses the properties and 3319 02:41:01,960 --> 02:41:08,399 methods from the existing interface now 3320 02:41:05,479 --> 02:41:13,080 it sounds more complex than it actually 3321 02:41:08,399 --> 02:41:16,080 is imagine the scenario we have an 3322 02:41:13,080 --> 02:41:16,080 employee 3323 02:41:16,920 --> 02:41:21,680 instance and for now we're not going to 3324 02:41:19,520 --> 02:41:23,920 set the type let's just set it equal to 3325 02:41:21,680 --> 02:41:26,920 an object I'm going to go with some kind 3326 02:41:23,920 --> 02:41:29,399 of name I'm going to go not with John 3327 02:41:26,920 --> 02:41:32,279 let's go with Jane over here then let's 3328 02:41:29,399 --> 02:41:34,840 also set up the age CU 3329 02:41:32,279 --> 02:41:38,160 remember we already added this to a 3330 02:41:34,840 --> 02:41:41,920 person so now essentially we need name 3331 02:41:38,160 --> 02:41:45,000 we need get details and we also need an 3332 02:41:41,920 --> 02:41:49,640 age so let's set up some kind of age 3333 02:41:45,000 --> 02:41:51,600 here then let's go with our details 3334 02:41:49,640 --> 02:41:53,040 however in this case since we're going 3335 02:41:51,600 --> 02:41:55,359 to add one more 3336 02:41:53,040 --> 02:41:58,000 property the return is going to be a 3337 02:41:55,359 --> 02:42:00,800 little bit different here's the thing 3338 02:41:58,000 --> 02:42:03,240 Jane is a person that kind of makes 3339 02:42:00,800 --> 02:42:07,479 sense correct so I can definitely set it 3340 02:42:03,240 --> 02:42:10,920 equal to a person and call it a day 3341 02:42:07,479 --> 02:42:13,880 however let's imagine that Jane since 3342 02:42:10,920 --> 02:42:16,920 she's an employee she also has the 3343 02:42:13,880 --> 02:42:19,960 employee ID now I don't want to add it 3344 02:42:16,920 --> 02:42:22,920 to a person so I want to keep this as a 3345 02:42:19,960 --> 02:42:25,439 separate interface but I do want to use 3346 02:42:22,920 --> 02:42:28,680 all of the properties that I already 3347 02:42:25,439 --> 02:42:32,000 have in my person one and just add on 3348 02:42:28,680 --> 02:42:34,439 that employee ID so here's how we can do 3349 02:42:32,000 --> 02:42:37,439 that we go with 3350 02:42:34,439 --> 02:42:39,240 interface then come up with a name in my 3351 02:42:37,439 --> 02:42:41,680 case I'm going to go with employee and 3352 02:42:39,240 --> 02:42:44,040 then we go with this extends and now 3353 02:42:41,680 --> 02:42:46,520 we're just looking for a specific 3354 02:42:44,040 --> 02:42:48,120 interface we want to use and now let's 3355 02:42:46,520 --> 02:42:53,120 just add this 3356 02:42:48,120 --> 02:42:55,760 employee ID and set it equal to a number 3357 02:42:53,120 --> 02:42:58,000 so now Jane not only she's going to be a 3358 02:42:55,760 --> 02:43:01,279 person but she's also going to be an 3359 02:42:58,000 --> 02:43:05,160 employee so now when we set up the type 3360 02:43:01,279 --> 02:43:08,160 we go with employee and now not only I 3361 02:43:05,160 --> 02:43:12,359 have name and age but I also need to 3362 02:43:08,160 --> 02:43:15,960 apply this employee ID so let's add this 3363 02:43:12,359 --> 02:43:18,800 one let's say over here 1 two 3 and then 3364 02:43:15,960 --> 02:43:21,240 remember details needs to return a 3365 02:43:18,800 --> 02:43:25,000 string so in this case I'm just going to 3366 02:43:21,240 --> 02:43:28,160 grab whatever we have over here and then 3367 02:43:25,000 --> 02:43:30,560 let's just add the employee ID as well 3368 02:43:28,160 --> 02:43:33,840 so let's add a 3369 02:43:30,560 --> 02:43:37,439 comma we're going to go with ID and then 3370 02:43:33,840 --> 02:43:39,680 let's access this Dot and then employee 3371 02:43:37,439 --> 02:43:41,600 ID now just so we can see that 3372 02:43:39,680 --> 02:43:42,800 everything works I guess instead of 3373 02:43:41,600 --> 02:43:46,200 person 3374 02:43:42,800 --> 02:43:49,520 details let me go with 3375 02:43:46,200 --> 02:43:54,240 employee get details and yep and have 3376 02:43:49,520 --> 02:43:57,760 all three values in the console now we 3377 02:43:54,240 --> 02:44:00,840 can also inherit from multiple 3378 02:43:57,760 --> 02:44:04,120 interfaces essentially I can extend from 3379 02:44:00,840 --> 02:44:07,240 the person as well as the dog 3380 02:44:04,120 --> 02:44:09,520 owner so we're going to go with a 3381 02:44:07,240 --> 02:44:11,319 manager instance I guess so we're going 3382 02:44:09,520 --> 02:44:13,520 to go here with 3383 02:44:11,319 --> 02:44:16,040 manager eventually we'll set up the type 3384 02:44:13,520 --> 02:44:19,439 for now I just want to provide a name 3385 02:44:16,040 --> 02:44:22,640 it's going to be equal to a bob and also 3386 02:44:19,439 --> 02:44:24,040 for now let's go with the AG which will 3387 02:44:22,640 --> 02:44:27,479 be equal to 3388 02:44:24,040 --> 02:44:32,000 A35 now when it comes to 3389 02:44:27,479 --> 02:44:33,479 Bob not only is going to be a person so 3390 02:44:32,000 --> 02:44:35,560 we'll need to provide all of the 3391 02:44:33,479 --> 02:44:38,040 properties but also let's imagine that 3392 02:44:35,560 --> 02:44:40,479 he's a dog owner so essentially we want 3393 02:44:38,040 --> 02:44:43,760 to extend from both interfaces that we 3394 02:44:40,479 --> 02:44:47,880 already currently have the person and a 3395 02:44:43,760 --> 02:44:51,319 dog owner now since Bob is a manager I 3396 02:44:47,880 --> 02:44:53,279 also want to add a a method onto the 3397 02:44:51,319 --> 02:44:55,960 interface and in my case I'm going to 3398 02:44:53,279 --> 02:44:58,600 call this manage people so let's move 3399 02:44:55,960 --> 02:45:00,479 where we have Bob and first I guess 3400 02:44:58,600 --> 02:45:02,600 let's construct the interface and then 3401 02:45:00,479 --> 02:45:05,560 we'll worry about the properties so 3402 02:45:02,600 --> 02:45:08,000 we're going to go with interface manager 3403 02:45:05,560 --> 02:45:10,200 for stars let's just add that method 3404 02:45:08,000 --> 02:45:13,080 that I was talking about so I simply 3405 02:45:10,200 --> 02:45:15,080 want to go with manage 3406 02:45:13,080 --> 02:45:17,240 people uh it's not going to take any 3407 02:45:15,080 --> 02:45:20,399 parameters and we're not going to return 3408 02:45:17,240 --> 02:45:23,800 anything and if we set this equal to a 3409 02:45:20,399 --> 02:45:25,880 bob we'll have an issue since we have 3410 02:45:23,800 --> 02:45:28,439 two properties that we haven't set up in 3411 02:45:25,880 --> 02:45:31,680 our interface so now let's go with 3412 02:45:28,439 --> 02:45:34,160 extends and one by one let's look for 3413 02:45:31,680 --> 02:45:39,640 those interfaces so we're looking for a 3414 02:45:34,160 --> 02:45:44,439 person and a dog owner so Bob is a 3415 02:45:39,640 --> 02:45:47,920 manager a person and a dog owner and now 3416 02:45:44,439 --> 02:45:51,080 if I navigate over here and set it equal 3417 02:45:47,920 --> 02:45:53,760 to manager type I will have to provide 3418 02:45:51,080 --> 02:45:56,359 all of these values what they are well 3419 02:45:53,760 --> 02:45:59,560 we need to provide a dog name let's go 3420 02:45:56,359 --> 02:46:01,439 direct Rex it's a super popular name 3421 02:45:59,560 --> 02:46:03,200 then we also have get details and you 3422 02:46:01,439 --> 02:46:06,319 know what in this case I will speed this 3423 02:46:03,200 --> 02:46:08,200 up I really don't want to type 3424 02:46:06,319 --> 02:46:10,319 everything from the scratch so we have 3425 02:46:08,200 --> 02:46:13,640 get details what do we want to access 3426 02:46:10,319 --> 02:46:16,560 here I guess let's just go with age 3427 02:46:13,640 --> 02:46:19,880 since we don't have the employee ID then 3428 02:46:16,560 --> 02:46:23,600 we also have one for dog so let's go 3429 02:46:19,880 --> 02:46:23,600 with get dog 3430 02:46:24,240 --> 02:46:30,880 details now we will go with name is and 3431 02:46:27,399 --> 02:46:30,880 then let's access the dog 3432 02:46:32,840 --> 02:46:38,960 name dog name over here then let's 3433 02:46:35,600 --> 02:46:41,600 remove the age guess we don't need it 3434 02:46:38,960 --> 02:46:45,160 and lastly let's set up that manage 3435 02:46:41,600 --> 02:46:48,439 people manage people let's invoke it and 3436 02:46:45,160 --> 02:46:51,840 for now we simply want to log so let's 3437 02:46:48,439 --> 02:46:55,279 set up a console log and let's write a 3438 02:46:51,840 --> 02:46:57,600 text managing people and lastly just to 3439 02:46:55,279 --> 02:47:01,560 test it out that everything works want 3440 02:46:57,600 --> 02:47:03,880 to go with manager and it's invoke 3441 02:47:01,560 --> 02:47:06,760 manage people if everything is correct 3442 02:47:03,880 --> 02:47:09,279 we should have this text in a console 3443 02:47:06,760 --> 02:47:11,800 and again this is how we can merge 3444 02:47:09,279 --> 02:47:14,359 interfaces by essentially using the same 3445 02:47:11,800 --> 02:47:17,080 name and just adding the properties 3446 02:47:14,359 --> 02:47:19,479 another name that you might hear is 3447 02:47:17,080 --> 02:47:22,920 reopen essentially they mean the same 3448 02:47:19,479 --> 02:47:25,720 thing where we declare the interface 3449 02:47:22,920 --> 02:47:28,439 with the same name one more time and 3450 02:47:25,720 --> 02:47:32,560 then typescript is going to merge them 3451 02:47:28,439 --> 02:47:35,600 together and we can also extend so let's 3452 02:47:32,560 --> 02:47:37,600 say if we have an interface with some 3453 02:47:35,600 --> 02:47:40,560 properties and methods and we just want 3454 02:47:37,600 --> 02:47:43,680 to grab methods and properties from some 3455 02:47:40,560 --> 02:47:46,560 other interface simply go with extends 3456 02:47:43,680 --> 02:47:48,279 we look for that specific interface and 3457 02:47:46,560 --> 02:47:49,399 in the process we'll right away get 3458 02:47:48,279 --> 02:47:52,880 those methods 3459 02:47:49,399 --> 02:47:56,200 and properties and with this in place we 3460 02:47:52,880 --> 02:47:58,120 can move on to the next task and up next 3461 02:47:56,200 --> 02:48:01,600 let's work on another 3462 02:47:58,120 --> 02:48:03,080 challenge and during the challenge we'll 3463 02:48:01,600 --> 02:48:05,080 see how we can use 3464 02:48:03,080 --> 02:48:09,200 typu with 3465 02:48:05,080 --> 02:48:12,319 the interface now I did split up the 3466 02:48:09,200 --> 02:48:15,840 challenge into multiple videos so in the 3467 02:48:12,319 --> 02:48:18,520 first video we'll kind of prepare 3468 02:48:15,840 --> 02:48:22,040 everything we'll set up our function we 3469 02:48:18,520 --> 02:48:24,640 will will return one of the interface 3470 02:48:22,040 --> 02:48:27,000 types and then in the following video we 3471 02:48:24,640 --> 02:48:29,479 will work on more logic where we'll see 3472 02:48:27,000 --> 02:48:31,200 how we can use the typ guard so I didn't 3473 02:48:29,479 --> 02:48:32,800 want to jam everything in one video 3474 02:48:31,200 --> 02:48:33,760 because there's quite a few lines of 3475 02:48:32,800 --> 02:48:36,399 code 3476 02:48:33,760 --> 02:48:38,680 and I mean the most important thing is 3477 02:48:36,399 --> 02:48:41,120 kind of at the very end and usually if 3478 02:48:38,680 --> 02:48:43,640 it's a long video then people just lose 3479 02:48:41,120 --> 02:48:48,080 the interest so long story short we want 3480 02:48:43,640 --> 02:48:51,040 to define a person interface with the 3481 02:48:48,080 --> 02:48:53,680 name String then we want to set up a dog 3482 02:48:51,040 --> 02:48:55,479 owner which extends the person and just 3483 02:48:53,680 --> 02:48:57,720 add the dog name which is going to be a 3484 02:48:55,479 --> 02:49:00,720 string and also we want to set up a 3485 02:48:57,720 --> 02:49:04,000 manager which extends the person so not 3486 02:49:00,720 --> 02:49:06,479 person and dog owner no the manager is 3487 02:49:04,000 --> 02:49:10,439 going to extend only the person and 3488 02:49:06,479 --> 02:49:13,680 we'll just add two more methods on it 3489 02:49:10,439 --> 02:49:17,399 then we want to set up the instance 3490 02:49:13,680 --> 02:49:20,880 which can be either of them so we'll use 3491 02:49:17,399 --> 02:49:24,160 the Union type and actually we'll set it 3492 02:49:20,880 --> 02:49:27,000 equal to get employee function now in 3493 02:49:24,160 --> 02:49:30,880 there in get employee we'll just call 3494 02:49:27,000 --> 02:49:34,520 math.random and based on value we'll 3495 02:49:30,880 --> 02:49:37,239 return one of those interfaces so 3496 02:49:34,520 --> 02:49:39,520 whether a person dog owner and a manager 3497 02:49:37,239 --> 02:49:41,560 and again in the next video we'll see 3498 02:49:39,520 --> 02:49:43,200 how we can use the typ guard because 3499 02:49:41,560 --> 02:49:46,680 there's a 3500 02:49:43,200 --> 02:49:49,640 gacha that we need to be aware of STS 3501 02:49:46,680 --> 02:49:51,800 let's navigate to tutorial one by one 3502 02:49:49,640 --> 02:49:54,439 let's set it up so this is an interface 3503 02:49:51,800 --> 02:49:58,120 we want to go with person let's go with 3504 02:49:54,439 --> 02:49:59,840 name equals string okay then let's also 3505 02:49:58,120 --> 02:50:03,319 set up a dog 3506 02:49:59,840 --> 02:50:09,120 owner so we have here 3507 02:50:03,319 --> 02:50:13,960 interface dog owner now that one will 3508 02:50:09,120 --> 02:50:16,200 extend the person so we'll get that name 3509 02:50:13,960 --> 02:50:19,800 property and we also want to add the dog 3510 02:50:16,200 --> 02:50:22,760 name dog name will be equal to a string 3511 02:50:19,800 --> 02:50:25,399 and then the last interface is going to 3512 02:50:22,760 --> 02:50:28,520 be a 3513 02:50:25,399 --> 02:50:32,640 manager which is going to extend the 3514 02:50:28,520 --> 02:50:36,279 person and in here let's go with manage 3515 02:50:32,640 --> 02:50:38,800 people that's our method and we're 3516 02:50:36,279 --> 02:50:43,040 simply going to go with void and also 3517 02:50:38,800 --> 02:50:43,040 copy and paste and let's say delegate 3518 02:50:43,800 --> 02:50:49,520 tasks so those are our two 3519 02:50:46,520 --> 02:50:52,160 functions and then let's set up our our 3520 02:50:49,520 --> 02:50:54,640 instance but before we do that let's 3521 02:50:52,160 --> 02:50:56,960 also set up a function and I'm going to 3522 02:50:54,640 --> 02:50:59,040 call this get 3523 02:50:56,960 --> 02:51:01,359 employe and this is the function that 3524 02:50:59,040 --> 02:51:03,720 we're going to use in our employee 3525 02:51:01,359 --> 02:51:06,520 instance so for starters I'm going to go 3526 02:51:03,720 --> 02:51:09,600 here with const 3527 02:51:06,520 --> 02:51:11,960 employee and as far as the type I'm 3528 02:51:09,600 --> 02:51:14,439 going to use the union type so either 3529 02:51:11,960 --> 02:51:17,279 it's going to be a person or it's going 3530 02:51:14,439 --> 02:51:19,399 to be a dog owner or it's going to be a 3531 02:51:17,279 --> 02:51:22,880 manager and I'm going to say it equal to 3532 02:51:19,399 --> 02:51:25,720 my get employee and I'm going to invoke 3533 02:51:22,880 --> 02:51:29,640 it now I didn't want to move this 3534 02:51:25,720 --> 02:51:31,279 function up simply because in the 3535 02:51:29,640 --> 02:51:33,120 following video we'll write the code 3536 02:51:31,279 --> 02:51:35,600 over here and I think it's going to be 3537 02:51:33,120 --> 02:51:37,479 less confusing so let me move it up 3538 02:51:35,600 --> 02:51:41,800 that's going to be our function and 3539 02:51:37,479 --> 02:51:44,479 here's the task inside of the get 3540 02:51:41,800 --> 02:51:47,560 employee I want to 3541 02:51:44,479 --> 02:51:51,479 invoke math. random and then based on 3542 02:51:47,560 --> 02:51:53,960 the value if it's say less than 3543 02:51:51,479 --> 02:51:57,040 0.33 well then I'm just going to return 3544 02:51:53,960 --> 02:51:59,800 a person interface if not then it's 3545 02:51:57,040 --> 02:52:01,920 going to be a dog owner and if not then 3546 02:51:59,800 --> 02:52:04,239 it's going to be a manager so I have 3547 02:52:01,920 --> 02:52:07,160 this employee notice over here it just 3548 02:52:04,239 --> 02:52:08,760 says well the moment you're returning 3549 02:52:07,160 --> 02:52:11,239 void 3550 02:52:08,760 --> 02:52:14,479 and that's not what the employee is 3551 02:52:11,239 --> 02:52:15,760 expecting so in here I'm looking for one 3552 02:52:14,479 --> 02:52:18,399 of these three 3553 02:52:15,760 --> 02:52:20,319 values now in this get employee we're 3554 02:52:18,399 --> 02:52:22,960 not not going to pass any 3555 02:52:20,319 --> 02:52:26,520 parameters simply want to go with our 3556 02:52:22,960 --> 02:52:28,960 logic so let's set up the random that 3557 02:52:26,520 --> 02:52:31,800 one will be equal to math. random so 3558 02:52:28,960 --> 02:52:35,640 we'll get some random value between zero 3559 02:52:31,800 --> 02:52:38,160 and one and now let's just work on those 3560 02:52:35,640 --> 02:52:40,560 returns but also let's set up the type 3561 02:52:38,160 --> 02:52:45,080 right away so we're looking for 3562 02:52:40,560 --> 02:52:47,720 person we'll also grab the dog owner and 3563 02:52:45,080 --> 02:52:50,520 potentially this could be a manager so 3564 02:52:47,720 --> 02:52:53,239 again we don't know what the value is 3565 02:52:50,520 --> 02:52:55,359 going to be so every time we'll refresh 3566 02:52:53,239 --> 02:52:58,120 the browser we'll get some random value 3567 02:52:55,359 --> 02:53:00,800 over here and then based on that value 3568 02:52:58,120 --> 02:53:03,600 we'll set up that return so let's start 3569 02:53:00,800 --> 02:53:05,800 with our first option if the value is 3570 02:53:03,600 --> 02:53:08,479 less than 3571 02:53:05,800 --> 02:53:12,520 0.33 well in that case I want to go here 3572 02:53:08,479 --> 02:53:14,920 and I want to return a name John so 3573 02:53:12,520 --> 02:53:18,040 which interface is that well that's the 3574 02:53:14,920 --> 02:53:21,279 person one correct so these two match 3575 02:53:18,040 --> 02:53:26,520 then let's also set up another else 3576 02:53:21,279 --> 02:53:30,439 if so if the random is less than 3577 02:53:26,520 --> 02:53:33,520 0.66 in that case let me grab this and 3578 02:53:30,439 --> 02:53:35,880 now we just want to add the dog name so 3579 02:53:33,520 --> 02:53:39,359 in this case we're going to go with 3580 02:53:35,880 --> 02:53:41,960 Sarah and we're going to go with dog 3581 02:53:39,359 --> 02:53:43,880 name and we're going to set it equal to 3582 02:53:41,960 --> 02:53:48,960 Rex now 3583 02:53:43,880 --> 02:53:52,319 lastly if it's not less than 0.33 if 3584 02:53:48,960 --> 02:53:52,319 it's not less than 3585 02:53:52,680 --> 02:53:59,680 0.66 then we're going to go with another 3586 02:53:56,120 --> 02:54:02,600 else and in here we want to return a 3587 02:53:59,680 --> 02:54:06,160 manager interface so in this case we're 3588 02:54:02,600 --> 02:54:08,840 going to go with return Then object name 3589 02:54:06,160 --> 02:54:11,160 will be equal to a bob and then let's 3590 02:54:08,840 --> 02:54:13,680 set up those two methods we're going to 3591 02:54:11,160 --> 02:54:17,120 go with manage people and as far as the 3592 02:54:13,680 --> 02:54:21,760 logic let me just go with console log 3593 02:54:17,120 --> 02:54:21,760 and we're going to say managing 3594 02:54:22,399 --> 02:54:30,319 people dot okay good and then let's do 3595 02:54:26,000 --> 02:54:30,319 the same thing for delegate 3596 02:54:32,239 --> 02:54:39,920 tasks that's my function and as far as 3597 02:54:36,520 --> 02:54:39,920 the text it's going to 3598 02:54:41,840 --> 02:54:48,000 match now let me navigate where I have 3599 02:54:45,359 --> 02:54:50,600 the employee and let's just log it like 3600 02:54:48,000 --> 02:54:52,880 I said we'll have to refresh the browser 3601 02:54:50,600 --> 02:54:56,920 so once I navigate over here notice this 3602 02:54:52,880 --> 02:55:00,160 was my person instance then I get over 3603 02:54:56,920 --> 02:55:03,520 here the manager correct then I get the 3604 02:55:00,160 --> 02:55:06,160 dog owner and as you can see every time 3605 02:55:03,520 --> 02:55:08,680 we refresh the browser we're getting a 3606 02:55:06,160 --> 02:55:11,960 different value so in here 3607 02:55:08,680 --> 02:55:15,279 again we want to set get employee and we 3608 02:55:11,960 --> 02:55:18,560 want to make sure that we only return 3609 02:55:15,279 --> 02:55:21,080 one of these interfaces we use the unit 3610 02:55:18,560 --> 02:55:24,160 type and then when it comes to the 3611 02:55:21,080 --> 02:55:27,080 instance same deal I'm looking for one 3612 02:55:24,160 --> 02:55:30,160 of these three values so employee is 3613 02:55:27,080 --> 02:55:32,680 going to be either a person a dog owner 3614 02:55:30,160 --> 02:55:35,200 or a manager and in the following video 3615 02:55:32,680 --> 02:55:38,520 we'll introduce the typ guard and make 3616 02:55:35,200 --> 02:55:40,800 this challenge more complex okay and 3617 02:55:38,520 --> 02:55:43,920 with this in place now let's make this 3618 02:55:40,800 --> 02:55:47,840 challenge more complex by adding a 3619 02:55:43,920 --> 02:55:51,160 function which is going to check for 3620 02:55:47,840 --> 02:55:55,399 specific interface and only if it's a 3621 02:55:51,160 --> 02:55:58,520 manager we want to invoke delegate tasks 3622 02:55:55,399 --> 02:56:01,680 because here's the kicker we get back 3623 02:55:58,520 --> 02:56:04,319 our object we store it in the employee 3624 02:56:01,680 --> 02:56:07,120 and if we try to go with name everything 3625 02:56:04,319 --> 02:56:10,080 works why well because dog owner extends 3626 02:56:07,120 --> 02:56:12,399 the person manager extends the person so 3627 02:56:10,080 --> 02:56:14,319 that property will be definitely there 3628 02:56:12,399 --> 02:56:16,040 we can navigate to the browser we just 3629 02:56:14,319 --> 02:56:19,080 need to refresh and eventually we will 3630 02:56:16,040 --> 02:56:22,439 see all three values but it's different 3631 02:56:19,080 --> 02:56:24,920 with delegate tasks so notice once you 3632 02:56:22,439 --> 02:56:27,920 start typing typescript is complaining 3633 02:56:24,920 --> 02:56:30,960 it says well listen it could be either 3634 02:56:27,920 --> 02:56:34,760 the person or a dog owner or a manager 3635 02:56:30,960 --> 02:56:37,760 so you don't know that 100% the delegate 3636 02:56:34,760 --> 02:56:40,520 tasks is going to be there at runtime 3637 02:56:37,760 --> 02:56:43,040 this is why we want to set up a function 3638 02:56:40,520 --> 02:56:46,040 and we'll start with somewhat 3639 02:56:43,040 --> 02:56:49,640 straightforward approach and then we'll 3640 02:56:46,040 --> 02:56:51,720 cover why we want to use type predicate 3641 02:56:49,640 --> 02:56:53,680 so let's go here with a function my 3642 02:56:51,720 --> 02:56:55,680 okaye I'm going to call this is manager 3643 02:56:53,680 --> 02:56:59,000 so I want to make sure that it's a 3644 02:56:55,680 --> 02:57:01,160 manager and then in the if block I'm 3645 02:56:59,000 --> 02:57:04,080 just going to invoke delegate tasks 3646 02:57:01,160 --> 02:57:05,560 That's My overall goal so I'm going to 3647 02:57:04,080 --> 02:57:08,120 be looking for object because this is 3648 02:57:05,560 --> 02:57:10,920 what we're getting back from employee 3649 02:57:08,120 --> 02:57:14,120 but same deal as far as the parameter 3650 02:57:10,920 --> 02:57:17,560 well it can be one of these three 3651 02:57:14,120 --> 02:57:19,640 interfaces okay then as far as the 3652 02:57:17,560 --> 02:57:21,800 return like I said for now let's just go 3653 02:57:19,640 --> 02:57:23,640 with the straightforward approach where 3654 02:57:21,800 --> 02:57:26,160 I'm going to go with Boolean either it's 3655 02:57:23,640 --> 02:57:29,120 going to be true or false and I guess 3656 02:57:26,160 --> 02:57:31,520 the first gotcha in here is the 3657 02:57:29,120 --> 02:57:33,880 return remember previously when we used 3658 02:57:31,520 --> 02:57:38,120 the typ guards we did something like 3659 02:57:33,880 --> 02:57:41,399 this we went with type of then object 3660 02:57:38,120 --> 02:57:44,520 and set it equal to something now is it 3661 02:57:41,399 --> 02:57:47,120 equal to what object well all of them 3662 02:57:44,520 --> 02:57:50,680 are objects so that's not 3663 02:57:47,120 --> 02:57:54,000 specific so if I invoke his manager with 3664 02:57:50,680 --> 02:57:55,880 employee this should return for all of 3665 02:57:54,000 --> 02:57:58,319 them pretty much the same value let's 3666 02:57:55,880 --> 02:58:02,279 try it out we're going to go here with 3667 02:57:58,319 --> 02:58:03,960 log is manager then let's pass in the 3668 02:58:02,279 --> 02:58:06,920 employee and let's take a look at the 3669 02:58:03,960 --> 02:58:10,160 console notice as I'm clicking I'm all 3670 02:58:06,920 --> 02:58:12,960 the time getting true so yeah this time 3671 02:58:10,160 --> 02:58:17,000 guard is great but it's not going to 3672 02:58:12,960 --> 02:58:18,640 work in this case so if we want to check 3673 02:58:17,000 --> 02:58:19,880 for manager we want to look for 3674 02:58:18,640 --> 02:58:22,960 something 3675 02:58:19,880 --> 02:58:25,279 specific and that something specific is 3676 02:58:22,960 --> 02:58:27,920 one of these methods and the syntax is 3677 02:58:25,279 --> 02:58:31,279 following where we want to go with the 3678 02:58:27,920 --> 02:58:34,880 method which will pass in the string and 3679 02:58:31,279 --> 02:58:39,200 in and then the parameter in this case 3680 02:58:34,880 --> 02:58:43,760 object so now let's correctly type one 3681 02:58:39,200 --> 02:58:46,200 of the methods in my case manage people 3682 02:58:43,760 --> 02:58:49,000 and now let's navigate to the browser 3683 02:58:46,200 --> 02:58:50,439 and again once we refresh we'll actually 3684 02:58:49,000 --> 02:58:54,200 see correct 3685 02:58:50,439 --> 02:58:57,439 results only if the interface is manager 3686 02:58:54,200 --> 02:58:59,960 then we return true and if it's not the 3687 02:58:57,439 --> 02:59:03,120 case then we return false but there's 3688 02:58:59,960 --> 02:59:05,760 another gotcha so now let's go and set 3689 02:59:03,120 --> 02:59:09,239 up if block I'm going to go with is 3690 02:59:05,760 --> 02:59:12,960 manager I'll pass in the employee pretty 3691 02:59:09,239 --> 02:59:14,080 much our setup and then let's try to 3692 02:59:12,960 --> 02:59:17,160 invoke 3693 02:59:14,080 --> 02:59:19,399 employee and same deal I don't have 3694 02:59:17,160 --> 02:59:23,120 delegate tasks for some 3695 02:59:19,399 --> 02:59:25,479 reason let me try to invoke it and again 3696 02:59:23,120 --> 02:59:29,439 typescript is complaining so here's the 3697 02:59:25,479 --> 02:59:32,600 deal yes our logic over here is correct 3698 02:59:29,439 --> 02:59:35,560 but just because we return a Boolean 3699 02:59:32,600 --> 02:59:38,000 from this function doesn't automatically 3700 02:59:35,560 --> 02:59:40,880 signal to a typescript that now 3701 02:59:38,000 --> 02:59:43,479 employees actually type of 3702 02:59:40,880 --> 02:59:46,080 manager and here basically yeah we 3703 02:59:43,479 --> 02:59:48,600 passed the if condition but as far as 3704 02:59:46,080 --> 02:59:51,359 typescript is concerned it it can be 3705 02:59:48,600 --> 02:59:55,680 still one of these three types and this 3706 02:59:51,359 --> 02:59:58,760 is where type predicate is super useful 3707 02:59:55,680 --> 03:00:02,200 and that is a special kind of return 3708 02:59:58,760 --> 03:00:03,200 that not only returns a bullion but also 3709 03:00:02,200 --> 03:00:06,840 it 3710 03:00:03,200 --> 03:00:09,640 asserts the argument to be a specific 3711 03:00:06,840 --> 03:00:12,880 type so what am I talking about in here 3712 03:00:09,640 --> 03:00:16,120 instead of the Boolean we can go with 3713 03:00:12,880 --> 03:00:18,560 object that's going to be the syntax is 3714 03:00:16,120 --> 03:00:19,640 and then since I'm checking over here 3715 03:00:18,560 --> 03:00:21,760 for the 3716 03:00:19,640 --> 03:00:24,439 manager that's the interface I'm going 3717 03:00:21,760 --> 03:00:28,359 to use and now typescript definitely 3718 03:00:24,439 --> 03:00:33,000 knows that once we pass this condition 3719 03:00:28,359 --> 03:00:35,200 this will be a interface manager so once 3720 03:00:33,000 --> 03:00:38,080 I navigate back over here going to 3721 03:00:35,200 --> 03:00:41,399 refresh once I'm getting back the 3722 03:00:38,080 --> 03:00:44,200 manager I'm also logging delegating 3723 03:00:41,399 --> 03:00:47,680 tasks okay and at the very end of the 3724 03:00:44,200 --> 03:00:50,800 interface section I also want to cover 3725 03:00:47,680 --> 03:00:54,120 the differences between the interface 3726 03:00:50,800 --> 03:00:58,200 and the type alas so if you're setting 3727 03:00:54,120 --> 03:01:00,640 up the object since interface is used to 3728 03:00:58,200 --> 03:01:03,000 provide the shape of an object you can 3729 03:01:00,640 --> 03:01:07,040 use either of them so if I have object 3730 03:01:03,000 --> 03:01:10,800 John with name and age I can either use 3731 03:01:07,040 --> 03:01:13,640 the Alias type person or I can use the 3732 03:01:10,800 --> 03:01:16,920 interface person now as far as the 3733 03:01:13,640 --> 03:01:20,439 differences you see type alas we can 3734 03:01:16,920 --> 03:01:22,960 also set up for primitive types for 3735 03:01:20,439 --> 03:01:26,359 example if I have number or the union 3736 03:01:22,960 --> 03:01:28,840 type number or string and the literal 3737 03:01:26,359 --> 03:01:30,960 types you won't be able to do that with 3738 03:01:28,840 --> 03:01:33,239 the interface so that's the first 3739 03:01:30,960 --> 03:01:36,880 difference again when it comes to 3740 03:01:33,239 --> 03:01:39,160 interfaces they're useful to provide the 3741 03:01:36,880 --> 03:01:42,479 shape of an object but you won't use 3742 03:01:39,160 --> 03:01:47,279 them with the Primitive or with the 3743 03:01:42,479 --> 03:01:49,720 literal type now with interfaces we can 3744 03:01:47,279 --> 03:01:53,920 merge them together like we covered 3745 03:01:49,720 --> 03:01:56,399 before or another word is to reopen them 3746 03:01:53,920 --> 03:02:00,960 you won't be able to do that with the 3747 03:01:56,399 --> 03:02:03,000 type so with the interface you can set 3748 03:02:00,960 --> 03:02:05,200 up the interface you can add some 3749 03:02:03,000 --> 03:02:08,120 properties then I don't know three lines 3750 03:02:05,200 --> 03:02:10,120 later you can create the same interface 3751 03:02:08,120 --> 03:02:12,600 basically with the same name just add 3752 03:02:10,120 --> 03:02:15,880 more properties and you're good to go 3753 03:02:12,600 --> 03:02:18,760 that doesn't fly with the type that's 3754 03:02:15,880 --> 03:02:21,359 another difference now as as far as the 3755 03:02:18,760 --> 03:02:24,680 interfaces they can be implemented by 3756 03:02:21,359 --> 03:02:27,120 the classes while the type aliases 3757 03:02:24,680 --> 03:02:28,880 cannot be implemented in such way so if 3758 03:02:27,120 --> 03:02:32,160 you work with classes this is something 3759 03:02:28,880 --> 03:02:33,920 to keep in mind you can use the 3760 03:02:32,160 --> 03:02:36,120 interface with the class but you won't 3761 03:02:33,920 --> 03:02:39,279 be able to implement the type Alias with 3762 03:02:36,120 --> 03:02:42,000 the class and lastly remember when we 3763 03:02:39,279 --> 03:02:44,600 covered computed properties you can only 3764 03:02:42,000 --> 03:02:48,160 do that with type Alias so you won't be 3765 03:02:44,600 --> 03:02:50,520 able to use the computered properties 3766 03:02:48,160 --> 03:02:52,800 with the interface and with this in 3767 03:02:50,520 --> 03:02:55,279 place now we can move on to our next 3768 03:02:52,800 --> 03:02:58,960 topic okay and up next let's discuss 3769 03:02:55,279 --> 03:03:01,279 Tuple or Tuple in typescript which is a 3770 03:02:58,960 --> 03:03:04,399 special type since we don't have it in 3771 03:03:01,279 --> 03:03:06,479 JavaScript and essentially it allows us 3772 03:03:04,399 --> 03:03:08,760 to set up an 3773 03:03:06,479 --> 03:03:13,200 array with fixed 3774 03:03:08,760 --> 03:03:15,520 length and ordered with fixed type in 3775 03:03:13,200 --> 03:03:18,760 other words it's super useful when we 3776 03:03:15,520 --> 03:03:23,520 want to group different types of values 3777 03:03:18,760 --> 03:03:27,080 together so for example I have a let 3778 03:03:23,520 --> 03:03:31,479 person now this is an array and here I 3779 03:03:27,080 --> 03:03:35,840 have string of John and then number of 3780 03:03:31,479 --> 03:03:38,439 25 I can set the type in such a way 3781 03:03:35,840 --> 03:03:41,359 where I go with my array and then I 3782 03:03:38,439 --> 03:03:44,200 provide those values so in this case I'm 3783 03:03:41,359 --> 03:03:47,560 going to go with string and number so 3784 03:03:44,200 --> 03:03:51,040 now this value is fixed so if I'm going 3785 03:03:47,560 --> 03:03:54,840 to try to add something else like hello 3786 03:03:51,040 --> 03:03:57,319 typescript is going to complain if the 3787 03:03:54,840 --> 03:04:02,120 types are not going to match if for 3788 03:03:57,319 --> 03:04:04,880 example I do decide to place 25 over 3789 03:04:02,120 --> 03:04:07,720 here typescript also is going to 3790 03:04:04,880 --> 03:04:11,279 complain so not only we have fixed 3791 03:04:07,720 --> 03:04:15,840 length but also we right away set up the 3792 03:04:11,279 --> 03:04:18,520 fixed types so now this value can be 3793 03:04:15,840 --> 03:04:21,040 only the string and number now there is 3794 03:04:18,520 --> 03:04:23,600 a gacha something I'm going to cover in 3795 03:04:21,040 --> 03:04:26,000 a second but for now let's just focus on 3796 03:04:23,600 --> 03:04:28,640 this that yes it's going to be John and 3797 03:04:26,000 --> 03:04:32,359 it's going to be 25 now you're not 3798 03:04:28,640 --> 03:04:35,880 limited to only two values over here you 3799 03:04:32,359 --> 03:04:37,479 can set up as many as you want and for 3800 03:04:35,880 --> 03:04:40,560 example you can go here with number and 3801 03:04:37,479 --> 03:04:42,880 number I mean that's also an option 3802 03:04:40,560 --> 03:04:45,920 let's say you don't want to have just an 3803 03:04:42,880 --> 03:04:49,479 array of numbers but you want to have an 3804 03:04:45,920 --> 03:04:51,760 array with fixed amount of numbers so 3805 03:04:49,479 --> 03:04:55,120 let me just go here let's say with 3806 03:04:51,760 --> 03:04:58,800 date so in this case it's going to be 3807 03:04:55,120 --> 03:05:00,920 number number and number and then 3808 03:04:58,800 --> 03:05:03,000 depending where you live either you'll 3809 03:05:00,920 --> 03:05:06,040 pass in the month or the day so let's 3810 03:05:03,000 --> 03:05:09,439 say I'm going to go 12 then 3811 03:05:06,040 --> 03:05:13,279 17 and then we're going to go with 3812 03:05:09,439 --> 03:05:16,279 2001 so notice now this is a fixed array 3813 03:05:13,279 --> 03:05:19,200 with number number number so again if we 3814 03:05:16,279 --> 03:05:22,399 make any changes that typescript will 3815 03:05:19,200 --> 03:05:24,040 complain about it now it's useful if 3816 03:05:22,399 --> 03:05:26,200 let's say we want to return something 3817 03:05:24,040 --> 03:05:29,800 from the function let me go with 3818 03:05:26,200 --> 03:05:32,319 function get person and we're not going 3819 03:05:29,800 --> 03:05:34,160 to provide any parameters I'm just going 3820 03:05:32,319 --> 03:05:37,479 to say that yeah this function is going 3821 03:05:34,160 --> 03:05:39,960 to return an array with such types so 3822 03:05:37,479 --> 03:05:42,680 string and number so now let's set up 3823 03:05:39,960 --> 03:05:46,080 the function body where we want to go 3824 03:05:42,680 --> 03:05:48,880 with our array we want to pass in John 3825 03:05:46,080 --> 03:05:52,840 and we're going to go with our 3826 03:05:48,880 --> 03:05:56,279 25 so now if we have random person now 3827 03:05:52,840 --> 03:05:59,399 and that one is equal to a get person we 3828 03:05:56,279 --> 03:06:03,920 invoke it if we 3829 03:05:59,399 --> 03:06:06,720 log random person and then zero copy and 3830 03:06:03,920 --> 03:06:10,800 paste and one in a console we should 3831 03:06:06,720 --> 03:06:14,640 have both of those values now like I 3832 03:06:10,800 --> 03:06:16,200 mentioned the gotcha here is that even 3833 03:06:14,640 --> 03:06:19,680 though when we're hardcoding these 3834 03:06:16,200 --> 03:06:21,760 values typescript is going to complain 3835 03:06:19,680 --> 03:06:25,960 unless we provide over here this read 3836 03:06:21,760 --> 03:06:30,319 only we can actually add items or remove 3837 03:06:25,960 --> 03:06:31,880 items from our particular array so how 3838 03:06:30,319 --> 03:06:34,239 is that going to look like well let's 3839 03:06:31,880 --> 03:06:37,680 say if I go with date. 3840 03:06:34,239 --> 03:06:39,399 push I can add the values and typescript 3841 03:06:37,680 --> 03:06:42,680 actually is not going to complain about 3842 03:06:39,399 --> 03:06:45,560 it now it complains because it's not a 3843 03:06:42,680 --> 03:06:49,720 number right so it says well it's not a 3844 03:06:45,560 --> 03:06:52,760 number but if I change this to do my 34 3845 03:06:49,720 --> 03:06:55,600 everything is going to be fine so I can 3846 03:06:52,760 --> 03:06:58,040 set up multiple instances and now if I 3847 03:06:55,600 --> 03:07:01,359 loged the date check it out it's 3848 03:06:58,040 --> 03:07:05,200 probably not something that we expected 3849 03:07:01,359 --> 03:07:08,520 now the workr is to provide this readon 3850 03:07:05,200 --> 03:07:10,720 over here so in this case I cannot do 3851 03:07:08,520 --> 03:07:13,600 that so once we add this 3852 03:07:10,720 --> 03:07:17,520 readon we cannot use anymore the push 3853 03:07:13,600 --> 03:07:19,880 pop or whatever methods that's something 3854 03:07:17,520 --> 03:07:23,319 to keep in mind now lastly I also want 3855 03:07:19,880 --> 03:07:27,359 to mention that we can use the 3856 03:07:23,319 --> 03:07:31,479 optional parameters as well so let's 3857 03:07:27,359 --> 03:07:34,720 say if I'm going to create a new 3858 03:07:31,479 --> 03:07:38,080 instance and I'm going to call this 3859 03:07:34,720 --> 03:07:41,040 Susan so I'm going to go with my Tuple 3860 03:07:38,080 --> 03:07:42,760 we're going to set up a string number 3861 03:07:41,040 --> 03:07:45,720 and in here I'm just going to go with my 3862 03:07:42,760 --> 03:07:49,880 optional parameter once I set it equal 3863 03:07:45,720 --> 03:07:52,520 to my function notice I can just provide 3864 03:07:49,880 --> 03:07:55,479 the string and basically I'm good to go 3865 03:07:52,520 --> 03:07:59,080 typescript is not complaining this 3866 03:07:55,479 --> 03:08:01,960 should do it for tuples in typescript 3867 03:07:59,080 --> 03:08:02,880 and up next we're going to discuss enums 3868 03:08:01,960 --> 03:08:05,399 in 3869 03:08:02,880 --> 03:08:08,200 typescript okay and up next let's 3870 03:08:05,399 --> 03:08:10,640 discuss enums in typescript against 3871 03:08:08,200 --> 03:08:13,200 something we don't have in JavaScript 3872 03:08:10,640 --> 03:08:17,640 and essentially enums allow us to define 3873 03:08:13,200 --> 03:08:19,479 a set of named constants the syntax as 3874 03:08:17,640 --> 03:08:22,200 following we want to go with keyword 3875 03:08:19,479 --> 03:08:23,880 enum then let's come up with a name in 3876 03:08:22,200 --> 03:08:27,160 my case I'm going to go with server 3877 03:08:23,880 --> 03:08:29,279 response status since we'll have a 3878 03:08:27,160 --> 03:08:32,520 little bit of functionality so we're 3879 03:08:29,279 --> 03:08:34,479 going to go with response 3880 03:08:32,520 --> 03:08:37,239 status then we want to go with the 3881 03:08:34,479 --> 03:08:40,640 curries and this is totally up to you 3882 03:08:37,239 --> 03:08:42,720 you can go all lowercase you can 3883 03:08:40,640 --> 03:08:44,279 capitalize the first letter or you can 3884 03:08:42,720 --> 03:08:47,040 go all 3885 03:08:44,279 --> 03:08:49,520 uppercase totally up to you we just need 3886 03:08:47,040 --> 03:08:51,800 to provide divide over here the values 3887 03:08:49,520 --> 03:08:53,279 and in a second I'll show you what this 3888 03:08:51,800 --> 03:08:56,279 actually results 3889 03:08:53,279 --> 03:08:59,279 to so first one is going to be success 3890 03:08:56,279 --> 03:09:01,880 and then the error now we can add more 3891 03:08:59,279 --> 03:09:05,200 but for our example we really don't need 3892 03:09:01,880 --> 03:09:07,359 it then let's set up the 3893 03:09:05,200 --> 03:09:11,880 interface in this case I'm going to call 3894 03:09:07,359 --> 03:09:14,520 This Server response and then the first 3895 03:09:11,880 --> 03:09:17,760 property result actually is going to be 3896 03:09:14,520 --> 03:09:21,080 type of enum so let's set it equal to to 3897 03:09:17,760 --> 03:09:23,080 server response status and then another 3898 03:09:21,080 --> 03:09:25,880 one is going to be a data and this will 3899 03:09:23,080 --> 03:09:29,760 be an array of strings so now let's set 3900 03:09:25,880 --> 03:09:31,880 up a function get server response which 3901 03:09:29,760 --> 03:09:34,720 actually is going to return this 3902 03:09:31,880 --> 03:09:38,520 interface so we go here with function 3903 03:09:34,720 --> 03:09:41,840 get server response let's invoke that as 3904 03:09:38,520 --> 03:09:44,479 far as the return type let's set up the 3905 03:09:41,840 --> 03:09:48,479 server response so this is equal right 3906 03:09:44,479 --> 03:09:51,160 now to our interface and then inside of 3907 03:09:48,479 --> 03:09:54,120 the function body we want to go result 3908 03:09:51,160 --> 03:09:56,840 and now since this is type enum over 3909 03:09:54,120 --> 03:10:00,359 here this is what we want to use so we 3910 03:09:56,840 --> 03:10:02,800 go over here with server response status 3911 03:10:00,359 --> 03:10:05,840 and notice we either have error or 3912 03:10:02,800 --> 03:10:07,520 success again you can use enm if you 3913 03:10:05,840 --> 03:10:10,600 want to store some constants and then 3914 03:10:07,520 --> 03:10:14,960 later on use it in your project so now 3915 03:10:10,600 --> 03:10:17,720 in this case we only can provide success 3916 03:10:14,960 --> 03:10:20,439 or the error so let me type this one and 3917 03:10:17,720 --> 03:10:24,319 then in order to match our interface 3918 03:10:20,439 --> 03:10:27,040 I'll provide data as well I'll say first 3919 03:10:24,319 --> 03:10:29,800 item then comma and we're going to go 3920 03:10:27,040 --> 03:10:32,359 with second item now I do need to add a 3921 03:10:29,800 --> 03:10:35,960 comma over here we do want to invoke it 3922 03:10:32,359 --> 03:10:38,760 and set it equal to response 3923 03:10:35,960 --> 03:10:43,920 const response is equal 3924 03:10:38,760 --> 03:10:46,080 to our get server response let's invoke 3925 03:10:43,920 --> 03:10:48,800 this function over here and let's set up 3926 03:10:46,080 --> 03:10:51,120 the type what we're expecting so we're 3927 03:10:48,800 --> 03:10:52,359 going to look for our interface and then 3928 03:10:51,120 --> 03:10:54,840 let's 3929 03:10:52,359 --> 03:10:58,520 also log the response just because I 3930 03:10:54,840 --> 03:11:00,600 want to show you the potential values we 3931 03:10:58,520 --> 03:11:04,200 can set up over here so at the moment we 3932 03:11:00,600 --> 03:11:05,319 have success and error and by default 3933 03:11:04,200 --> 03:11:09,680 they 3934 03:11:05,319 --> 03:11:11,040 actually get the numb starting with zero 3935 03:11:09,680 --> 03:11:13,960 and I'll show you what I mean in a 3936 03:11:11,040 --> 03:11:16,920 second so first let me just log the 3937 03:11:13,960 --> 03:11:17,960 response and notice how the result is 3938 03:11:16,920 --> 03:11:21,640 zero 3939 03:11:17,960 --> 03:11:24,920 now why is that well because if we go up 3940 03:11:21,640 --> 03:11:28,600 over here and we log server response 3941 03:11:24,920 --> 03:11:32,640 status we'll see that we have right now 3942 03:11:28,600 --> 03:11:36,680 the success and error and the values are 3943 03:11:32,640 --> 03:11:39,399 zero and one so it starts with zero then 3944 03:11:36,680 --> 03:11:41,560 one and then pretty much the more values 3945 03:11:39,399 --> 03:11:44,040 you add well the more numbers you're 3946 03:11:41,560 --> 03:11:46,880 going to have now don't be surprised if 3947 03:11:44,040 --> 03:11:48,399 you see quite a few values in a console 3948 03:11:46,880 --> 03:11:50,359 ENT 3949 03:11:48,399 --> 03:11:52,840 at the moment we have four of them this 3950 03:11:50,359 --> 03:11:54,680 is something we're going to discuss in 3951 03:11:52,840 --> 03:11:58,279 the upcoming video for now I just want 3952 03:11:54,680 --> 03:12:01,840 to showcase that even by default you get 3953 03:11:58,279 --> 03:12:04,920 those values starting with zero so 0 1 2 3954 03:12:01,840 --> 03:12:07,040 3 and whatever you can also reassign 3955 03:12:04,920 --> 03:12:10,800 them so for example I can say that 3956 03:12:07,040 --> 03:12:13,120 success is going to be equal to a 500 or 3957 03:12:10,800 --> 03:12:16,800 I'm sorry 200 and error is going to be 3958 03:12:13,120 --> 03:12:19,239 equal to 500 or I can mix and match I 3959 03:12:16,800 --> 03:12:22,120 can say that yeah error is actually 3960 03:12:19,239 --> 03:12:24,720 going to be equal to this string this 3961 03:12:22,120 --> 03:12:27,120 also is valid and again the main point 3962 03:12:24,720 --> 03:12:30,040 is that now we have over here this 3963 03:12:27,120 --> 03:12:32,479 constant which we can use all throughout 3964 03:12:30,040 --> 03:12:35,279 our project instead of trying to make 3965 03:12:32,479 --> 03:12:38,279 sure that we type the string or the 3966 03:12:35,279 --> 03:12:41,319 number correctly okay and up next let's 3967 03:12:38,279 --> 03:12:42,960 take a look at the gacha with enams 3968 03:12:41,319 --> 03:12:45,800 which is the fact that when you have 3969 03:12:42,960 --> 03:12:47,640 number values it's actually going to do 3970 03:12:45,800 --> 03:12:51,399 the reverse mapping 3971 03:12:47,640 --> 03:12:53,840 and remember it is the default so if we 3972 03:12:51,399 --> 03:12:56,560 don't provide anything we will get that 3973 03:12:53,840 --> 03:12:59,600 reverse mapping for example if I provide 3974 03:12:56,560 --> 03:13:02,680 over here 200 and 500 if I navigate to 3975 03:12:59,600 --> 03:13:05,560 the browser again I have more values 3976 03:13:02,680 --> 03:13:08,680 over here in this object so not only 3977 03:13:05,560 --> 03:13:11,600 error and success but I also have this 3978 03:13:08,680 --> 03:13:14,000 reverse where number is actually a 3979 03:13:11,600 --> 03:13:18,200 property and then whatever is my 3980 03:13:14,000 --> 03:13:20,760 constant name is set as value now why do 3981 03:13:18,200 --> 03:13:24,960 we care well because let's say we want 3982 03:13:20,760 --> 03:13:28,439 to iterate over the values in JavaScript 3983 03:13:24,960 --> 03:13:31,319 we have object. values and then we pass 3984 03:13:28,439 --> 03:13:33,640 in the object in this case we'll pass in 3985 03:13:31,319 --> 03:13:35,279 the inum and you'll see that everything 3986 03:13:33,640 --> 03:13:37,359 is going to work but it's going to 3987 03:13:35,279 --> 03:13:39,760 depend what kind of value we have over 3988 03:13:37,359 --> 03:13:43,279 here if we have a number we'll have to 3989 03:13:39,760 --> 03:13:47,279 do an extra check so let's go here with 3990 03:13:43,279 --> 03:13:50,640 object dot values then let's provide 3991 03:13:47,279 --> 03:13:54,000 server response status and we're going 3992 03:13:50,640 --> 03:13:58,720 to go with for each so we're iterating 3993 03:13:54,000 --> 03:14:00,120 over now I want to grab every value and 3994 03:13:58,720 --> 03:14:04,439 we'll just log 3995 03:14:00,120 --> 03:14:07,640 it and again since I have the number 3996 03:14:04,439 --> 03:14:12,000 values I have this reverse mapping so 3997 03:14:07,640 --> 03:14:15,200 now not only I have the number value I 3998 03:14:12,000 --> 03:14:17,560 also have this success and error and 3999 03:14:15,200 --> 03:14:20,359 it's not going to be the case if 4000 03:14:17,560 --> 03:14:23,920 let's say we're going to go here with 4001 03:14:20,359 --> 03:14:26,279 success and in here error again 4002 03:14:23,920 --> 03:14:29,000 something valid that you might set up in 4003 03:14:26,279 --> 03:14:33,359 your code now everything is correct now 4004 03:14:29,000 --> 03:14:36,399 we're only accessing that string so if 4005 03:14:33,359 --> 03:14:38,960 we have numbers which again is the 4006 03:14:36,399 --> 03:14:43,520 default setup so again let me set it up 4007 03:14:38,960 --> 03:14:46,399 here as 200 just so we're on the same 4008 03:14:43,520 --> 03:14:49,600 page the solution over here is just to 4009 03:14:46,399 --> 03:14:51,800 add another another typ guard so if I 4010 03:14:49,600 --> 03:14:55,000 only want to access the values they are 4011 03:14:51,800 --> 03:14:57,479 type what their number so this was going 4012 03:14:55,000 --> 03:15:00,399 to be a string therefore in here I'll 4013 03:14:57,479 --> 03:15:04,359 say if type 4014 03:15:00,399 --> 03:15:08,760 off and value that was the name of my 4015 03:15:04,359 --> 03:15:11,040 parameter if it's equal to a number well 4016 03:15:08,760 --> 03:15:13,520 then I want to log it or do some kind of 4017 03:15:11,040 --> 03:15:15,399 functionality in this case just log so 4018 03:15:13,520 --> 03:15:18,120 we're going to go with console log and 4019 03:15:15,399 --> 03:15:21,080 we'll log the value so now we 4020 03:15:18,120 --> 03:15:24,960 correctly just access those number 4021 03:15:21,080 --> 03:15:27,760 values now another place where we need 4022 03:15:24,960 --> 03:15:30,080 to be careful is when we're setting up 4023 03:15:27,760 --> 03:15:32,279 the value so notice something 4024 03:15:30,080 --> 03:15:35,520 interesting if I have an 4025 03:15:32,279 --> 03:15:41,080 enum I have a constant over here and the 4026 03:15:35,520 --> 03:15:44,720 value is number then later when I set up 4027 03:15:41,080 --> 03:15:48,279 this enum as my type I can still use the 4028 03:15:44,720 --> 03:15:50,720 good old number so this is going to work 4029 03:15:48,279 --> 03:15:54,600 because again typescript is doing that 4030 03:15:50,720 --> 03:15:57,319 reverse mapping however if you have an 4031 03:15:54,600 --> 03:16:00,600 enum where the constant value is 4032 03:15:57,319 --> 03:16:03,640 actually string this is not going to fly 4033 03:16:00,600 --> 03:16:06,080 so just to Showcase that I think I'll 4034 03:16:03,640 --> 03:16:10,199 comment this one out so if I have here 4035 03:16:06,080 --> 03:16:13,720 200 and 500 if I set up over here this 4036 03:16:10,199 --> 03:16:16,640 result to be equal to my enum I can go 4037 03:16:13,720 --> 03:16:19,120 to my server response and this is going 4038 03:16:16,640 --> 03:16:21,479 to work work so typescript is not going 4039 03:16:19,120 --> 03:16:24,160 to complain about it and I can see that 4040 03:16:21,479 --> 03:16:27,160 my code runs and everything is awesome 4041 03:16:24,160 --> 03:16:29,960 however if again and I promised for the 4042 03:16:27,160 --> 03:16:33,319 last time we'll change it around we go 4043 03:16:29,960 --> 03:16:36,479 with success and error this is not going 4044 03:16:33,319 --> 03:16:39,760 to fly if we navigate back to our get 4045 03:16:36,479 --> 03:16:42,359 server response and try to provide the 4046 03:16:39,760 --> 03:16:44,960 string value as you can see right now 4047 03:16:42,359 --> 03:16:50,040 typescript is complaining so we cannot 4048 03:16:44,960 --> 03:16:52,000 assign string to a type enum however 4049 03:16:50,040 --> 03:16:54,359 when we use the number value it's going 4050 03:16:52,000 --> 03:16:56,840 to work because there is this reverse 4051 03:16:54,359 --> 03:16:59,439 mapping hopefully that's clear and now 4052 03:16:56,840 --> 03:17:01,359 we're ready to move on to the next topic 4053 03:16:59,439 --> 03:17:05,560 okay and now let's work on our next 4054 03:17:01,359 --> 03:17:08,279 challenge where we want to define a enum 4055 03:17:05,560 --> 03:17:12,160 with the consant of admin manager and 4056 03:17:08,279 --> 03:17:15,720 employee Define the type Alias named 4057 03:17:12,160 --> 03:17:19,000 user and in there we'll set up the ID 4058 03:17:15,720 --> 03:17:22,279 property name as well as the rooll and 4059 03:17:19,000 --> 03:17:25,439 the RO is going to be equal to our enum 4060 03:17:22,279 --> 03:17:28,359 and also we'll set up the contact which 4061 03:17:25,439 --> 03:17:31,680 is going to be a tuple on two elements 4062 03:17:28,359 --> 03:17:35,760 the email which is a string as well as 4063 03:17:31,680 --> 03:17:37,840 the phone then we'll set up a function 4064 03:17:35,760 --> 03:17:40,560 by the name of create user which will 4065 03:17:37,840 --> 03:17:44,199 take in the user object as a parameter 4066 03:17:40,560 --> 03:17:46,239 and return the user object and then at 4067 03:17:44,199 --> 03:17:47,720 the very end we'll call the user object 4068 03:17:46,239 --> 03:17:51,120 provide the object object and we'll 4069 03:17:47,720 --> 03:17:53,960 create the instance so in tutorial I 4070 03:17:51,120 --> 03:17:56,479 think I want to start by defining that 4071 03:17:53,960 --> 03:18:00,120 enum here let's call this 4072 03:17:56,479 --> 03:18:02,160 user role let me set up my constants 4073 03:18:00,120 --> 03:18:06,359 here and I think I'm going to rely on 4074 03:18:02,160 --> 03:18:08,880 those default values so 0 1 two and I 4075 03:18:06,359 --> 03:18:12,960 guess in this case that's going to be it 4076 03:18:08,880 --> 03:18:15,160 only going to be 0 and one two then we 4077 03:18:12,960 --> 03:18:17,359 also want to go with 4078 03:18:15,160 --> 03:18:20,640 employee after that let's set up that 4079 03:18:17,359 --> 03:18:23,319 type alias in this case 4080 03:18:20,640 --> 03:18:26,520 user here one by one let's set up those 4081 03:18:23,319 --> 03:18:30,000 properties ID is equal to a number then 4082 03:18:26,520 --> 03:18:32,239 we also have name is equal to string 4083 03:18:30,000 --> 03:18:35,600 we're going to go with roll and in this 4084 03:18:32,239 --> 03:18:37,239 case it's going to be equal to a user R 4085 03:18:35,600 --> 03:18:40,120 so one of 4086 03:18:37,239 --> 03:18:42,640 our three constants and then we also 4087 03:18:40,120 --> 03:18:46,239 want to go with contact and we set this 4088 03:18:42,640 --> 03:18:49,399 one up as string and string we have 4089 03:18:46,239 --> 03:18:52,680 create user function so let's set it up 4090 03:18:49,399 --> 03:18:56,199 over here create user and this one is 4091 03:18:52,680 --> 03:18:59,439 looking for a user parameter which is 4092 03:18:56,199 --> 03:19:01,600 going to be type user same deal over 4093 03:18:59,439 --> 03:19:04,439 here we're going to go with user and 4094 03:19:01,600 --> 03:19:07,439 from the function we'll return the user 4095 03:19:04,439 --> 03:19:10,439 and the last step is to create the 4096 03:19:07,439 --> 03:19:14,800 instance so let's go with the user which 4097 03:19:10,439 --> 03:19:17,680 will be type user let's call create user 4098 03:19:14,800 --> 03:19:23,080 and now let's provide the values 4099 03:19:17,680 --> 03:19:27,640 ID name John do then also we'll have a 4100 03:19:23,080 --> 03:19:30,399 roll and since we're using our enum 4101 03:19:27,640 --> 03:19:33,720 we're just going to set it equal to user 4102 03:19:30,399 --> 03:19:35,720 R then Dot and one of the values in my 4103 03:19:33,720 --> 03:19:38,319 case I think I'm going to go with admin 4104 03:19:35,720 --> 03:19:41,080 one and then at the very end we also 4105 03:19:38,319 --> 03:19:45,640 have the contact I want to set it equal 4106 03:19:41,080 --> 03:19:50,319 to our array and let's just provide some 4107 03:19:45,640 --> 03:19:52,439 values gmail.com comma and then we're 4108 03:19:50,319 --> 03:19:57,640 going to go with 1 2 4109 03:19:52,439 --> 03:20:02,399 3 four 5 six and 7 4110 03:19:57,640 --> 03:20:03,920 89 the very end let's just log it and 4111 03:20:02,399 --> 03:20:06,720 pre sure everything is going to be 4112 03:20:03,920 --> 03:20:09,439 correct Yep looks like I have the 4113 03:20:06,720 --> 03:20:12,319 correct value and with this in place 4114 03:20:09,439 --> 03:20:16,040 we're ready to move on to the next topic 4115 03:20:12,319 --> 03:20:18,720 and up next let's cover type assertion 4116 03:20:16,040 --> 03:20:23,239 which is a way way to tell the browser 4117 03:20:18,720 --> 03:20:25,920 what type an existing variable is and 4118 03:20:23,239 --> 03:20:29,279 this is especially useful when we know 4119 03:20:25,920 --> 03:20:32,960 more about the type than the typescript 4120 03:20:29,279 --> 03:20:36,640 does as a side not it's very handy with 4121 03:20:32,960 --> 03:20:39,439 dom elements so if these examples don't 4122 03:20:36,640 --> 03:20:42,279 seem that convincing in the later 4123 03:20:39,439 --> 03:20:45,000 sections we will cover different 4124 03:20:42,279 --> 03:20:48,800 examples as well so let's imagine the 4125 03:20:45,000 --> 03:20:51,399 scenario we have a variable and I'm 4126 03:20:48,800 --> 03:20:54,279 going to go with some value and for some 4127 03:20:51,399 --> 03:20:56,680 reason type is any now as far as the 4128 03:20:54,279 --> 03:21:00,479 value I'll set this up as a string so 4129 03:20:56,680 --> 03:21:03,560 this is a string and then in the next 4130 03:21:00,479 --> 03:21:06,080 line I want to get the string length 4131 03:21:03,560 --> 03:21:09,840 which actually is going to be a number 4132 03:21:06,080 --> 03:21:11,800 so in here let's set it up type is going 4133 03:21:09,840 --> 03:21:15,800 to be equal to a number and now I want 4134 03:21:11,800 --> 03:21:18,600 to go with DOT length but I want to 4135 03:21:15,800 --> 03:21:21,800 assert to a typescript that actually 4136 03:21:18,600 --> 03:21:25,520 this is a string so I'll say some value 4137 03:21:21,800 --> 03:21:28,319 and not just any type I'll say as string 4138 03:21:25,520 --> 03:21:31,279 so the keyword we're looking for is this 4139 03:21:28,319 --> 03:21:35,239 as that's the assertion where we tell 4140 03:21:31,279 --> 03:21:37,760 typescript that yeah I know that maybe 4141 03:21:35,239 --> 03:21:41,239 this is not something you expect but I 4142 03:21:37,760 --> 03:21:42,479 know more and I know that this is the 4143 03:21:41,239 --> 03:21:44,960 specific 4144 03:21:42,479 --> 03:21:48,399 type this is also something that we can 4145 03:21:44,960 --> 03:21:52,399 use for example with the Json parse 4146 03:21:48,399 --> 03:21:58,439 because it returns any so let's say we 4147 03:21:52,399 --> 03:22:01,359 have a type bird now this type bird only 4148 03:21:58,439 --> 03:22:04,040 has a name and we'll set it equal to a 4149 03:22:01,359 --> 03:22:07,720 string now it looks like I have a type 4150 03:22:04,040 --> 03:22:12,319 over here so let me set it properly and 4151 03:22:07,720 --> 03:22:14,359 now I'll set up two Json strings and 4152 03:22:12,319 --> 03:22:17,520 we'll run pars now in order to speed 4153 03:22:14,359 --> 03:22:20,760 this up I will grab those values from 4154 03:22:17,520 --> 03:22:23,479 the read meor here so basically we have 4155 03:22:20,760 --> 03:22:26,359 bird string and a dog string we want to 4156 03:22:23,479 --> 03:22:28,920 set it up over here and now again once 4157 03:22:26,359 --> 03:22:32,239 we'll run Json pars you'll see that type 4158 03:22:28,920 --> 03:22:35,680 actually is any so let's say I have a 4159 03:22:32,239 --> 03:22:39,800 bird object and that one is equal to 4160 03:22:35,680 --> 03:22:42,399 Json then parse and now I'll pass in my 4161 03:22:39,800 --> 03:22:45,279 bird string so again let me copy and 4162 03:22:42,399 --> 03:22:48,399 paste and let me set up one for the 4163 03:22:45,279 --> 03:22:52,640 dog and as we hover over like I said 4164 03:22:48,399 --> 03:22:55,359 type is any but I don't want type to be 4165 03:22:52,640 --> 03:22:59,840 any I actually want this to be a type 4166 03:22:55,359 --> 03:23:02,479 bird so if we create an instance or any 4167 03:22:59,840 --> 03:23:06,160 other functionality and we set it equal 4168 03:23:02,479 --> 03:23:10,239 to a bird object instead of just leaving 4169 03:23:06,160 --> 03:23:14,080 it as type any I can go with as and then 4170 03:23:10,239 --> 03:23:16,520 my type because I clearly know that yeah 4171 03:23:14,080 --> 03:23:19,160 maybe I'm getting this back as type any 4172 03:23:16,520 --> 03:23:21,960 but I know that the signature matches 4173 03:23:19,160 --> 03:23:24,560 whatever I have here for the type bird 4174 03:23:21,960 --> 03:23:27,880 now please be careful you can also do 4175 03:23:24,560 --> 03:23:31,840 something like this where you can set up 4176 03:23:27,880 --> 03:23:36,199 the dog to be a bird and most likely 4177 03:23:31,840 --> 03:23:39,000 that's not the behavior you want so if I 4178 03:23:36,199 --> 03:23:41,479 go here with not bird object I'm going 4179 03:23:39,000 --> 03:23:45,880 to go with a dog object and I'll set it 4180 03:23:41,479 --> 03:23:50,160 equal to a bird I will be able to look 4181 03:23:45,880 --> 03:23:52,600 for dog. name but if we take a look at 4182 03:23:50,160 --> 03:23:55,520 the dog string it doesn't have that 4183 03:23:52,600 --> 03:24:00,160 property so it has the breed so if we go 4184 03:23:55,520 --> 03:24:02,040 here with log and we type bird name and 4185 03:24:00,160 --> 03:24:05,560 as I said for some reason it's 4186 03:24:02,040 --> 03:24:08,520 complaining it says cannot find bird and 4187 03:24:05,560 --> 03:24:12,359 yep it's because for some reason I named 4188 03:24:08,520 --> 03:24:15,000 it bed so let me go here with bird and 4189 03:24:12,359 --> 03:24:17,040 also a dog notice how typescript is not 4190 03:24:15,000 --> 03:24:19,760 complaining because 4191 03:24:17,040 --> 03:24:23,040 I made a type assertion that yes this 4192 03:24:19,760 --> 03:24:26,840 will be type bird even though we can 4193 03:24:23,040 --> 03:24:29,720 clearly see that it's actually type dog 4194 03:24:26,840 --> 03:24:31,880 now we don't have the type yet but you 4195 03:24:29,720 --> 03:24:34,960 get the point so if I navigate right out 4196 03:24:31,880 --> 03:24:36,960 of the browser notice how I will get 4197 03:24:34,960 --> 03:24:40,199 this unexpected 4198 03:24:36,960 --> 03:24:43,120 undefined at runtime and at the very end 4199 03:24:40,199 --> 03:24:45,479 let me just show you a use case that I 4200 03:24:43,120 --> 03:24:49,160 just encountered when I was working on 4201 03:24:45,479 --> 03:24:53,080 my next JS project so in there there was 4202 03:24:49,160 --> 03:24:55,120 a enum and I believe the name was status 4203 03:24:53,080 --> 03:24:57,640 don't quote me on that and there were 4204 03:24:55,120 --> 03:25:00,239 more values but I'm just going to set up 4205 03:24:57,640 --> 03:25:03,560 two so we don't have to spend all day 4206 03:25:00,239 --> 03:25:06,760 typing and essentially there was a 4207 03:25:03,560 --> 03:25:09,800 constant and I set the values equal to a 4208 03:25:06,760 --> 03:25:12,000 string so there was pending I believe 4209 03:25:09,800 --> 03:25:14,000 interview declin but we're just going to 4210 03:25:12,000 --> 03:25:16,399 go with these two so we're going to go 4211 03:25:14,000 --> 03:25:20,800 with declined and we'll set it equal 4212 03:25:16,399 --> 03:25:23,760 also to a string so I had this enum and 4213 03:25:20,800 --> 03:25:26,279 then I was just adding it to I believe 4214 03:25:23,760 --> 03:25:27,960 it was the job instance and I was saving 4215 03:25:26,279 --> 03:25:30,439 it in a 4216 03:25:27,960 --> 03:25:33,199 database now here's the kicker when I 4217 03:25:30,439 --> 03:25:35,199 was saving actually in a database I was 4218 03:25:33,199 --> 03:25:39,120 saving it as a string because that was 4219 03:25:35,199 --> 03:25:41,120 the value for my status pending and then 4220 03:25:39,120 --> 03:25:43,880 when I was getting back the value from 4221 03:25:41,120 --> 03:25:47,760 the database it was already a string but 4222 03:25:43,880 --> 03:25:49,760 I needed to reassign this value to one 4223 03:25:47,760 --> 03:25:51,760 of the instances and at that point 4224 03:25:49,760 --> 03:25:53,920 typescript was complaining because they 4225 03:25:51,760 --> 03:25:55,760 didn't match and if this doesn't make 4226 03:25:53,920 --> 03:25:58,000 any sense let me just 4227 03:25:55,760 --> 03:26:00,319 showcase let's imagine that we have a 4228 03:25:58,000 --> 03:26:03,760 user object and in there I have two 4229 03:26:00,319 --> 03:26:06,720 things I have name String and also 4230 03:26:03,760 --> 03:26:10,880 status is now going to be equal to a 4231 03:26:06,720 --> 03:26:13,520 type status so we know that ually we can 4232 03:26:10,880 --> 03:26:15,760 use status. pending status. decline 4233 03:26:13,520 --> 03:26:19,319 everything is nice but since these 4234 03:26:15,760 --> 03:26:21,560 values are not numbers we don't have 4235 03:26:19,319 --> 03:26:24,040 that reverse mapping so if we'll try to 4236 03:26:21,560 --> 03:26:26,439 set this value as a string it's not 4237 03:26:24,040 --> 03:26:28,640 going to work so now let me quickly 4238 03:26:26,439 --> 03:26:31,399 navigate to read me and I just want to 4239 03:26:28,640 --> 03:26:33,040 grab these two comments over here again 4240 03:26:31,399 --> 03:26:36,720 just so we don't have to type this from 4241 03:26:33,040 --> 03:26:39,880 the scratch imagine this scenario now we 4242 03:26:36,720 --> 03:26:42,120 save status pending in a database but we 4243 03:26:39,880 --> 03:26:44,880 save it as a string so we'll save this 4244 03:26:42,120 --> 03:26:47,560 value over here then we get it back from 4245 03:26:44,880 --> 03:26:49,960 the database and 4246 03:26:47,560 --> 03:26:54,199 now I'm going to go with the status 4247 03:26:49,960 --> 03:26:56,800 value and I'll set it equal to the 4248 03:26:54,199 --> 03:26:58,199 string value of pending so effectively 4249 03:26:56,800 --> 03:26:59,880 this is what I'm getting back from the 4250 03:26:58,199 --> 03:27:02,600 database again kind of makes sense 4251 03:26:59,880 --> 03:27:06,120 correct because this is the actual 4252 03:27:02,600 --> 03:27:08,840 string value now if I'm going to go here 4253 03:27:06,120 --> 03:27:10,319 and set up my user instance and set it 4254 03:27:08,840 --> 03:27:12,880 equal to my user 4255 03:27:10,319 --> 03:27:15,840 type yep I typed the name everything's 4256 03:27:12,880 --> 03:27:18,960 correct but if just going to go with the 4257 03:27:15,840 --> 03:27:21,960 stat is equal to a status value notice 4258 03:27:18,960 --> 03:27:24,960 how typescript immediately complains 4259 03:27:21,960 --> 03:27:27,319 type pending is not assignable to this 4260 03:27:24,960 --> 03:27:29,439 type so typescript doesn't know that I 4261 03:27:27,319 --> 03:27:32,000 saved in the database as a string so I 4262 03:27:29,439 --> 03:27:33,359 know for sure that it's the value that's 4263 03:27:32,000 --> 03:27:36,720 going to 4264 03:27:33,359 --> 03:27:38,720 match the constants here in the status 4265 03:27:36,720 --> 03:27:42,680 TPT doesn't know that it just says Hey 4266 03:27:38,720 --> 03:27:45,520 listen status here is type status so you 4267 03:27:42,680 --> 03:27:48,600 cannot set it equal to a string and this 4268 03:27:45,520 --> 03:27:51,880 is where the assertion is useful where I 4269 03:27:48,600 --> 03:27:56,399 say yeah it is a string but I know for 4270 03:27:51,880 --> 03:28:00,680 sure that is type status so again type 4271 03:27:56,399 --> 03:28:04,160 assertion essentially is a way to tell 4272 03:28:00,680 --> 03:28:07,080 the typescript that we know what the 4273 03:28:04,160 --> 03:28:09,760 type is that we know more about the type 4274 03:28:07,080 --> 03:28:12,399 than the typescript does okay and up 4275 03:28:09,760 --> 03:28:16,560 next let's cover type unknown which is a 4276 03:28:12,399 --> 03:28:19,199 type safe counterpart to type any so 4277 03:28:16,560 --> 03:28:23,319 just like type any we can assign any 4278 03:28:19,199 --> 03:28:25,840 value to type unknown but the difference 4279 03:28:23,319 --> 03:28:28,319 is that before we can use the variable 4280 03:28:25,840 --> 03:28:32,520 with type unknown we need to perform 4281 03:28:28,319 --> 03:28:36,239 some type checking which again avoids 4282 03:28:32,520 --> 03:28:38,960 the unnecessary runtime errors so let me 4283 03:28:36,239 --> 03:28:42,680 go to tutorial and in here I want to 4284 03:28:38,960 --> 03:28:46,239 create a variable and I guess in my case 4285 03:28:42,680 --> 03:28:47,680 I'm going to call it unknown value so 4286 03:28:46,239 --> 03:28:50,239 that will be my 4287 03:28:47,680 --> 03:28:53,439 name and as far as the type I will set 4288 03:28:50,239 --> 03:28:57,720 it equal to unknown so just like with 4289 03:28:53,439 --> 03:29:00,880 any I can go here with unknown value and 4290 03:28:57,720 --> 03:29:04,120 I can set it up for example to be a I 4291 03:29:00,880 --> 03:29:06,680 don't know a hello world so Yep this is 4292 03:29:04,120 --> 03:29:09,760 allowed I can also go to a next line and 4293 03:29:06,680 --> 03:29:12,239 set it equal to for example array that 4294 03:29:09,760 --> 03:29:14,399 is also allowed so let me set up my 4295 03:29:12,239 --> 03:29:17,640 array I'm going to go with numbers okay 4296 03:29:14,399 --> 03:29:21,359 good and then also we can set it up to 4297 03:29:17,640 --> 03:29:24,439 be a number so let me go with 42 and 4298 03:29:21,359 --> 03:29:26,920 then let me add some numbers after your 4299 03:29:24,439 --> 03:29:29,640 D dot so this is allowed now the 4300 03:29:26,920 --> 03:29:32,439 difference is that I cannot go over here 4301 03:29:29,640 --> 03:29:35,080 and say unknown value and then set it 4302 03:29:32,439 --> 03:29:38,120 equal to a fixed as we can see 4303 03:29:35,080 --> 03:29:41,279 typescript complains it says Hey listen 4304 03:29:38,120 --> 03:29:43,399 that type is unknown and right away 4305 03:29:41,279 --> 03:29:45,279 you're running some kind of method how 4306 03:29:43,399 --> 03:29:49,279 am I supposed to know that it's actually 4307 03:29:45,279 --> 03:29:52,319 a number and we can run this particular 4308 03:29:49,279 --> 03:29:55,760 method I mean maybe it's still a string 4309 03:29:52,319 --> 03:29:58,479 so the difference again unlike type any 4310 03:29:55,760 --> 03:30:02,399 before we can use that value if it's 4311 03:29:58,479 --> 03:30:04,399 type unknown we need to check what is 4312 03:30:02,399 --> 03:30:07,960 the type so in this case we can do 4313 03:30:04,399 --> 03:30:11,720 following operation I can go type off 4314 03:30:07,960 --> 03:30:14,560 and then unknown value and I can check 4315 03:30:11,720 --> 03:30:17,000 well if it's a number well then I want 4316 03:30:14,560 --> 03:30:19,439 to run my method so let me move this 4317 03:30:17,000 --> 03:30:22,600 sucker down and notice now everything 4318 03:30:19,439 --> 03:30:25,359 works because in this condition 4319 03:30:22,600 --> 03:30:28,439 typescript knows that yeah unknown value 4320 03:30:25,359 --> 03:30:32,120 is actually a number now one place where 4321 03:30:28,439 --> 03:30:35,160 we'll definitely see type unknown is in 4322 03:30:32,120 --> 03:30:37,640 the try catch block because by default 4323 03:30:35,160 --> 03:30:40,560 typ scrip is going to set this error to 4324 03:30:37,640 --> 03:30:43,479 be unknown now why do we care about that 4325 03:30:40,560 --> 03:30:46,560 well what is typically the first thing 4326 03:30:43,479 --> 03:30:50,160 we do over here go here with log 4327 03:30:46,560 --> 03:30:54,000 and then error and then the message and 4328 03:30:50,160 --> 03:30:56,239 notice how T says well error is type 4329 03:30:54,000 --> 03:30:58,239 unknown so how do you know that there's 4330 03:30:56,239 --> 03:31:00,520 definitely going to be over here this 4331 03:30:58,239 --> 03:31:02,920 message property and this assumption 4332 03:31:00,520 --> 03:31:05,359 comes from the fact that typically 4333 03:31:02,920 --> 03:31:07,640 especially if we're throwing our own 4334 03:31:05,359 --> 03:31:11,080 errors over here we use the following 4335 03:31:07,640 --> 03:31:13,359 code we go with throw new error and 4336 03:31:11,080 --> 03:31:15,439 provide whatever message and in this 4337 03:31:13,359 --> 03:31:18,520 case I'm going to go there was an error 4338 03:31:15,439 --> 03:31:21,080 so this is is a nice assumption but 4339 03:31:18,520 --> 03:31:23,520 please keep in mind that JavaScript can 4340 03:31:21,080 --> 03:31:27,720 also do something like this we can go 4341 03:31:23,520 --> 03:31:29,800 with throw and some string over here and 4342 03:31:27,720 --> 03:31:33,120 I know what you're thinking well there's 4343 03:31:29,800 --> 03:31:36,359 no way I'll do that in try clatch block 4344 03:31:33,120 --> 03:31:39,000 yeah but you might be running some code 4345 03:31:36,359 --> 03:31:41,080 from third party library and at that 4346 03:31:39,000 --> 03:31:42,399 point you don't know whether the error 4347 03:31:41,080 --> 03:31:46,399 you're going to be getting back is going 4348 03:31:42,399 --> 03:31:48,880 to be the instance of the error class or 4349 03:31:46,399 --> 03:31:52,000 it's going to be something else again it 4350 03:31:48,880 --> 03:31:55,199 literally can be a simple string now to 4351 03:31:52,000 --> 03:31:57,960 demonstrate that I will use somewhat of 4352 03:31:55,199 --> 03:32:00,120 a silly example where I'm going to set 4353 03:31:57,960 --> 03:32:03,040 up the function over here I'm going to 4354 03:32:00,120 --> 03:32:06,960 go with run some code so let's imagine 4355 03:32:03,040 --> 03:32:09,960 that we're saving an image in a database 4356 03:32:06,960 --> 03:32:12,040 or I don't know we're posting some data 4357 03:32:09,960 --> 03:32:14,319 somewhere or sending an email or 4358 03:32:12,040 --> 03:32:16,680 whatever and in here I'm just going to 4359 03:32:14,319 --> 03:32:19,680 set it equal to I Rand them then let's 4360 03:32:16,680 --> 03:32:22,160 go with math. random let's invoke that 4361 03:32:19,680 --> 03:32:25,800 and essentially if it's less than 4362 03:32:22,160 --> 03:32:29,120 0.5 then I want to throw a new error if 4363 03:32:25,800 --> 03:32:31,680 not then I'll just throw good old string 4364 03:32:29,120 --> 03:32:36,199 so let's set up over here if 4365 03:32:31,680 --> 03:32:37,920 else and let's check for random if it's 4366 03:32:36,199 --> 03:32:40,439 less than 4367 03:32:37,920 --> 03:32:43,319 0.5 then I want to use the 4368 03:32:40,439 --> 03:32:46,560 seconder let me set it up over here and 4369 03:32:43,319 --> 03:32:48,319 if not then I'll just go with throw and 4370 03:32:46,560 --> 03:32:52,279 we're going to go with 4371 03:32:48,319 --> 03:32:56,439 some error so let me save this and now 4372 03:32:52,279 --> 03:32:59,479 in Tri block let's go with run some code 4373 03:32:56,439 --> 03:33:01,840 and effectively in here in a catch we 4374 03:32:59,479 --> 03:33:04,760 want to check whether the error is the 4375 03:33:01,840 --> 03:33:06,199 instance of the error class and if 4376 03:33:04,760 --> 03:33:08,279 that's the case yeah then we'll have 4377 03:33:06,199 --> 03:33:11,239 access to the message in this case there 4378 03:33:08,279 --> 03:33:14,760 was an error and if not well then we'll 4379 03:33:11,239 --> 03:33:16,439 simply log and provide some hardcoded 4380 03:33:14,760 --> 03:33:17,680 error and this is essentially what 4381 03:33:16,439 --> 03:33:19,520 you're going to do when you're working 4382 03:33:17,680 --> 03:33:21,600 with typescript 4383 03:33:19,520 --> 03:33:24,000 before you do something with that 4384 03:33:21,600 --> 03:33:26,520 message you want to check whether the 4385 03:33:24,000 --> 03:33:29,319 error you're getting is actually the 4386 03:33:26,520 --> 03:33:31,760 instance of the error class so here in 4387 03:33:29,319 --> 03:33:35,560 the cach block we want to go with 4388 03:33:31,760 --> 03:33:38,840 error instance off we're looking for the 4389 03:33:35,560 --> 03:33:42,399 error and then if that's the case yeah 4390 03:33:38,840 --> 03:33:44,479 then tcri knows okay there is a message 4391 03:33:42,399 --> 03:33:47,840 property and here we can go right now 4392 03:33:44,479 --> 03:33:50,880 with log and and we can set up the error 4393 03:33:47,840 --> 03:33:53,439 do message now if it's not the case 4394 03:33:50,880 --> 03:33:55,279 since I already have some text I'll 4395 03:33:53,439 --> 03:33:57,520 simply log the 4396 03:33:55,279 --> 03:34:00,160 error let me log it so now let's 4397 03:33:57,520 --> 03:34:03,399 navigate to the browser and check it out 4398 03:34:00,160 --> 03:34:06,439 depending on that math random either 4399 03:34:03,399 --> 03:34:09,960 we're going to get there was an error or 4400 03:34:06,439 --> 03:34:14,000 we'll have that string some error and 4401 03:34:09,960 --> 03:34:17,279 again just like any type unknown can be 4402 03:34:14,000 --> 03:34:19,960 anything but before we can start using 4403 03:34:17,279 --> 03:34:23,000 type unknown we'll have to do some type 4404 03:34:19,960 --> 03:34:26,319 checking and therefore this is a type 4405 03:34:23,000 --> 03:34:29,239 safe alternative to type any and up next 4406 03:34:26,319 --> 03:34:31,800 let's cover type never which is a 4407 03:34:29,239 --> 03:34:36,000 special type that represents the type of 4408 03:34:31,800 --> 03:34:38,520 values that never occur now I'm not 4409 03:34:36,000 --> 03:34:41,920 going to Sugar Code it especially in the 4410 03:34:38,520 --> 03:34:45,040 beginning this entire concept might seem 4411 03:34:41,920 --> 03:34:47,080 pretty confusing but hopefully after we 4412 03:34:45,040 --> 03:34:50,399 cover one of the most common use cases 4413 03:34:47,080 --> 03:34:54,199 of type never it's going to be easier to 4414 03:34:50,399 --> 03:34:57,239 grasp the main concept of type never for 4415 03:34:54,199 --> 03:35:00,239 starters let's just establish that if we 4416 03:34:57,239 --> 03:35:03,399 have a variable with type never we 4417 03:35:00,239 --> 03:35:07,800 cannot assign value to it so let's say 4418 03:35:03,399 --> 03:35:10,760 if we set up a variable some value and 4419 03:35:07,800 --> 03:35:14,239 if let's say it's zero if I'm going to 4420 03:35:10,760 --> 03:35:17,160 go with type never typt is going to 4421 03:35:14,239 --> 03:35:20,199 complain and I'm going to get the same 4422 03:35:17,160 --> 03:35:22,960 result regardless of the value over here 4423 03:35:20,199 --> 03:35:26,199 so this can be hello world this can be 4424 03:35:22,960 --> 03:35:30,520 an array doesn't really matter we cannot 4425 03:35:26,199 --> 03:35:33,800 assign any value to type never so that's 4426 03:35:30,520 --> 03:35:36,359 the first thing we need to remember 4427 03:35:33,800 --> 03:35:40,680 second you might encounter in your own 4428 03:35:36,359 --> 03:35:43,359 code Type never if you set up one too 4429 03:35:40,680 --> 03:35:45,359 many conditionals speaking from the 4430 03:35:43,359 --> 03:35:48,880 personal experience so let's say you 4431 03:35:45,359 --> 03:35:52,239 have a type theme over here 4432 03:35:48,880 --> 03:35:55,479 and you set it equal to two possible 4433 03:35:52,239 --> 03:35:58,279 values either is going to be light or 4434 03:35:55,479 --> 03:36:01,319 it's going to be dark then let's set up 4435 03:35:58,279 --> 03:36:05,720 a very useful function called check 4436 03:36:01,319 --> 03:36:07,680 theme yes we'll say that theme parameter 4437 03:36:05,720 --> 03:36:09,359 is going to be equal to our type of 4438 03:36:07,680 --> 03:36:10,920 theme now we're not going to return 4439 03:36:09,359 --> 03:36:13,600 anything so I'm just going to set it 4440 03:36:10,920 --> 03:36:15,120 equal to void and then inside of the 4441 03:36:13,600 --> 03:36:17,439 function I'm going to set up two 4442 03:36:15,120 --> 03:36:20,760 conditions I'm going to go with well if 4443 03:36:17,439 --> 03:36:22,760 theme is equal to and then I'm going to 4444 03:36:20,760 --> 03:36:26,120 go with light one and I'm not going to 4445 03:36:22,760 --> 03:36:29,120 be particularly original I'm just going 4446 03:36:26,120 --> 03:36:31,840 to go with light theme and then let's 4447 03:36:29,120 --> 03:36:34,279 return from the function and we already 4448 03:36:31,840 --> 03:36:37,359 know that typescript is super smart it 4449 03:36:34,279 --> 03:36:41,239 knows that at this point theme can only 4450 03:36:37,359 --> 03:36:43,479 be value of dark correct because these 4451 03:36:41,239 --> 03:36:46,000 are the only two options but let's 4452 03:36:43,479 --> 03:36:48,520 imagine scenario where we do add another 4453 03:36:46,000 --> 03:36:53,239 condition I'm going to go with if theme 4454 03:36:48,520 --> 03:36:55,239 is dark okay good then I want to log it 4455 03:36:53,239 --> 03:36:58,000 but I'll change the value and I'll also 4456 03:36:55,239 --> 03:37:00,120 return let me change this one around let 4457 03:36:58,000 --> 03:37:04,199 me go with dark theme and notice 4458 03:37:00,120 --> 03:37:06,880 something interesting if we type theme 4459 03:37:04,199 --> 03:37:11,040 after both of these conditions 4460 03:37:06,880 --> 03:37:14,199 typescript says Hey listen theme is type 4461 03:37:11,040 --> 03:37:16,279 never because we have handled all of the 4462 03:37:14,199 --> 03:37:19,359 possible cases 4463 03:37:16,279 --> 03:37:22,359 so at this point in time there is no 4464 03:37:19,359 --> 03:37:26,120 possible value for the steam so if you 4465 03:37:22,359 --> 03:37:27,920 ever encounter such scenario you'll have 4466 03:37:26,120 --> 03:37:31,000 to go back and look at the original 4467 03:37:27,920 --> 03:37:34,640 value and also all the conditions you're 4468 03:37:31,000 --> 03:37:36,880 setting up because somewhere things are 4469 03:37:34,640 --> 03:37:39,920 going south if you are at the point 4470 03:37:36,880 --> 03:37:42,239 where variable is type never well then 4471 03:37:39,920 --> 03:37:44,960 you already have handled all the 4472 03:37:42,239 --> 03:37:47,760 possible cases and what's really awesome 4473 03:37:44,960 --> 03:37:49,720 we can actually use it to our advantage 4474 03:37:47,760 --> 03:37:53,760 the fact that we cannot assign any 4475 03:37:49,720 --> 03:37:56,279 values to never and also the fact that 4476 03:37:53,760 --> 03:38:01,479 typescript will immediately assign type 4477 03:37:56,279 --> 03:38:03,520 never if we cover all of the possible 4478 03:38:01,479 --> 03:38:05,600 conditions as far as our example we're 4479 03:38:03,520 --> 03:38:07,760 going to go with enom I'm going to set 4480 03:38:05,600 --> 03:38:11,120 it equal to a color we're going to go 4481 03:38:07,760 --> 03:38:13,560 with red and blue so those are two 4482 03:38:11,120 --> 03:38:16,680 possible values now we can assign it to 4483 03:38:13,560 --> 03:38:18,720 a string but there's really no need then 4484 03:38:16,680 --> 03:38:21,840 we want to set up a function in my case 4485 03:38:18,720 --> 03:38:23,680 I'm going to go with get color name as a 4486 03:38:21,840 --> 03:38:25,560 I not I'm just going to set up a switch 4487 03:38:23,680 --> 03:38:28,359 statement where we check for possible 4488 03:38:25,560 --> 03:38:31,600 values but if you work with Redux you'll 4489 03:38:28,359 --> 03:38:34,199 find this example very similar to how we 4490 03:38:31,600 --> 03:38:36,120 set up the Redux store so as far as the 4491 03:38:34,199 --> 03:38:38,439 type for my parameter it's going to be a 4492 03:38:36,120 --> 03:38:40,479 color and inside of this function I 4493 03:38:38,439 --> 03:38:43,120 simply want to go with 4494 03:38:40,479 --> 03:38:46,000 switch I want to check for my color and 4495 03:38:43,120 --> 03:38:47,880 let's set up our cases so at this point 4496 03:38:46,000 --> 03:38:52,600 we have two of them so we're going to go 4497 03:38:47,880 --> 03:38:54,880 with color. red so if the color is red 4498 03:38:52,600 --> 03:38:56,560 what do we do then well then we want to 4499 03:38:54,880 --> 03:38:59,000 go with the return 4500 03:38:56,560 --> 03:39:00,720 Red so let's set up our 4501 03:38:59,000 --> 03:39:03,920 colon 4502 03:39:00,720 --> 03:39:08,040 return and we're returning the string of 4503 03:39:03,920 --> 03:39:10,720 red and if it's blue well then let's 4504 03:39:08,040 --> 03:39:11,920 just change it around so let me select 4505 03:39:10,720 --> 03:39:16,439 these 4506 03:39:11,920 --> 03:39:19,840 two and let me just go with blue then 4507 03:39:16,439 --> 03:39:23,800 let's set up to console logs iess let's 4508 03:39:19,840 --> 03:39:27,279 invoke get color name and we'll pass in 4509 03:39:23,800 --> 03:39:30,800 color then red and we're going to copy 4510 03:39:27,279 --> 03:39:32,199 and paste and set up blue as well and 4511 03:39:30,800 --> 03:39:36,279 everything's awesome if we take a look 4512 03:39:32,199 --> 03:39:39,800 at the console yep we have red and blue 4513 03:39:36,279 --> 03:39:43,960 but what if sometime later we go to a 4514 03:39:39,800 --> 03:39:46,199 color and add green and notice 4515 03:39:43,960 --> 03:39:48,399 everything is valid not breaking any 4516 03:39:46,199 --> 03:39:51,399 rules over here the problem is going to 4517 03:39:48,399 --> 03:39:54,000 be here when we invoke 4518 03:39:51,399 --> 03:39:56,279 it this is going to 4519 03:39:54,000 --> 03:39:59,040 fail but the worst case it's actually 4520 03:39:56,279 --> 03:40:01,880 going to fail silently so if we navigate 4521 03:39:59,040 --> 03:40:04,640 right now to the browser we can see red 4522 03:40:01,880 --> 03:40:06,279 blue and undefined why well because 4523 03:40:04,640 --> 03:40:10,239 we're not handling the case over here 4524 03:40:06,279 --> 03:40:14,680 and again this fails silently so we set 4525 03:40:10,239 --> 03:40:17,840 up nicely an additional enm we correctly 4526 03:40:14,680 --> 03:40:20,880 pass it in and then unexpectedly we're 4527 03:40:17,840 --> 03:40:23,040 getting an error correct and if you're 4528 03:40:20,880 --> 03:40:25,239 familiar with such code you're probably 4529 03:40:23,040 --> 03:40:26,640 screaming at me you're like Hey listen 4530 03:40:25,239 --> 03:40:30,040 just set up the 4531 03:40:26,640 --> 03:40:32,520 default case and throw the error and 4532 03:40:30,040 --> 03:40:35,920 you're correct before typescript that 4533 03:40:32,520 --> 03:40:40,399 was the approach so let's go here with 4534 03:40:35,920 --> 03:40:43,479 default then we go with throw new 4535 03:40:40,399 --> 03:40:46,040 error and for now let's just take a look 4536 03:40:43,479 --> 03:40:48,399 at the color and all of the is nice but 4537 03:40:46,040 --> 03:40:50,880 keep in mind this is again happening at 4538 03:40:48,399 --> 03:40:54,160 runtime so yeah we have this default 4539 03:40:50,880 --> 03:40:57,800 case where we throw the error if none of 4540 03:40:54,160 --> 03:40:59,479 the conditions match but I actually 4541 03:40:57,800 --> 03:41:02,359 would like to do that while I'm writing 4542 03:40:59,479 --> 03:41:04,720 the code not when my application is 4543 03:41:02,359 --> 03:41:08,680 running so we can definitely keep this 4544 03:41:04,720 --> 03:41:12,239 one over here but a typescript approach 4545 03:41:08,680 --> 03:41:15,399 is to come up with the variable and I'm 4546 03:41:12,239 --> 03:41:17,120 going to go with at build time here and 4547 03:41:15,399 --> 03:41:18,800 me add another comment and in here I'm 4548 03:41:17,120 --> 03:41:21,199 going to go with at 4549 03:41:18,800 --> 03:41:24,600 runtime and in here we want to set up 4550 03:41:21,199 --> 03:41:29,120 another variable now the name is really 4551 03:41:24,600 --> 03:41:29,120 up to you but I'm going to go with 4552 03:41:29,479 --> 03:41:34,760 unexpected 4553 03:41:31,120 --> 03:41:37,560 color the type will be equal to a never 4554 03:41:34,760 --> 03:41:39,560 and I will assign it to the color and 4555 03:41:37,560 --> 03:41:43,439 notice how immediately typ script 4556 03:41:39,560 --> 03:41:45,680 complains why well because initially we 4557 03:41:43,439 --> 03:41:47,640 only had two values so if we go back to 4558 03:41:45,680 --> 03:41:51,520 this one everything is 4559 03:41:47,640 --> 03:41:55,199 correct I only have this yellow squiggly 4560 03:41:51,520 --> 03:41:57,199 line which is totally okay but if I'm 4561 03:41:55,199 --> 03:41:59,800 going to add this green one then right 4562 03:41:57,199 --> 03:42:03,479 Away tab script is going to yell at me 4563 03:41:59,800 --> 03:42:05,920 because now I have one more condition 4564 03:42:03,479 --> 03:42:08,359 and of course we already saw that if we 4565 03:42:05,920 --> 03:42:11,399 don't handle it over here then 4566 03:42:08,359 --> 03:42:14,120 essentially we will throw that error at 4567 03:42:11,399 --> 03:42:16,199 runtime so what's the solution well we 4568 03:42:14,120 --> 03:42:18,600 go with another case 4569 03:42:16,199 --> 03:42:21,760 let's go color 4570 03:42:18,600 --> 03:42:25,680 green and same deal just want to go with 4571 03:42:21,760 --> 03:42:28,319 the return and return a string in this 4572 03:42:25,680 --> 03:42:31,960 case it's going to be a green one and as 4573 03:42:28,319 --> 03:42:34,720 you can see the moment we shut up our 4574 03:42:31,960 --> 03:42:38,159 cage types doesn't complain anymore 4575 03:42:34,720 --> 03:42:42,600 everything is fine because we have three 4576 03:42:38,159 --> 03:42:45,560 possible enums and we're handling all of 4577 03:42:42,600 --> 03:42:48,479 those cases in our get color name as 4578 03:42:45,560 --> 03:42:51,840 well so essentially this is a great 4579 03:42:48,479 --> 03:42:55,640 benefit for us where we can check for 4580 03:42:51,840 --> 03:42:58,439 those potential errors at build time 4581 03:42:55,640 --> 03:43:00,359 instead of just runtime okay and up next 4582 03:42:58,439 --> 03:43:03,000 let's switch gears and quickly discuss 4583 03:43:00,359 --> 03:43:06,520 modules and typescript now if you are 4584 03:43:03,000 --> 03:43:08,680 familiar with how es6 modules work most 4585 03:43:06,520 --> 03:43:12,680 of the things we're about to cover will 4586 03:43:08,680 --> 03:43:14,560 be a nice review but when it comes to 4587 03:43:12,680 --> 03:43:16,000 modules and typescript there are some 4588 03:43:14,560 --> 03:43:18,279 GES 4589 03:43:16,000 --> 03:43:21,399 we need to be aware of and let's start 4590 03:43:18,279 --> 03:43:26,040 with the main one you see by default in 4591 03:43:21,399 --> 03:43:29,399 typescript files are not modules they're 4592 03:43:26,040 --> 03:43:32,319 actually treated as script in a global 4593 03:43:29,399 --> 03:43:35,239 scope so what does that mean well if I 4594 03:43:32,319 --> 03:43:38,720 go to a source come up with a name for 4595 03:43:35,239 --> 03:43:41,399 example actions TS come up with some 4596 03:43:38,720 --> 03:43:44,199 random variables one is going to be 4597 03:43:41,399 --> 03:43:49,080 Susan then second one is going to be 4598 03:43:44,199 --> 03:43:53,359 something so let something is equal to 4599 03:43:49,080 --> 03:43:56,399 something if I create another file or in 4600 03:43:53,359 --> 03:43:57,920 the tutorial try to use the same 4601 03:43:56,399 --> 03:44:00,680 variable 4602 03:43:57,920 --> 03:44:03,880 name not going to be successful so 4603 03:44:00,680 --> 03:44:07,680 typescript is actually going to complain 4604 03:44:03,880 --> 03:44:10,760 so it doesn't treat this file as a 4605 03:44:07,680 --> 03:44:14,000 module so whatever I set up in my files 4606 03:44:10,760 --> 03:44:18,040 is actually put in a global scope which 4607 03:44:14,000 --> 03:44:20,319 obviously limits the variables I can use 4608 03:44:18,040 --> 03:44:22,640 I have Susan over here and I also have 4609 03:44:20,319 --> 03:44:24,800 Susan in the actions and therefore 4610 03:44:22,640 --> 03:44:27,359 typescript is complaining so pretty much 4611 03:44:24,800 --> 03:44:30,560 think of it this way everything that you 4612 03:44:27,359 --> 03:44:32,960 put in the files by default again we'll 4613 03:44:30,560 --> 03:44:36,080 fix that in a second but by default was 4614 03:44:32,960 --> 03:44:40,640 actually put in the global 4615 03:44:36,080 --> 03:44:43,000 scope so in order to fix it we have two 4616 03:44:40,640 --> 03:44:46,680 options we can go to TS 4617 03:44:43,000 --> 03:44:49,600 config and essentially set the property 4618 03:44:46,680 --> 03:44:51,560 or we can add the export and import and 4619 03:44:49,600 --> 03:44:55,359 the moment we add export and import then 4620 03:44:51,560 --> 03:44:57,920 they're treated like typical es6 modules 4621 03:44:55,359 --> 03:45:01,040 now also don't be surprised about this 4622 03:44:57,920 --> 03:45:04,720 gotcha where let's say in 4623 03:45:01,040 --> 03:45:07,319 tutorial you'll try to come up with the 4624 03:45:04,720 --> 03:45:09,560 variable with the name of name and you 4625 03:45:07,319 --> 03:45:12,159 won't be able to even though technically 4626 03:45:09,560 --> 03:45:16,520 in actions we don't have this value over 4627 03:45:12,159 --> 03:45:18,479 here you see such name is used in 4628 03:45:16,520 --> 03:45:20,479 declaration file again something we're 4629 03:45:18,479 --> 03:45:22,720 going to cover a little bit 4630 03:45:20,479 --> 03:45:25,000 later and again we have this name 4631 03:45:22,720 --> 03:45:27,800 Collision because typescript doesn't 4632 03:45:25,000 --> 03:45:30,359 treat this file as a module so 4633 03:45:27,800 --> 03:45:31,920 essentially unless we change that 4634 03:45:30,359 --> 03:45:34,279 there's going to be some keywords that 4635 03:45:31,920 --> 03:45:36,720 we cannot use and this was 4636 03:45:34,279 --> 03:45:39,239 super confusing to me in the beginning 4637 03:45:36,720 --> 03:45:41,920 because as I started learning typescript 4638 03:45:39,239 --> 03:45:44,239 I wanted to use this name variable and I 4639 03:45:41,920 --> 03:45:45,800 got this red Squigly line and I was like 4640 03:45:44,239 --> 03:45:48,000 what's happening over here why I cannot 4641 03:45:45,800 --> 03:45:50,800 use this name one I mean there's 4642 03:45:48,000 --> 03:45:52,960 technically no error and again let me 4643 03:45:50,800 --> 03:45:55,840 repeat for a third time it's because by 4644 03:45:52,960 --> 03:46:00,199 default typescript is going to 4645 03:45:55,840 --> 03:46:03,279 essentially treat the files as just 4646 03:46:00,199 --> 03:46:05,720 scripts in the global scope so once we 4647 03:46:03,279 --> 03:46:09,319 know the issue how we can fix that like 4648 03:46:05,720 --> 03:46:12,479 I said we have two options over here we 4649 03:46:09,319 --> 03:46:14,279 can either go to TS config and add the 4650 03:46:12,479 --> 03:46:16,439 property and you know what let me just 4651 03:46:14,279 --> 03:46:19,800 first showcase is how this would work 4652 03:46:16,439 --> 03:46:23,040 with Import and Export if we add export 4653 03:46:19,800 --> 03:46:25,840 or import statement to our file 4654 03:46:23,040 --> 03:46:28,279 immediately typescript is going to 4655 03:46:25,840 --> 03:46:30,800 essentially treat this as a module so 4656 03:46:28,279 --> 03:46:33,080 for example if I'm going to go back to a 4657 03:46:30,800 --> 03:46:37,159 tutorial and if I'm going to go with 4658 03:46:33,080 --> 03:46:39,880 import and I believe this one was the 4659 03:46:37,159 --> 03:46:42,840 named export so we're going to go with 4660 03:46:39,880 --> 03:46:45,840 something from the actions everything is 4661 03:46:42,840 --> 03:46:49,319 awesome so notice we don't have the 4662 03:46:45,840 --> 03:46:53,920 issue anymore with the global scope 4663 03:46:49,319 --> 03:46:56,560 correct so now these ones are scoped to 4664 03:46:53,920 --> 03:46:59,640 tutorial and then these ones are scoped 4665 03:46:56,560 --> 03:47:02,680 to the actions hopefully that is clear 4666 03:46:59,640 --> 03:47:06,040 so this is one approach now what if I 4667 03:47:02,680 --> 03:47:09,199 don't want to add Import and Export like 4668 03:47:06,040 --> 03:47:12,199 force it in a way well then we can go 4669 03:47:09,199 --> 03:47:14,920 over here to TS config and we're looking 4670 03:47:12,199 --> 03:47:18,080 for the property by the name of my 4671 03:47:14,920 --> 03:47:20,800 module detection and we want to go with 4672 03:47:18,080 --> 03:47:23,439 Force so by default it's going to be set 4673 03:47:20,800 --> 03:47:27,560 to Auto but we want to force it so in 4674 03:47:23,439 --> 03:47:30,040 this case even if there is no export 4675 03:47:27,560 --> 03:47:31,760 statement everything is going to work so 4676 03:47:30,040 --> 03:47:33,720 notice now of course I have different 4677 03:47:31,760 --> 03:47:36,359 complaint because I'm importing from the 4678 03:47:33,720 --> 03:47:39,840 actions and there is no export but the 4679 03:47:36,359 --> 03:47:41,159 moment I save I can use the name even 4680 03:47:39,840 --> 03:47:43,840 though it's technically used in 4681 03:47:41,159 --> 03:47:45,439 declaration file which was the issue 4682 03:47:43,840 --> 03:47:47,840 before 4683 03:47:45,439 --> 03:47:50,319 and I don't have this name collision 4684 03:47:47,840 --> 03:47:53,199 with actions anymore as well so I have 4685 03:47:50,319 --> 03:47:55,479 Susan over here and the same is in 4686 03:47:53,199 --> 03:47:59,120 actions again few 4687 03:47:55,479 --> 03:48:02,520 approaches we can either add the 4688 03:47:59,120 --> 03:48:05,319 property in TS config or if we add 4689 03:48:02,520 --> 03:48:07,800 Import and Export then immediately 4690 03:48:05,319 --> 03:48:10,359 typescript is going to treat it as a 4691 03:48:07,800 --> 03:48:13,080 module okay and once we're familiar with 4692 03:48:10,359 --> 03:48:16,760 the main gotcha let's just see how we 4693 03:48:13,080 --> 03:48:18,760 can use es6 modules in typescript and 4694 03:48:16,760 --> 03:48:21,000 like I mentioned before if you are 4695 03:48:18,760 --> 03:48:24,279 familiar with the main 4696 03:48:21,000 --> 03:48:27,159 Concepts this is just going to be a nice 4697 03:48:24,279 --> 03:48:30,080 review now before we start typing away I 4698 03:48:27,159 --> 03:48:33,720 do want to mention one property in the 4699 03:48:30,080 --> 03:48:36,239 TS config which is module and at the 4700 03:48:33,720 --> 03:48:42,000 moment notice how we have the value of 4701 03:48:36,239 --> 03:48:45,040 es next now this property controls what 4702 03:48:42,000 --> 03:48:48,239 module syntax is set up in the out put 4703 03:48:45,040 --> 03:48:51,000 code so effectively once the typescript 4704 03:48:48,239 --> 03:48:53,760 is done compiling our code so if I 4705 03:48:51,000 --> 03:48:56,000 navigate to a playground notice I have 4706 03:48:53,760 --> 03:48:58,239 my export statement over here where I'm 4707 03:48:56,000 --> 03:49:01,479 exporting some function and this is 4708 03:48:58,239 --> 03:49:04,720 going to be the result now if I go to TS 4709 03:49:01,479 --> 03:49:07,439 config and change from es next to for 4710 03:49:04,720 --> 03:49:08,960 example commonjs and as a sign not if 4711 03:49:07,439 --> 03:49:12,479 you're not familiar with 4712 03:49:08,960 --> 03:49:15,680 commonjs it used to be the main module 4713 03:49:12,479 --> 03:49:19,680 syntax in node.js but even no. JS is 4714 03:49:15,680 --> 03:49:23,560 right now slowly but surely switching to 4715 03:49:19,680 --> 03:49:26,159 a es6 module syntax and notice the 4716 03:49:23,560 --> 03:49:29,399 moment we change it actually this will 4717 03:49:26,159 --> 03:49:33,159 be the output so this is a common JS 4718 03:49:29,399 --> 03:49:37,000 syntax so if you ever have a use case 4719 03:49:33,159 --> 03:49:39,600 where you want to use different module 4720 03:49:37,000 --> 03:49:42,720 syntax in the output code just look for 4721 03:49:39,600 --> 03:49:46,560 the module property something I wanted 4722 03:49:42,720 --> 03:49:50,560 to mention and now just go to actions 4723 03:49:46,560 --> 03:49:53,439 and set up few values we want to export 4724 03:49:50,560 --> 03:49:56,319 and then in tutorial we will import them 4725 03:49:53,439 --> 03:49:59,159 again just to have a nice review how the 4726 03:49:56,319 --> 03:50:00,840 es6 modules essentially work just so you 4727 03:49:59,159 --> 03:50:04,399 can see that pretty much the idea is 4728 03:50:00,840 --> 03:50:07,439 exactly the same first I want to go with 4729 03:50:04,399 --> 03:50:09,439 um some kind of function I'm going to go 4730 03:50:07,439 --> 03:50:12,840 with say 4731 03:50:09,439 --> 03:50:15,880 hello let's set up name equal to a 4732 03:50:12,840 --> 03:50:18,080 string and you know what I'll also say 4733 03:50:15,880 --> 03:50:21,359 that we're not returning anything from 4734 03:50:18,080 --> 03:50:24,840 this function in here we're going to set 4735 03:50:21,359 --> 03:50:26,720 up a nice console log we're going to go 4736 03:50:24,840 --> 03:50:30,760 with 4737 03:50:26,720 --> 03:50:31,880 hello and let's access the name so 4738 03:50:30,760 --> 03:50:35,120 nothing 4739 03:50:31,880 --> 03:50:39,319 really groundbreaking then let's also 4740 03:50:35,120 --> 03:50:43,279 export let person is equal to 4741 03:50:39,319 --> 03:50:46,239 Susan and let's also set up a new 4742 03:50:43,279 --> 03:50:48,680 student so this will be my object and 4743 03:50:46,239 --> 03:50:50,399 there's going to be a type for this 4744 03:50:48,680 --> 03:50:52,920 student so new 4745 03:50:50,399 --> 03:50:55,199 student I don't have the type yet so let 4746 03:50:52,920 --> 03:51:01,159 me just type over here 4747 03:50:55,199 --> 03:51:02,960 Peter and age let's set it equal to 24 4748 03:51:01,159 --> 03:51:04,279 and you know what let's do set up to 4749 03:51:02,960 --> 03:51:06,760 type because I want to showcase how we 4750 03:51:04,279 --> 03:51:08,080 can nicely export type so for now let's 4751 03:51:06,760 --> 03:51:10,319 just go with 4752 03:51:08,080 --> 03:51:15,920 type 4753 03:51:10,319 --> 03:51:19,920 student and name is equal to a string 4754 03:51:15,920 --> 03:51:21,800 then age is equal to not string sorry 4755 03:51:19,920 --> 03:51:25,760 I'm going to go with number and then new 4756 03:51:21,800 --> 03:51:28,359 student is going to be type student and 4757 03:51:25,760 --> 03:51:30,319 now since I already have two named 4758 03:51:28,359 --> 03:51:32,720 exports I just want to showcase how we 4759 03:51:30,319 --> 03:51:36,239 can do the default one as well so we're 4760 03:51:32,720 --> 03:51:39,479 going to go with export default then new 4761 03:51:36,239 --> 03:51:42,000 student and essentially call it a day so 4762 03:51:39,479 --> 03:51:44,159 now I want to navigate to tutorial and 4763 03:51:42,000 --> 03:51:46,760 one by one let's grab all of those 4764 03:51:44,159 --> 03:51:48,239 values since I have the default export 4765 03:51:46,760 --> 03:51:50,720 I'm going to go with new student which 4766 03:51:48,239 --> 03:51:53,520 is coming from the actions of course we 4767 03:51:50,720 --> 03:51:55,560 can use a different name but I'm not 4768 03:51:53,520 --> 03:51:58,479 going to do that and then for the name 4769 03:51:55,560 --> 03:52:01,319 ones we're going to go and say hello and 4770 03:51:58,479 --> 03:52:05,600 also we'll set up the 4771 03:52:01,319 --> 03:52:09,359 person one second yep I want to save and 4772 03:52:05,600 --> 03:52:11,279 let me open up the tutorial over here so 4773 03:52:09,359 --> 03:52:14,920 these are the things we are importing 4774 03:52:11,279 --> 03:52:19,319 right now let's invoke say hello we're 4775 03:52:14,920 --> 03:52:21,520 going to go typescript okay good then 4776 03:52:19,319 --> 03:52:26,000 let's log the new 4777 03:52:21,520 --> 03:52:29,120 student and also a person just so we can 4778 03:52:26,000 --> 03:52:31,600 see whether we're getting the correct 4779 03:52:29,120 --> 03:52:33,720 values and then we'll talk about the 4780 03:52:31,600 --> 03:52:36,479 types and how we can export them so 4781 03:52:33,720 --> 03:52:41,640 hello typescript I have Peter as well as 4782 03:52:36,479 --> 03:52:44,720 the Susan so everything works as we 4783 03:52:41,640 --> 03:52:48,239 expect and as far as the types yes we 4784 03:52:44,720 --> 03:52:51,439 can nicely export types as well so in 4785 03:52:48,239 --> 03:52:54,600 here I can go with export then type 4786 03:52:51,439 --> 03:52:56,359 student and then back in tutorial I have 4787 03:52:54,600 --> 03:52:58,680 few options I can either go here with 4788 03:52:56,359 --> 03:53:02,439 the student notice how I'm getting right 4789 03:52:58,680 --> 03:53:04,720 away a nice suggestion or we can also 4790 03:53:02,439 --> 03:53:06,399 add over here this type again this 4791 03:53:04,720 --> 03:53:08,600 doesn't change anything as far as the 4792 03:53:06,399 --> 03:53:12,600 functionality it's just 4793 03:53:08,600 --> 03:53:15,520 a common approach to signal that this is 4794 03:53:12,600 --> 03:53:18,479 a type we're importing 4795 03:53:15,520 --> 03:53:21,159 so this clearly signals to me that hey 4796 03:53:18,479 --> 03:53:25,000 this is a type that I can use in this 4797 03:53:21,159 --> 03:53:30,120 file so for example I can go here with 4798 03:53:25,000 --> 03:53:31,359 const another student set it equal to my 4799 03:53:30,120 --> 03:53:34,359 student 4800 03:53:31,359 --> 03:53:36,239 type and let me add different values so 4801 03:53:34,359 --> 03:53:41,560 this will be equal to 4802 03:53:36,239 --> 03:53:43,920 Bob me add an equal sign over here comma 4803 03:53:41,560 --> 03:53:47,960 and we're also going to set up the age 4804 03:53:43,920 --> 03:53:50,279 of 20 three and then if I log another 4805 03:53:47,960 --> 03:53:52,399 student we can clearly see that we don't 4806 03:53:50,279 --> 03:53:55,720 have any typescript errors and then in a 4807 03:53:52,399 --> 03:53:57,760 console we should have the correct value 4808 03:53:55,720 --> 03:54:01,479 now million doll 4809 03:53:57,760 --> 03:54:06,040 question what is the correct quote 4810 03:54:01,479 --> 03:54:08,439 unquote approach should we set up the 4811 03:54:06,040 --> 03:54:12,720 types in a separate file and then import 4812 03:54:08,439 --> 03:54:17,600 it or we keep it in the file where we're 4813 03:54:12,720 --> 03:54:19,399 actually using the type and as always 4814 03:54:17,600 --> 03:54:21,720 the answer is probably not something 4815 03:54:19,399 --> 03:54:24,840 you're looking for it really comes down 4816 03:54:21,720 --> 03:54:28,080 to your preference yes definitely you 4817 03:54:24,840 --> 03:54:31,120 can set up over here a new file call it 4818 03:54:28,080 --> 03:54:32,760 types DS and then pretty much every time 4819 03:54:31,120 --> 03:54:36,439 you have a 4820 03:54:32,760 --> 03:54:39,640 type in your project set it up in the 4821 03:54:36,439 --> 03:54:41,960 types and then import in all the files 4822 03:54:39,640 --> 03:54:44,120 where you're using that value it's 4823 03:54:41,960 --> 03:54:46,399 definitely an option you can definitely 4824 03:54:44,120 --> 03:54:50,720 do do that and another approach is to 4825 03:54:46,399 --> 03:54:53,880 keep the type in the file where you 4826 03:54:50,720 --> 03:54:56,840 initially are using it and then if you 4827 03:54:53,880 --> 03:55:00,319 ever have a use case like in our case in 4828 03:54:56,840 --> 03:55:04,399 tutorial you export and then grab it in 4829 03:55:00,319 --> 03:55:06,319 tutorial and a common scenario is that 4830 03:55:04,399 --> 03:55:09,199 you initially just set up the type in 4831 03:55:06,319 --> 03:55:13,239 the file and then if you see that you 4832 03:55:09,199 --> 03:55:15,040 are reusing it all over your project 4833 03:55:13,239 --> 03:55:19,000 then yeah at that point it kind of makes 4834 03:55:15,040 --> 03:55:20,760 sense to keep it in one place since the 4835 03:55:19,000 --> 03:55:23,040 Imports are going to be easier you won't 4836 03:55:20,760 --> 03:55:26,720 have to think about well where is the 4837 03:55:23,040 --> 03:55:30,120 actual type located but again as always 4838 03:55:26,720 --> 03:55:31,880 it really comes down to your preference 4839 03:55:30,120 --> 03:55:35,199 all right and while we're still on a 4840 03:55:31,880 --> 03:55:39,120 topic of modules let's also see how 4841 03:55:35,199 --> 03:55:41,680 typescript treats JavaScript files so 4842 03:55:39,120 --> 03:55:43,840 remember at the very very beginning of 4843 03:55:41,680 --> 03:55:45,120 tutorial I mentioned that whenever we 4844 03:55:43,840 --> 03:55:47,279 work with 4845 03:55:45,120 --> 03:55:49,080 typescript in our case since we're 4846 03:55:47,279 --> 03:55:53,159 working with JavaScript files and react 4847 03:55:49,080 --> 03:55:56,600 files we need to stick with either do DS 4848 03:55:53,159 --> 03:55:58,680 extension or DSX 4849 03:55:56,600 --> 03:56:01,120 extension and now let's see how 4850 03:55:58,680 --> 03:56:03,359 typescript is going to treat the file if 4851 03:56:01,120 --> 03:56:05,479 it's just a JS extension so let's say 4852 03:56:03,359 --> 03:56:08,960 I'm going to go with source and we're 4853 03:56:05,479 --> 03:56:11,960 going to create the example 4854 03:56:08,960 --> 03:56:15,000 JS and let's go with 4855 03:56:11,960 --> 03:56:17,920 export some value 4856 03:56:15,000 --> 03:56:20,520 that is equal to some value everything 4857 03:56:17,920 --> 03:56:22,720 is awesome I'm just missing over here 4858 03:56:20,520 --> 03:56:26,880 the keyword in this case I'm going to go 4859 03:56:22,720 --> 03:56:28,720 with let and now if we go to tutorial 4860 03:56:26,880 --> 03:56:30,880 what do you think is going to happen how 4861 03:56:28,720 --> 03:56:34,120 do you think typescript is going to 4862 03:56:30,880 --> 03:56:35,319 treat this file let's go with import 4863 03:56:34,120 --> 03:56:38,000 some 4864 03:56:35,319 --> 03:56:41,800 value and it's coming 4865 03:56:38,000 --> 03:56:46,120 from and I don't see essentially it in 4866 03:56:41,800 --> 03:56:49,960 my suggestions so I can try to force it 4867 03:56:46,120 --> 03:56:53,920 I can say over here example JS but not 4868 03:56:49,960 --> 03:56:56,080 going to be successful so effectively 4869 03:56:53,920 --> 03:56:58,720 typescript is complaining that it 4870 03:56:56,080 --> 03:57:00,359 couldn't find the file now the reason 4871 03:56:58,720 --> 03:57:02,319 for that is because it's not a 4872 03:57:00,359 --> 03:57:04,760 typescript file so here's the thing 4873 03:57:02,319 --> 03:57:08,040 about typescript it pretty much needs to 4874 03:57:04,760 --> 03:57:09,720 know everything about your project and 4875 03:57:08,040 --> 03:57:11,319 this is something that we're actually 4876 03:57:09,720 --> 03:57:14,640 going to come back to when we start 4877 03:57:11,319 --> 03:57:16,960 working with the third party libraries 4878 03:57:14,640 --> 03:57:18,600 in our project essentially when we start 4879 03:57:16,960 --> 03:57:21,840 using the code that's coming from 4880 03:57:18,600 --> 03:57:25,560 somewhere else so as far as typescript 4881 03:57:21,840 --> 03:57:28,600 is concern no we need to set this one up 4882 03:57:25,560 --> 03:57:31,640 as a typescript file so then it clearly 4883 03:57:28,600 --> 03:57:33,600 knows well what is the shape for the 4884 03:57:31,640 --> 03:57:35,960 data we're getting because at the moment 4885 03:57:33,600 --> 03:57:38,359 typescript is like hey I don't know what 4886 03:57:35,960 --> 03:57:43,120 are you getting over here I have no info 4887 03:57:38,359 --> 03:57:45,600 on this sum value now we can force it 4888 03:57:43,120 --> 03:57:49,000 there's a property we can add in the 4889 03:57:45,600 --> 03:57:52,439 typescript config just keep in mind that 4890 03:57:49,000 --> 03:57:55,760 typescripts ability to infert types it's 4891 03:57:52,439 --> 03:57:58,319 not going to be as robust as when we're 4892 03:57:55,760 --> 03:58:01,800 working with the typescript file so yes 4893 03:57:58,319 --> 03:58:05,359 we can navigate right now to TS config 4894 03:58:01,800 --> 03:58:06,680 we can look for compiler options and we 4895 03:58:05,359 --> 03:58:09,439 can go with 4896 03:58:06,680 --> 03:58:11,960 allowjs and essentially we can set it 4897 03:58:09,439 --> 03:58:14,800 equal to true now we do need to add a 4898 03:58:11,960 --> 03:58:17,920 Comm over here and now everything 4899 03:58:14,800 --> 03:58:21,040 magically Works in our tutorial so 4900 03:58:17,920 --> 03:58:23,159 essentially typescript can infer that 4901 03:58:21,040 --> 03:58:25,880 some value is actually a string but just 4902 03:58:23,159 --> 03:58:27,520 be careful with such approach 4903 03:58:25,880 --> 03:58:30,120 effectively since we're working on a 4904 03:58:27,520 --> 03:58:32,640 typescript project yes we do want to 4905 03:58:30,120 --> 03:58:35,479 name our files 4906 03:58:32,640 --> 03:58:37,520 TS because then typescript is going to 4907 03:58:35,479 --> 03:58:39,720 know that it's a typescript file and 4908 03:58:37,520 --> 03:58:41,199 it's going to have the info on the shape 4909 03:58:39,720 --> 03:58:44,680 of the 4910 03:58:41,199 --> 03:58:48,399 data and if you'll just set up 4911 03:58:44,680 --> 03:58:51,760 JS file without making any changes to 4912 03:58:48,399 --> 03:58:53,479 the DS config try to run it typescript 4913 03:58:51,760 --> 03:58:57,920 will complain basically it's going to 4914 03:58:53,479 --> 03:59:01,120 say Hey listen I'm not sure what are you 4915 03:58:57,920 --> 03:59:05,199 exporting what's happening over here I'm 4916 03:59:01,120 --> 03:59:08,159 not okay with this so let me remove and 4917 03:59:05,199 --> 03:59:10,239 delete this import as well and with this 4918 03:59:08,159 --> 03:59:12,800 in place now we can move on to the next 4919 03:59:10,239 --> 03:59:15,359 topic and up next let's explore the 4920 03:59:12,800 --> 03:59:17,640 tyard options available able in 4921 03:59:15,359 --> 03:59:20,399 typescript basically my goal was to 4922 03:59:17,640 --> 03:59:22,840 gather all of our options in one place 4923 03:59:20,399 --> 03:59:25,040 and since we already discussed most of 4924 03:59:22,840 --> 03:59:28,359 them throughout the course this section 4925 03:59:25,040 --> 03:59:31,199 will primarily feature challenges so if 4926 03:59:28,359 --> 03:59:33,120 you're interested read the steps and 4927 03:59:31,199 --> 03:59:36,000 attempt to complete the challenge before 4928 03:59:33,120 --> 03:59:38,920 watching the video also for some 4929 03:59:36,000 --> 03:59:42,520 challenges I already provided star code 4930 03:59:38,920 --> 03:59:44,680 so we can spend more time on the typ 4931 03:59:42,520 --> 03:59:48,560 guard Concepts and we're going to start 4932 03:59:44,680 --> 03:59:51,640 with the good old type off typ guard and 4933 03:59:48,560 --> 03:59:54,840 this is going to be our star code where 4934 03:59:51,640 --> 03:59:58,439 we have a type it's either string number 4935 03:59:54,840 --> 04:00:02,439 or Boolean we assign it to a variable 4936 03:59:58,439 --> 04:00:06,319 and then we just invoke math. random and 4937 04:00:02,439 --> 04:00:08,680 then depending on the random value if 4938 04:00:06,319 --> 04:00:12,040 it's less than 4939 04:00:08,680 --> 04:00:17,560 0.33 then the value is going to be low 4940 04:00:12,040 --> 04:00:21,520 if it's bigger than 0.33 but less than 4941 04:00:17,560 --> 04:00:25,199 0.66 then it's going to be a number and 4942 04:00:21,520 --> 04:00:29,560 if it's actually bigger than 4943 04:00:25,199 --> 04:00:33,040 0.66 then it will be a bullion and our 4944 04:00:29,560 --> 04:00:35,239 goal is to set up a function in my case 4945 04:00:33,040 --> 04:00:38,479 I'm going to call this check value that 4946 04:00:35,239 --> 04:00:41,199 takes in one parameter value and the 4947 04:00:38,479 --> 04:00:44,720 type will be this value type and instead 4948 04:00:41,199 --> 04:00:47,439 of the function effectively we want to 4949 04:00:44,720 --> 04:00:50,520 check for all of these conditions for 4950 04:00:47,439 --> 04:00:52,439 the string number and Boolean and based 4951 04:00:50,520 --> 04:00:54,199 on that we want to do something again it 4952 04:00:52,439 --> 04:00:57,399 doesn't really matter what we do as far 4953 04:00:54,199 --> 04:01:00,760 as the functionality but we do want to 4954 04:00:57,399 --> 04:01:03,479 set up those typ guards correctly so let 4955 04:01:00,760 --> 04:01:06,479 me just take the star code just so we 4956 04:01:03,479 --> 04:01:09,080 can spend more time on the actual 4957 04:01:06,479 --> 04:01:11,560 function let me set it up here in 4958 04:01:09,080 --> 04:01:13,640 tutorial and let me start working on the 4959 04:01:11,560 --> 04:01:15,159 function so we want to go here with 4960 04:01:13,640 --> 04:01:18,399 function 4961 04:01:15,159 --> 04:01:21,279 then check value is we'll be looking for 4962 04:01:18,399 --> 04:01:24,000 the value parameter and I'll right away 4963 04:01:21,279 --> 04:01:26,479 set it equal to a value type now this 4964 04:01:24,000 --> 04:01:30,479 function is not going to return 4965 04:01:26,479 --> 04:01:33,159 anything at least in my setup and then 4966 04:01:30,479 --> 04:01:36,640 let's just set up those typ guards and 4967 04:01:33,159 --> 04:01:39,880 remember one of the most straightforward 4968 04:01:36,640 --> 04:01:42,840 type guards we have is type off and then 4969 04:01:39,880 --> 04:01:46,439 we have bunch of possible values so if I 4970 04:01:42,840 --> 04:01:48,880 go here with if then type off I'm 4971 04:01:46,439 --> 04:01:53,239 looking for my parameter and I set it 4972 04:01:48,880 --> 04:01:55,399 equal to and in here we should at least 4973 04:01:53,239 --> 04:01:58,880 get a list now the reason why we don't 4974 04:01:55,399 --> 04:02:01,000 have the list is because I have three 4975 04:01:58,880 --> 04:02:04,319 equal signs over here so we have this 4976 04:02:01,000 --> 04:02:06,159 big in fion function and you can read 4977 04:02:04,319 --> 04:02:09,880 the rest so let's start with the string 4978 04:02:06,159 --> 04:02:12,359 one over here and I guess since people 4979 04:02:09,880 --> 04:02:14,680 are tired with the uppercase when don't 4980 04:02:12,359 --> 04:02:18,560 we go with log 4981 04:02:14,680 --> 04:02:22,439 and value two and let's try the lower 4982 04:02:18,560 --> 04:02:24,080 case somewhere to lower case okay good 4983 04:02:22,439 --> 04:02:26,520 let's 4984 04:02:24,080 --> 04:02:30,159 return so we want to set up the return 4985 04:02:26,520 --> 04:02:31,120 here then let's check for the number and 4986 04:02:30,159 --> 04:02:35,080 if 4987 04:02:31,120 --> 04:02:37,439 we bypass both of these conditions then 4988 04:02:35,080 --> 04:02:41,199 logic tells us that the value is going 4989 04:02:37,439 --> 04:02:42,880 to be bullan so I guess you know what 4990 04:02:41,199 --> 04:02:45,040 order to speed this up I'll just copy 4991 04:02:42,880 --> 04:02:49,520 and paste in this case I'm not looking 4992 04:02:45,040 --> 04:02:53,840 for string we're looking for number and 4993 04:02:49,520 --> 04:02:56,520 then we want to go with two and fixed 4994 04:02:53,840 --> 04:02:59,640 and let's pass in two again we want to 4995 04:02:56,520 --> 04:03:03,199 return and at this point it is a Boolean 4996 04:02:59,640 --> 04:03:06,439 so let's go here with log and let's just 4997 04:03:03,199 --> 04:03:08,880 log the value so let's access the value 4998 04:03:06,439 --> 04:03:11,840 and let's invoke the 4999 04:03:08,880 --> 04:03:13,800 function so let's go with check value 5000 04:03:11,840 --> 04:03:17,439 and pass in the value now let me 5001 04:03:13,800 --> 04:03:20,080 navigate to the browser notice at this 5002 04:03:17,439 --> 04:03:22,880 point it's a number then I have hello 5003 04:03:20,080 --> 04:03:25,080 and at this point it's a Boolean and 5004 04:03:22,880 --> 04:03:28,239 with this in place we can move on to the 5005 04:03:25,080 --> 04:03:30,319 next typ guard all right and the next 5006 04:03:28,239 --> 04:03:34,640 typ Guard we're going to discuss is 5007 04:03:30,319 --> 04:03:37,080 going to be equality narrowing now don't 5008 04:03:34,640 --> 04:03:39,600 be scared of the fancy name essentially 5009 04:03:37,080 --> 04:03:44,040 is just a narrowing which occurs when we 5010 04:03:39,600 --> 04:03:47,680 use equality checks now I think will 5011 04:03:44,040 --> 04:03:50,920 right away also go with the check for 5012 04:03:47,680 --> 04:03:54,000 property typ guard because effectively 5013 04:03:50,920 --> 04:03:56,439 we'll have the same starring code so we 5014 04:03:54,000 --> 04:04:00,040 have these two types one is dog one is 5015 04:03:56,439 --> 04:04:03,640 cat and in here we have the type 5016 04:04:00,040 --> 04:04:07,120 property which references the actual 5017 04:04:03,640 --> 04:04:09,279 type and also we have the name as well 5018 04:04:07,120 --> 04:04:13,000 as the method which is unique to that 5019 04:04:09,279 --> 04:04:15,840 specific type and essentially our goal 5020 04:04:13,000 --> 04:04:18,199 is to come up up with a function which 5021 04:04:15,840 --> 04:04:20,319 is going to check for that specific type 5022 04:04:18,199 --> 04:04:23,319 and if it's a dog then we bark and if 5023 04:04:20,319 --> 04:04:25,199 it's a cat then we meow and like I 5024 04:04:23,319 --> 04:04:26,800 mentioned before we'll pretty much work 5025 04:04:25,199 --> 04:04:29,239 on both of the challenges since the 5026 04:04:26,800 --> 04:04:32,279 starting code is the same and we just 5027 04:04:29,239 --> 04:04:36,120 have multiple options how we can narrow 5028 04:04:32,279 --> 04:04:39,000 down one of the types so in tutorial I 5029 04:04:36,120 --> 04:04:42,720 want to grab this code notice how we're 5030 04:04:39,000 --> 04:04:46,040 using over here the Union as well so now 5031 04:04:42,720 --> 04:04:48,319 we have this animal which could be a dog 5032 04:04:46,040 --> 04:04:51,399 or a cat so now let's set up our 5033 04:04:48,319 --> 04:04:55,199 function which makes the sound so make 5034 04:04:51,399 --> 04:04:57,880 sound function we're going to go with 5035 04:04:55,199 --> 04:05:01,040 animal so that is going to be our 5036 04:04:57,880 --> 04:05:04,439 parameter and that one will be equal to 5037 04:05:01,040 --> 04:05:08,319 our type animal and here let's go with 5038 04:05:04,439 --> 04:05:10,760 animal and which property is unique well 5039 04:05:08,319 --> 04:05:14,920 that will be the type one correct so we 5040 04:05:10,760 --> 04:05:19,359 can just say well if animal type is 5041 04:05:14,920 --> 04:05:21,960 equal to in our case dog well what do we 5042 04:05:19,359 --> 04:05:25,560 want to do we're going to go with animal 5043 04:05:21,960 --> 04:05:27,560 and bark because we know that it's a dog 5044 04:05:25,560 --> 04:05:30,600 now if it's not the case we already know 5045 04:05:27,560 --> 04:05:34,120 that we have animal and our other option 5046 04:05:30,600 --> 04:05:36,319 essentially is to meow so if we go over 5047 04:05:34,120 --> 04:05:38,520 here and just invoke the 5048 04:05:36,319 --> 04:05:40,720 function depending on what we're going 5049 04:05:38,520 --> 04:05:42,840 to pass in that's what we're going to 5050 04:05:40,720 --> 04:05:44,800 get in the console now at the moment 5051 04:05:42,840 --> 04:05:46,920 again there's no function a over here 5052 04:05:44,800 --> 04:05:50,720 hopefully it's clear that effectively 5053 04:05:46,920 --> 04:05:54,239 we're just narrowing down the types so 5054 04:05:50,720 --> 04:05:55,960 this is one option we have also we can 5055 04:05:54,239 --> 04:05:58,159 check for 5056 04:05:55,960 --> 04:06:00,840 property and how does that look like 5057 04:05:58,159 --> 04:06:05,199 well we can go here with function then 5058 04:06:00,840 --> 04:06:09,600 make sound same deal animal is equal to 5059 04:06:05,199 --> 04:06:13,000 our animal type and then inside of the 5060 04:06:09,600 --> 04:06:15,920 function we want to set up if and now 5061 04:06:13,000 --> 04:06:20,279 we're looking for a specific 5062 04:06:15,920 --> 04:06:22,120 property in the type now in our case 5063 04:06:20,279 --> 04:06:24,520 which one is the specific one well you 5064 04:06:22,120 --> 04:06:27,600 can actually check for method you can 5065 04:06:24,520 --> 04:06:30,359 take a look whether bark exists if it is 5066 04:06:27,600 --> 04:06:32,640 an animal then we know that it's a dog 5067 04:06:30,359 --> 04:06:35,600 if not then in our case we know that 5068 04:06:32,640 --> 04:06:40,319 it's a cat so we're going to go with if 5069 04:06:35,600 --> 04:06:44,319 and then bark in so this is a keyword we 5070 04:06:40,319 --> 04:06:48,239 want to use in the animal well if it's 5071 04:06:44,319 --> 04:06:50,880 the case let's go with animal bark and 5072 04:06:48,239 --> 04:06:54,000 if not then we know that it's a cat so 5073 04:06:50,880 --> 04:06:56,560 we can run the other method let's go 5074 04:06:54,000 --> 04:06:59,279 with meow and let's invoke it so 5075 04:06:56,560 --> 04:07:02,000 effectively when we have such setup 5076 04:06:59,279 --> 04:07:06,920 these are the options we have either we 5077 04:07:02,000 --> 04:07:09,560 can check for property or a method or if 5078 04:07:06,920 --> 04:07:12,279 we have a property which has that unique 5079 04:07:09,560 --> 04:07:15,520 value for example in this case it's a 5080 04:07:12,279 --> 04:07:18,159 typon well then we can also use the 5081 04:07:15,520 --> 04:07:22,399 equality checks all right and up next we 5082 04:07:18,159 --> 04:07:25,720 have truthy or falsy guard and it's as 5083 04:07:22,399 --> 04:07:27,920 straightforward as the name suggests 5084 04:07:25,720 --> 04:07:31,080 essentially in here you just want to 5085 04:07:27,920 --> 04:07:33,279 check for truthy or falsy value in the 5086 04:07:31,080 --> 04:07:35,640 condition and depending on the result 5087 04:07:33,279 --> 04:07:37,560 just set up the functionality and as far 5088 04:07:35,640 --> 04:07:39,040 as the challenge we want to create a 5089 04:07:37,560 --> 04:07:42,000 function in my case I'm going to call 5090 04:07:39,040 --> 04:07:44,000 this print length we're going to be 5091 04:07:42,000 --> 04:07:48,399 looking for a 5092 04:07:44,000 --> 04:07:50,279 string parameter as far as the type 5093 04:07:48,399 --> 04:07:53,880 we're going to use the union type so 5094 04:07:50,279 --> 04:07:57,840 either it's going to be string null or 5095 04:07:53,880 --> 04:08:01,399 undefined and then if it's truthy if 5096 04:07:57,840 --> 04:08:04,159 it's a string then we just want to log 5097 04:08:01,399 --> 04:08:07,119 string. length if not then we'll just 5098 04:08:04,159 --> 04:08:10,040 say that no string was provided and then 5099 04:08:07,119 --> 04:08:12,199 we'll just test with few values so here 5100 04:08:10,040 --> 04:08:15,080 in the tutorial we want to go with 5101 04:08:12,199 --> 04:08:17,640 function print 5102 04:08:15,080 --> 04:08:21,119 length let's look for string I'm going 5103 04:08:17,640 --> 04:08:24,040 to go with SDR and then as far as the 5104 04:08:21,119 --> 04:08:27,199 type either it's going to be a string 5105 04:08:24,040 --> 04:08:30,040 either it will be null or we're going to 5106 04:08:27,199 --> 04:08:31,760 go here with undefined and if we want to 5107 04:08:30,040 --> 04:08:34,439 check for a valid string we simply want 5108 04:08:31,760 --> 04:08:37,600 to set up the if block and then pass in 5109 04:08:34,439 --> 04:08:40,960 the SDR so if it's truthy if there's 5110 04:08:37,600 --> 04:08:43,760 value in a string then we'll end up in 5111 04:08:40,960 --> 04:08:46,399 this if block and as far as the logic 5112 04:08:43,760 --> 04:08:48,279 we're going to go with s strr and then 5113 04:08:46,399 --> 04:08:50,960 length so at this point we know that 5114 04:08:48,279 --> 04:08:54,479 it's a string now if it's not the case 5115 04:08:50,960 --> 04:08:57,399 I'm just going to go with log and let's 5116 04:08:54,479 --> 04:08:59,479 just go with something generic no string 5117 04:08:57,399 --> 04:09:02,439 provided even though as I'm looking at 5118 04:08:59,479 --> 04:09:05,080 it I mean we're also going to end up 5119 04:09:02,439 --> 04:09:08,199 here if there's no value in the string 5120 04:09:05,080 --> 04:09:10,080 so technically it's not 100% correct but 5121 04:09:08,199 --> 04:09:13,000 let's just go with this one for now 5122 04:09:10,080 --> 04:09:15,479 again our main purpose was to set up the 5123 04:09:13,000 --> 04:09:17,479 function it inside of the function so 5124 04:09:15,479 --> 04:09:19,520 let's go with print length let's go with 5125 04:09:17,479 --> 04:09:22,319 hello over here then let's pass in the 5126 04:09:19,520 --> 04:09:26,239 empty string then we're going to go here 5127 04:09:22,319 --> 04:09:29,399 with null and after that let's pass in 5128 04:09:26,239 --> 04:09:31,119 pretty much nothing let's at least try 5129 04:09:29,399 --> 04:09:33,520 to do that now we're not going to be 5130 04:09:31,119 --> 04:09:35,640 able to since typescript is going to 5131 04:09:33,520 --> 04:09:37,960 complain but let's just pass it in just 5132 04:09:35,640 --> 04:09:41,479 for our reference so let's say if we 5133 04:09:37,960 --> 04:09:43,439 don't provide anything and also at the 5134 04:09:41,479 --> 04:09:46,080 very end let's go with print length and 5135 04:09:43,439 --> 04:09:48,920 and set up the undefined now let's 5136 04:09:46,080 --> 04:09:52,479 navigate to console and essentially 5137 04:09:48,920 --> 04:09:54,640 notice if we provide a valid string yep 5138 04:09:52,479 --> 04:09:58,359 the result is four so we get back this 5139 04:09:54,640 --> 04:10:01,239 length and if not then we just have a 5140 04:09:58,359 --> 04:10:04,720 list of no string provided again not 5141 04:10:01,239 --> 04:10:07,080 100% correct technically in here it is a 5142 04:10:04,720 --> 04:10:10,279 string but it's an empty one but again 5143 04:10:07,080 --> 04:10:13,600 these are just Minor Details the main 5144 04:10:10,279 --> 04:10:18,199 goal of this video was to showcase the 5145 04:10:13,600 --> 04:10:20,119 truthy or falsy type guard in typescript 5146 04:10:18,199 --> 04:10:23,640 all right and up next we have instance 5147 04:10:20,119 --> 04:10:26,640 off typu which is a way in typescript to 5148 04:10:23,640 --> 04:10:30,600 check the specific class or Constructor 5149 04:10:26,640 --> 04:10:32,040 function off an object at run time and 5150 04:10:30,600 --> 04:10:34,600 this is something we actually covered 5151 04:10:32,040 --> 04:10:38,359 very very recently when we talked about 5152 04:10:34,600 --> 04:10:42,520 type unknown so therefore in this case 5153 04:10:38,359 --> 04:10:46,199 again we'll have two examples we will go 5154 04:10:42,520 --> 04:10:48,199 back to trying and catch so we'll 5155 04:10:46,199 --> 04:10:51,760 reiterate pretty much everything we 5156 04:10:48,199 --> 04:10:55,199 learned in the type unknown video and 5157 04:10:51,760 --> 04:10:59,800 also we'll work on the another example 5158 04:10:55,199 --> 04:11:02,040 where we'll check for the date so first 5159 04:10:59,800 --> 04:11:06,880 let's just reiterate that if we have try 5160 04:11:02,040 --> 04:11:09,359 and catch this error is unknown so even 5161 04:11:06,880 --> 04:11:12,560 though we have over here for sure throw 5162 04:11:09,359 --> 04:11:14,560 new error and if I pass in the message 5163 04:11:12,560 --> 04:11:16,479 typescript will complain if we'll just 5164 04:11:14,560 --> 04:11:20,920 go with error. 5165 04:11:16,479 --> 04:11:25,359 message because there's also 5166 04:11:20,920 --> 04:11:26,439 a possibility for different type of 5167 04:11:25,359 --> 04:11:28,640 error 5168 04:11:26,439 --> 04:11:32,760 essentially we might have an error which 5169 04:11:28,640 --> 04:11:34,840 is not the instance of the error class 5170 04:11:32,760 --> 04:11:37,359 so therefore if we do want to access the 5171 04:11:34,840 --> 04:11:40,319 message first we want to go with error 5172 04:11:37,359 --> 04:11:43,840 and then instance off and we're looking 5173 04:11:40,319 --> 04:11:46,199 for our error class so if it's the 5174 04:11:43,840 --> 04:11:49,319 instance yes then we know that there is 5175 04:11:46,199 --> 04:11:52,080 that message if not well then we simply 5176 04:11:49,319 --> 04:11:54,760 want to log something some kind of 5177 04:11:52,080 --> 04:12:00,800 hardcoded message so let's go here with 5178 04:11:54,760 --> 04:12:00,800 log and then let's provide some kind of 5179 04:12:00,880 --> 04:12:09,199 string and we're looking for error 5180 04:12:05,680 --> 04:12:11,840 object and now we just want to render I 5181 04:12:09,199 --> 04:12:15,199 guess the error message so error that 5182 04:12:11,840 --> 04:12:17,119 message so now we can access it if not 5183 04:12:15,199 --> 04:12:21,439 well then let's simply log something 5184 04:12:17,119 --> 04:12:26,680 else we'll just go with unknown 5185 04:12:21,439 --> 04:12:28,359 error error and do dot dot again if we 5186 04:12:26,680 --> 04:12:31,439 navigate to the browser if everything is 5187 04:12:28,359 --> 04:12:34,880 correct we should have this one over 5188 04:12:31,439 --> 04:12:38,680 here why well because at the moment in 5189 04:12:34,880 --> 04:12:40,680 Tri block I have try new error now if 5190 04:12:38,680 --> 04:12:45,800 I'm going to comment this one out or 5191 04:12:40,680 --> 04:12:48,760 simply type above and say throw and some 5192 04:12:45,800 --> 04:12:51,600 error now I should have unknown error in 5193 04:12:48,760 --> 04:12:55,279 the console so again this is something 5194 04:12:51,600 --> 04:12:59,199 we covered pretty much few videos ago 5195 04:12:55,279 --> 04:13:02,880 now also we can use such setup to check 5196 04:12:59,199 --> 04:13:05,279 for date so we can check whether the 5197 04:13:02,880 --> 04:13:09,439 input we're passing in is a string or 5198 04:13:05,279 --> 04:13:12,199 it's actually the instance of the date 5199 04:13:09,439 --> 04:13:14,640 object so let's set up our function 5200 04:13:12,199 --> 04:13:16,880 we're going to go with check 5201 04:13:14,640 --> 04:13:19,439 input and in 5202 04:13:16,880 --> 04:13:22,439 here the variable name is going to be 5203 04:13:19,439 --> 04:13:27,119 input basically the parameter name and 5204 04:13:22,439 --> 04:13:29,000 then either it's going to be date or 5205 04:13:27,119 --> 04:13:32,319 it's going to be a string now from this 5206 04:13:29,000 --> 04:13:34,920 function we want to return a string and 5207 04:13:32,319 --> 04:13:38,080 now we want to check whether the input 5208 04:13:34,920 --> 04:13:41,080 is the instance of the data object if 5209 04:13:38,080 --> 04:13:44,040 it's the case then we want to turn it 5210 04:13:41,080 --> 04:13:46,000 into a string now if it's not the case 5211 04:13:44,040 --> 04:13:49,800 if it's simply a string then we want to 5212 04:13:46,000 --> 04:13:55,000 return the input so let's go with input 5213 04:13:49,800 --> 04:13:58,239 instance off then data object here and 5214 04:13:55,000 --> 04:14:01,080 then let's use input since this is a 5215 04:13:58,239 --> 04:14:03,560 date right now we have access to all of 5216 04:14:01,080 --> 04:14:06,080 these methods we're looking for get full 5217 04:14:03,560 --> 04:14:08,880 year let's invoke this one and we want 5218 04:14:06,080 --> 04:14:10,279 to turn it into a string so notice if I 5219 04:14:08,880 --> 04:14:12,560 just return this one typescript 5220 04:14:10,279 --> 04:14:13,560 immediately complains because this will 5221 04:14:12,560 --> 04:14:16,239 be a 5222 04:14:13,560 --> 04:14:19,439 but we said that we want to return a 5223 04:14:16,239 --> 04:14:22,279 string so let's invoke this two string 5224 04:14:19,439 --> 04:14:23,800 so now typescript is not complaining and 5225 04:14:22,279 --> 04:14:25,520 if it's not the case if it's not the 5226 04:14:23,800 --> 04:14:28,920 instance of the date well then I don't 5227 04:14:25,520 --> 04:14:31,880 need to do these acrobatics I can simply 5228 04:14:28,920 --> 04:14:34,399 return the input so now let's construct 5229 04:14:31,880 --> 04:14:37,319 two variables we're going to go check 5230 04:14:34,399 --> 04:14:40,680 input in here let's just pass in new 5231 04:14:37,319 --> 04:14:43,880 date and also we're going to go with 5232 04:14:40,680 --> 04:14:46,560 some random values as well random 5233 04:14:43,880 --> 04:14:48,279 and we'll set it equal to check input 5234 04:14:46,560 --> 04:14:50,119 and let's just pass in some kind of 5235 04:14:48,279 --> 04:14:52,399 string so 5236 04:14:50,119 --> 04:14:57,119 2020 the best year 5237 04:14:52,399 --> 04:15:00,319 ever of course in the air quotes and 5238 04:14:57,119 --> 04:15:02,880 let's pass in some random date then 5239 04:15:00,319 --> 04:15:05,600 let's log both of these values we're 5240 04:15:02,880 --> 04:15:08,359 going to go over here with year and 5241 04:15:05,600 --> 04:15:10,119 random I'm not even sure whether we are 5242 04:15:08,359 --> 04:15:12,680 really interested in the console but 5243 04:15:10,119 --> 04:15:15,680 let's just log it and notice essentially 5244 04:15:12,680 --> 04:15:19,680 both of them are strings and again the 5245 04:15:15,680 --> 04:15:22,439 instance of typ guard is a Nifty way for 5246 04:15:19,680 --> 04:15:27,199 us to check in typescript whether the 5247 04:15:22,439 --> 04:15:30,439 object is an instance of a class or 5248 04:15:27,199 --> 04:15:33,040 created by the Constructor function if 5249 04:15:30,439 --> 04:15:35,960 it's the case it's going to return true 5250 04:15:33,040 --> 04:15:38,159 if not it's going to return 5251 04:15:35,960 --> 04:15:40,920 false all right and up next let's 5252 04:15:38,159 --> 04:15:43,720 discuss type predicate function which is 5253 04:15:40,920 --> 04:15:47,800 a special kind of function 5254 04:15:43,720 --> 04:15:51,479 that not only returns true or false but 5255 04:15:47,800 --> 04:15:54,199 also we can narrow down the type and 5256 04:15:51,479 --> 04:15:57,760 again this should be a refresher because 5257 04:15:54,199 --> 04:16:00,399 we did discuss type predicate previously 5258 04:15:57,760 --> 04:16:03,840 as far as the star code let's just grab 5259 04:16:00,399 --> 04:16:07,680 this one and then we'll go over the 5260 04:16:03,840 --> 04:16:10,399 challenge so here in tutorial I have two 5261 04:16:07,680 --> 04:16:13,279 types I have the student one as well as 5262 04:16:10,399 --> 04:16:16,119 the user notice this property is the 5263 04:16:13,279 --> 04:16:18,600 same it's a string and the only 5264 04:16:16,119 --> 04:16:20,920 difference well they have different 5265 04:16:18,600 --> 04:16:24,279 methods so one is study and the other 5266 04:16:20,920 --> 04:16:26,880 one is login then I use the type Union 5267 04:16:24,279 --> 04:16:29,600 to create type person which can be 5268 04:16:26,880 --> 04:16:32,920 either the student or the user and then 5269 04:16:29,600 --> 04:16:34,880 again I go with my math. random if it's 5270 04:16:32,920 --> 04:16:39,040 bigger than 5271 04:16:34,880 --> 04:16:41,600 0.5 then I'll return one if not well 5272 04:16:39,040 --> 04:16:44,720 then I'll return another one so either 5273 04:16:41,600 --> 04:16:46,399 this is going to be a student or a user 5274 04:16:44,720 --> 04:16:49,279 and effectively what we want to do is 5275 04:16:46,399 --> 04:16:54,439 set up our type predicate function where 5276 04:16:49,279 --> 04:16:56,199 not only we check for specific type but 5277 04:16:54,439 --> 04:16:59,800 we also want to help typescript by 5278 04:16:56,199 --> 04:17:02,000 narrowing down the type uh in my case 5279 04:16:59,800 --> 04:17:05,279 I'm going to show you two options how we 5280 04:17:02,000 --> 04:17:07,960 can check for specific type but feel 5281 04:17:05,279 --> 04:17:10,040 free to use whichever makes the most 5282 04:17:07,960 --> 04:17:13,399 sense to you and then after that we want 5283 04:17:10,040 --> 04:17:15,399 to use if else essentially run our type 5284 04:17:13,399 --> 04:17:17,640 predicate and if it's true we want to 5285 04:17:15,399 --> 04:17:19,040 invoke one of the methods so let's say 5286 04:17:17,640 --> 04:17:22,399 in my case I'm going to be checking for 5287 04:17:19,040 --> 04:17:25,680 student if not then I'm going to invoke 5288 04:17:22,399 --> 04:17:27,080 login which is for the user so right 5289 04:17:25,680 --> 04:17:30,520 after the person I'm going to go with 5290 04:17:27,080 --> 04:17:31,800 function is student at least in my case 5291 04:17:30,520 --> 04:17:34,199 I'm going to be checking for student 5292 04:17:31,800 --> 04:17:37,319 again this is totally up to you you can 5293 04:17:34,199 --> 04:17:41,319 check for user as well now I'm going to 5294 04:17:37,319 --> 04:17:43,760 be expecting a person which again can be 5295 04:17:41,319 --> 04:17:46,720 either of them for now I'm not going to 5296 04:17:43,760 --> 04:17:50,479 set up the return type and as far as our 5297 04:17:46,720 --> 04:17:52,680 options well we can use the in operator 5298 04:17:50,479 --> 04:17:55,439 something we talked about literally few 5299 04:17:52,680 --> 04:17:58,119 videos ago where again we're looking for 5300 04:17:55,439 --> 04:18:00,640 something unique in this case either 5301 04:17:58,119 --> 04:18:04,840 it's going to be study or a login and we 5302 04:18:00,640 --> 04:18:06,920 just go with the method name then in and 5303 04:18:04,840 --> 04:18:11,600 then the person since that's our 5304 04:18:06,920 --> 04:18:15,359 parameter or we can also use the type 5305 04:18:11,600 --> 04:18:18,439 assertion which where we assert that 5306 04:18:15,359 --> 04:18:21,840 person is going to be a student then we 5307 04:18:18,439 --> 04:18:24,600 invoke the study method and then we 5308 04:18:21,840 --> 04:18:27,359 check whether it's defined or not so 5309 04:18:24,600 --> 04:18:29,560 again this part is totally up to you for 5310 04:18:27,359 --> 04:18:31,359 now again we don't have any returns I 5311 04:18:29,560 --> 04:18:34,880 just want to showcase that if we go here 5312 04:18:31,359 --> 04:18:37,880 with return and then study again our 5313 04:18:34,880 --> 04:18:40,080 method in person this will return either 5314 04:18:37,880 --> 04:18:45,040 true or false so 5315 04:18:40,080 --> 04:18:48,199 either this method is in the type or not 5316 04:18:45,040 --> 04:18:50,159 now if it's the case then we want to go 5317 04:18:48,199 --> 04:18:53,680 with person 5318 04:18:50,159 --> 04:18:56,040 is the student correct because at that 5319 04:18:53,680 --> 04:18:57,560 point that script knows for 5320 04:18:56,040 --> 04:19:02,080 sure 5321 04:18:57,560 --> 04:19:03,720 that the instance is the type of student 5322 04:19:02,080 --> 04:19:06,880 and if that's the case then we can use 5323 04:19:03,720 --> 04:19:09,279 the study method now alternatively we 5324 04:19:06,880 --> 04:19:13,279 can use this approach where we go to 5325 04:19:09,279 --> 04:19:14,359 return Then person and we assert that 5326 04:19:13,279 --> 04:19:18,680 it's a 5327 04:19:14,359 --> 04:19:22,720 student now what is the method student 5328 04:19:18,680 --> 04:19:24,159 has it has the steud one but again just 5329 04:19:22,720 --> 04:19:26,239 because we are asserting doesn't 5330 04:19:24,159 --> 04:19:28,279 magically make this person to be a 5331 04:19:26,239 --> 04:19:31,880 student and therefore in here we still 5332 04:19:28,279 --> 04:19:34,239 check we say okay we assert that person 5333 04:19:31,880 --> 04:19:37,279 is a student we know that there is a 5334 04:19:34,239 --> 04:19:41,359 study property but it also can be 5335 04:19:37,279 --> 04:19:43,239 undefined so it's only a student if it's 5336 04:19:41,359 --> 04:19:46,760 not undefined 5337 04:19:43,239 --> 04:19:48,720 like so so those are the two options you 5338 04:19:46,760 --> 04:19:51,239 can use over here and now let's set up 5339 04:19:48,720 --> 04:19:54,720 the if blog let's say well if it's a 5340 04:19:51,239 --> 04:19:57,159 student then let's pass in the person 5341 04:19:54,720 --> 04:20:00,560 what do we do at next well then we can 5342 04:19:57,159 --> 04:20:03,279 use essentially our method correct and 5343 04:20:00,560 --> 04:20:05,840 if it's not what options we have well 5344 04:20:03,279 --> 04:20:08,800 then it's a user correct so we go here 5345 04:20:05,840 --> 04:20:12,720 with person and then log in and also 5346 04:20:08,800 --> 04:20:15,760 invoke it again a very fancy name type 5347 04:20:12,720 --> 04:20:18,840 iate effectively just remember the is 5348 04:20:15,760 --> 04:20:21,040 keyword and remember the fact that not 5349 04:20:18,840 --> 04:20:24,920 only we're trying 5350 04:20:21,040 --> 04:20:29,000 to check for a specific type but we're 5351 04:20:24,920 --> 04:20:31,760 also helping typescript to narrow down 5352 04:20:29,000 --> 04:20:34,239 that type okay and before we move on to 5353 04:20:31,760 --> 04:20:36,159 the next topic I also quickly want to 5354 04:20:34,239 --> 04:20:40,560 show you one 5355 04:20:36,159 --> 04:20:42,199 gotcha and this involves type never now 5356 04:20:40,560 --> 04:20:43,880 technically this video is optional if 5357 04:20:42,199 --> 04:20:46,720 you're not interested you can just move 5358 04:20:43,880 --> 04:20:50,319 on to the next video but I simply want 5359 04:20:46,720 --> 04:20:54,680 to showcase that if we hardcode the 5360 04:20:50,319 --> 04:20:57,600 person to be a specific type typescript 5361 04:20:54,680 --> 04:20:59,800 is going to yell at us so in here yes we 5362 04:20:57,600 --> 04:21:02,920 are setting up that person randomly 5363 04:20:59,800 --> 04:21:05,960 correct so we have our random person and 5364 04:21:02,920 --> 04:21:10,960 pretty much depending on the value well 5365 04:21:05,960 --> 04:21:14,000 we either return a student or a user now 5366 04:21:10,960 --> 04:21:17,119 if I set up my person differently let's 5367 04:21:14,000 --> 04:21:20,119 say if I comment this one out again I'm 5368 04:21:17,119 --> 04:21:23,199 going to go with person I'll say that 5369 04:21:20,119 --> 04:21:26,760 this type is actually a person and then 5370 04:21:23,199 --> 04:21:31,119 I'll hard code so I'll say name is equal 5371 04:21:26,760 --> 04:21:33,479 to Anna and then since I'm creating a 5372 04:21:31,119 --> 04:21:37,439 student I'm going to set up my study 5373 04:21:33,479 --> 04:21:39,920 method so I'll come up with my function 5374 04:21:37,439 --> 04:21:44,920 and I believe it doesn't return anything 5375 04:21:39,920 --> 04:21:48,640 so I'll just go with log and study dot 5376 04:21:44,920 --> 04:21:52,119 do dot okay let me fix the typos and all 5377 04:21:48,640 --> 04:21:56,560 that so now I have this person but I'm 5378 04:21:52,119 --> 04:21:59,399 hardcoding this is not any more random 5379 04:21:56,560 --> 04:22:03,279 notice TPT essentially right away yells 5380 04:21:59,399 --> 04:22:07,840 at me says well there's no way this 5381 04:22:03,279 --> 04:22:11,239 person can be a user the other type why 5382 04:22:07,840 --> 04:22:15,040 well because you hardcoded to be a 5383 04:22:11,239 --> 04:22:17,159 student so yeah I can still check 5384 04:22:15,040 --> 04:22:20,000 whether the person is 5385 04:22:17,159 --> 04:22:22,680 student so type pred code still works 5386 04:22:20,000 --> 04:22:24,319 but in the lse block you'll notice that 5387 04:22:22,680 --> 04:22:26,840 person is 5388 04:22:24,319 --> 04:22:29,040 never and this brings me back to the 5389 04:22:26,840 --> 04:22:30,479 point that I was making in the type 5390 04:22:29,040 --> 04:22:32,840 never 5391 04:22:30,479 --> 04:22:36,080 video essentially if you have the 5392 04:22:32,840 --> 04:22:38,600 scenario where type is equal to never 5393 04:22:36,080 --> 04:22:40,880 you'll just have to go back to the 5394 04:22:38,600 --> 04:22:42,760 initial place where you're setting up 5395 04:22:40,880 --> 04:22:44,800 the variable and also start checking 5396 04:22:42,760 --> 04:22:47,239 your conditions because effectively in 5397 04:22:44,800 --> 04:22:51,880 here typescript just says Hey listen 5398 04:22:47,239 --> 04:22:55,520 there's no way that person can be a type 5399 04:22:51,880 --> 04:22:57,880 user because you hardcoded to be a type 5400 04:22:55,520 --> 04:23:00,040 student now what's interesting if I 5401 04:22:57,880 --> 04:23:03,880 change this around to a 5402 04:23:00,040 --> 04:23:05,760 login now the person is actually a user 5403 04:23:03,880 --> 04:23:09,159 correct so now I'm hardcoding to be a 5404 04:23:05,760 --> 04:23:12,119 user so I have my type predic cut I 5405 04:23:09,159 --> 04:23:15,399 check in my condition it's not the case 5406 04:23:12,119 --> 04:23:18,680 and now we can successfully get to else 5407 04:23:15,399 --> 04:23:21,800 but if I hardcode my person to be a 5408 04:23:18,680 --> 04:23:24,279 student then there's no way for me to 5409 04:23:21,800 --> 04:23:27,159 get to this pulse block hopefully it's 5410 04:23:24,279 --> 04:23:30,239 clear and now we can move on to the next 5411 04:23:27,159 --> 04:23:35,000 topic all right and up next let's 5412 04:23:30,239 --> 04:23:37,560 discuss discriminated unions and also 5413 04:23:35,000 --> 04:23:41,960 let's recall how we can set up the 5414 04:23:37,560 --> 04:23:43,880 exhausting check using type never now 5415 04:23:41,960 --> 04:23:46,880 again I I know that the name 5416 04:23:43,880 --> 04:23:50,439 discriminated Union most likely sounds 5417 04:23:46,880 --> 04:23:52,720 pretty scary but essentially it's just 5418 04:23:50,439 --> 04:23:56,399 going to be a 5419 04:23:52,720 --> 04:23:59,760 property which has a literal value now 5420 04:23:56,399 --> 04:24:03,000 as far as the star code this is what we 5421 04:23:59,760 --> 04:24:06,080 have over here so we have two types one 5422 04:24:03,000 --> 04:24:09,359 is going to be increment action second 5423 04:24:06,080 --> 04:24:12,040 one will be decrement action both of 5424 04:24:09,359 --> 04:24:13,960 them taking the number the amount time 5425 04:24:12,040 --> 04:24:16,359 stamp which is going to be a number 5426 04:24:13,960 --> 04:24:18,880 again this is totally random and the 5427 04:24:16,359 --> 04:24:24,040 same goes for user string this is 5428 04:24:18,880 --> 04:24:26,040 totally random and then we have a type 5429 04:24:24,040 --> 04:24:29,399 by the name of action where I use the 5430 04:24:26,040 --> 04:24:32,640 union type and I just use both of these 5431 04:24:29,399 --> 04:24:35,720 types here so either it's going to be 5432 04:24:32,640 --> 04:24:38,119 increment or decrement and then I will 5433 04:24:35,720 --> 04:24:40,520 set up a reducer function which is 5434 04:24:38,119 --> 04:24:43,319 looking for two things it's looking for 5435 04:24:40,520 --> 04:24:46,159 the state so again this is just going to 5436 04:24:43,319 --> 04:24:48,000 be some random number and also the 5437 04:24:46,159 --> 04:24:50,479 action so what do we want to do do we 5438 04:24:48,000 --> 04:24:53,680 want to increase the number or we want 5439 04:24:50,479 --> 04:24:57,000 to decrease so let's start setting up 5440 04:24:53,680 --> 04:25:00,439 and in a process you'll see why we need 5441 04:24:57,000 --> 04:25:04,520 the fancy discriminated unions which 5442 04:25:00,439 --> 04:25:09,119 again is just a property with a string 5443 04:25:04,520 --> 04:25:11,800 value so for S let's go here with new 5444 04:25:09,119 --> 04:25:13,520 state and you know what I haven't set up 5445 04:25:11,800 --> 04:25:16,119 the functions so maybe let's start with 5446 04:25:13,520 --> 04:25:20,159 this one so we have a 5447 04:25:16,119 --> 04:25:22,840 function looking for state which will be 5448 04:25:20,159 --> 04:25:25,439 a number so some kind of hardcoded 5449 04:25:22,840 --> 04:25:28,439 number and then also the action so 5450 04:25:25,439 --> 04:25:31,319 whether we want to increase or decrease 5451 04:25:28,439 --> 04:25:34,199 and by how much this is the amount over 5452 04:25:31,319 --> 04:25:36,680 here so if we want to increase then byy 5453 04:25:34,199 --> 04:25:39,239 this amount and the same goes for 5454 04:25:36,680 --> 04:25:41,040 decrease now I'm not going to set up 5455 04:25:39,239 --> 04:25:42,960 again anything as far as the 5456 04:25:41,040 --> 04:25:45,080 functionality I simply we want to go 5457 04:25:42,960 --> 04:25:48,119 with 5458 04:25:45,080 --> 04:25:49,920 reducer that's the function and of 5459 04:25:48,119 --> 04:25:52,880 course I forgot to add the name over 5460 04:25:49,920 --> 04:25:55,479 here my bad so we're looking for 5461 04:25:52,880 --> 04:25:56,880 reducer then as far as the initial value 5462 04:25:55,479 --> 04:25:59,439 again we're just hardcoding something 5463 04:25:56,880 --> 04:26:01,239 random so I'm going to go here with 15 5464 04:25:59,439 --> 04:26:03,800 and then when it comes to action well 5465 04:26:01,239 --> 04:26:06,520 now I'm going to pass in the object so 5466 04:26:03,800 --> 04:26:08,800 either it's going to be increment or 5467 04:26:06,520 --> 04:26:12,399 decrement and in this case I'm going to 5468 04:26:08,800 --> 04:26:15,359 hard code that user value will be John 5469 04:26:12,399 --> 04:26:18,880 we don't have the type yet so let's just 5470 04:26:15,359 --> 04:26:24,080 go here with amount of five and then 5471 04:26:18,880 --> 04:26:27,640 time stamp well let's set up 1 2 3 4 5 5472 04:26:24,080 --> 04:26:30,920 six or something along those lines and 5473 04:26:27,640 --> 04:26:33,800 now let's discuss why do we need the 5474 04:26:30,920 --> 04:26:36,800 discriminated unions in the first place 5475 04:26:33,800 --> 04:26:39,359 well we have two types 5476 04:26:36,800 --> 04:26:42,600 and how are we going to check for 5477 04:26:39,359 --> 04:26:46,760 specific type I mean we canot use type 5478 04:26:42,600 --> 04:26:49,600 off we cannot use in we cannot use I 5479 04:26:46,760 --> 04:26:52,000 mean instance off or any of the things 5480 04:26:49,600 --> 04:26:54,640 that we covered previously why well 5481 04:26:52,000 --> 04:26:56,760 because pretty much they're the same 5482 04:26:54,640 --> 04:26:59,880 correct now they do have different 5483 04:26:56,760 --> 04:27:01,800 meaning one is going to increase this 5484 04:26:59,880 --> 04:27:04,080 state over here and the other one is 5485 04:27:01,800 --> 04:27:06,479 going to decrease but in the reducer how 5486 04:27:04,080 --> 04:27:10,359 I'm going to check for that action this 5487 04:27:06,479 --> 04:27:13,159 is where the discriminated unions come 5488 04:27:10,359 --> 04:27:18,040 into play where 5489 04:27:13,159 --> 04:27:19,439 we'll have this unique literal value how 5490 04:27:18,040 --> 04:27:22,199 is that going to look like we'll 5491 04:27:19,439 --> 04:27:24,880 navigate back to increment action and 5492 04:27:22,199 --> 04:27:26,960 yes effectively you'll just add here 5493 04:27:24,880 --> 04:27:29,199 whatever property name you want in my 5494 04:27:26,960 --> 04:27:32,520 case it's going to be a type and it's 5495 04:27:29,199 --> 04:27:35,520 going to be equal to increment and now 5496 04:27:32,520 --> 04:27:38,479 we want to do the same thing for 5497 04:27:35,520 --> 04:27:43,080 decrement so instead of increment we 5498 04:27:38,479 --> 04:27:46,359 want to go with decrement so now in the 5499 04:27:43,080 --> 04:27:49,520 reducer we want to set up 5500 04:27:46,359 --> 04:27:53,720 a switch statement where we're going to 5501 04:27:49,520 --> 04:27:56,119 check for the type if it's increment 5502 04:27:53,720 --> 04:27:59,119 then we'll increase if decrement then we 5503 04:27:56,119 --> 04:28:02,080 decrease and also let's just recap how 5504 04:27:59,119 --> 04:28:04,880 we can set up our default case with the 5505 04:28:02,080 --> 04:28:06,720 help of type never now typescript is 5506 04:28:04,880 --> 04:28:10,479 complaining because I'm not providing a 5507 04:28:06,720 --> 04:28:12,040 typ over here so we do need to go with 5508 04:28:10,479 --> 04:28:13,199 one of them in this case I'm just going 5509 04:28:12,040 --> 04:28:15,399 to going to go with the increment again 5510 04:28:13,199 --> 04:28:18,600 it doesn't really matter 5511 04:28:15,399 --> 04:28:21,080 since our main focus is on this logic 5512 04:28:18,600 --> 04:28:24,479 here in the reducer so first let's set 5513 04:28:21,080 --> 04:28:27,040 up the switch one and we're going to be 5514 04:28:24,479 --> 04:28:29,560 looking for Action type that's the 5515 04:28:27,040 --> 04:28:33,239 property we're interested because it's 5516 04:28:29,560 --> 04:28:35,080 Unique to each type over here and again 5517 04:28:33,239 --> 04:28:39,239 if we have 10 types yeah we'll pretty 5518 04:28:35,080 --> 04:28:42,239 much have to set up the type for all of 5519 04:28:39,239 --> 04:28:44,920 them and provide that unique value then 5520 04:28:42,239 --> 04:28:47,520 we want to go with case and this case 5521 04:28:44,920 --> 04:28:49,080 I'm going to go with increment and as 5522 04:28:47,520 --> 04:28:51,960 far as what do I want to return from the 5523 04:28:49,080 --> 04:28:53,479 function well let's go with number so 5524 04:28:51,960 --> 04:28:56,119 we're going to go with that initial 5525 04:28:53,479 --> 04:28:58,800 value basically a state and we'll add 5526 04:28:56,119 --> 04:29:01,600 action Dot and the amount that I'm 5527 04:28:58,800 --> 04:29:07,000 passing in now we want to do the same 5528 04:29:01,600 --> 04:29:09,680 thing for the decrement correct so let's 5529 04:29:07,000 --> 04:29:13,520 just change it around and again let's 5530 04:29:09,680 --> 04:29:17,479 just recall the issue with the setup if 5531 04:29:13,520 --> 04:29:21,800 we add some other type to this action 5532 04:29:17,479 --> 04:29:23,000 well it's going to fail silently correct 5533 04:29:21,800 --> 04:29:27,119 and therefore we want to set up the 5534 04:29:23,000 --> 04:29:30,479 default case and we want to assign the 5535 04:29:27,119 --> 04:29:34,319 action to some variable which is type 5536 04:29:30,479 --> 04:29:38,640 never because in that case if there's 5537 04:29:34,319 --> 04:29:42,640 some other type over here it will 5538 04:29:38,640 --> 04:29:45,760 immediately be assigned to that variable 5539 04:29:42,640 --> 04:29:48,880 and we cannot assign to type never so 5540 04:29:45,760 --> 04:29:51,159 let's just go over here say 5541 04:29:48,880 --> 04:29:54,399 default that's going to be our default 5542 04:29:51,159 --> 04:29:58,399 case let's also throw I guess quite 5543 04:29:54,399 --> 04:30:00,880 typical scenario so throw new error and 5544 04:29:58,399 --> 04:30:04,319 then I'm going to set up the template 5545 04:30:00,880 --> 04:30:05,159 string and in here essentially let's set 5546 04:30:04,319 --> 04:30:08,199 up 5547 04:30:05,159 --> 04:30:13,600 unexpected for some reason this is the 5548 04:30:08,199 --> 04:30:17,159 biggest challenge as far as the spelling 5549 04:30:13,600 --> 04:30:19,680 unexpected action over here type will be 5550 04:30:17,159 --> 04:30:23,960 equal to never and we'll assign it to an 5551 04:30:19,680 --> 04:30:26,479 action so if actually action is never if 5552 04:30:23,960 --> 04:30:29,640 we have handled all the possible cases 5553 04:30:26,479 --> 04:30:32,479 we're good to go but if there's going to 5554 04:30:29,640 --> 04:30:34,720 be some additional case and we're not 5555 04:30:32,479 --> 04:30:37,560 handling right away typescript is going 5556 04:30:34,720 --> 04:30:40,760 to yell and in here let's just type 5557 04:30:37,560 --> 04:30:44,040 unexpected action and then let's access 5558 04:30:40,760 --> 04:30:47,080 the unexpected action again the point 5559 04:30:44,040 --> 04:30:50,119 really here is not to run the code I 5560 04:30:47,080 --> 04:30:52,760 mean we're mostly focusing just on the 5561 04:30:50,119 --> 04:30:56,159 logic over here in the redu sh so this 5562 04:30:52,760 --> 04:31:00,319 is how we can set up the discriminated 5563 04:30:56,159 --> 04:31:04,040 unions and exhaustive check using the 5564 04:31:00,319 --> 04:31:06,279 never type in typescript okay and we 5565 04:31:04,040 --> 04:31:08,640 have successfully arrived at the most 5566 04:31:06,279 --> 04:31:11,199 complex and confusing Topic in 5567 04:31:08,640 --> 04:31:12,560 typescript which is none other than the 5568 04:31:11,199 --> 04:31:15,640 generics 5569 04:31:12,560 --> 04:31:20,319 in short generics allow us to write for 5570 04:31:15,640 --> 04:31:23,279 example functions with any data type and 5571 04:31:20,319 --> 04:31:26,439 you can think of generics as a variable 5572 04:31:23,279 --> 04:31:29,520 for types now I'm not going to Sugar Cod 5573 04:31:26,439 --> 04:31:31,880 it it's not an easy topic the good news 5574 04:31:29,520 --> 04:31:33,800 is though that most likely in the 5575 04:31:31,880 --> 04:31:36,800 beginning at least based on my 5576 04:31:33,800 --> 04:31:39,960 experience you'll primarily use generic 5577 04:31:36,800 --> 04:31:43,840 functions or interfaces created by 5578 04:31:39,960 --> 04:31:47,080 somebody else whether it be the creators 5579 04:31:43,840 --> 04:31:50,040 of typescript or developers of some 5580 04:31:47,080 --> 04:31:54,359 third party library and in that case 5581 04:31:50,040 --> 04:31:56,080 you'll only need to remember one concept 5582 04:31:54,359 --> 04:32:00,239 every time you see those funny looking 5583 04:31:56,080 --> 04:32:03,359 angle brackets think generics and let's 5584 04:32:00,239 --> 04:32:07,159 begin by taking a closer look at the 5585 04:32:03,359 --> 04:32:10,479 arrays in typescript so at this point in 5586 04:32:07,159 --> 04:32:14,520 tutorial we should be already familiar 5587 04:32:10,479 --> 04:32:17,439 with this syntax so we have three 5588 04:32:14,520 --> 04:32:20,119 variables the values are arrays and here 5589 04:32:17,439 --> 04:32:23,439 we just say well what is the type for 5590 04:32:20,119 --> 04:32:26,239 the array and as we're looking at this 5591 04:32:23,439 --> 04:32:29,239 code and we're just focusing on 5592 04:32:26,239 --> 04:32:32,040 typescript so please don't tell me that 5593 04:32:29,239 --> 04:32:34,680 the answer here is the keyword let as 5594 04:32:32,040 --> 04:32:37,119 you're looking at this code what is the 5595 04:32:34,680 --> 04:32:39,479 one thing that doesn't 5596 04:32:37,119 --> 04:32:41,479 change so what is the one thing that 5597 04:32:39,479 --> 04:32:44,159 stays the same and it's kind of pretty 5598 04:32:41,479 --> 04:32:47,319 obvious but I do want to start from this 5599 04:32:44,159 --> 04:32:49,760 point and the answer is following notice 5600 04:32:47,319 --> 04:32:52,119 how the type is changing here all the 5601 04:32:49,760 --> 04:32:54,000 time but we're always always getting 5602 04:32:52,119 --> 04:32:56,520 this array now of course we can make 5603 04:32:54,000 --> 04:33:00,319 this complex we can provide our own type 5604 04:32:56,520 --> 04:33:03,040 we can provide the union type whatever 5605 04:33:00,319 --> 04:33:06,840 that doesn't change the main idea we're 5606 04:33:03,040 --> 04:33:09,080 always always getting back this array 5607 04:33:06,840 --> 04:33:12,920 and not only that we can pretty much 5608 04:33:09,080 --> 04:33:15,119 pass here whatever we want as far as the 5609 04:33:12,920 --> 04:33:18,000 type correct so it still needs to make 5610 04:33:15,119 --> 04:33:20,799 sense as far as type and we'll always 5611 04:33:18,000 --> 04:33:25,119 get back the array so it's not like I 5612 04:33:20,799 --> 04:33:28,400 need to go somewhere and look for string 5613 04:33:25,119 --> 04:33:30,359 array no I don't have such type I 5614 04:33:28,400 --> 04:33:33,039 literally just have this one universal 5615 04:33:30,359 --> 04:33:36,680 one that I can use because if you think 5616 04:33:33,039 --> 04:33:40,439 about it how difficult it would be 5617 04:33:36,680 --> 04:33:43,799 that not only we need to remember but 5618 04:33:40,439 --> 04:33:46,480 also to construct such types let's say 5619 04:33:43,799 --> 04:33:49,879 we'll need to have one numbers array 5620 04:33:46,480 --> 04:33:51,520 type then one Boolean one and I can go 5621 04:33:49,879 --> 04:33:55,920 on and on and on and 5622 04:33:51,520 --> 04:33:59,080 on and actually we can examine what type 5623 04:33:55,920 --> 04:34:04,119 are we getting back over here so let me 5624 04:33:59,080 --> 04:34:07,160 remove this silly text and now let me 5625 04:34:04,119 --> 04:34:10,000 right click then we want to go to type 5626 04:34:07,160 --> 04:34:13,000 definitions and yes I know we haven't 5627 04:34:10,000 --> 04:34:14,719 covered the Declarations yet don't worry 5628 04:34:13,000 --> 04:34:16,279 all of that is coming up and you know 5629 04:34:14,719 --> 04:34:17,639 what I'll actually pick the third one 5630 04:34:16,279 --> 04:34:20,879 because it's going to be easier for me 5631 04:34:17,639 --> 04:34:23,600 to show you so let's go here to type 5632 04:34:20,879 --> 04:34:25,199 definition and then we're looking for 5633 04:34:23,600 --> 04:34:28,920 this declaration one so we're looking 5634 04:34:25,199 --> 04:34:31,160 for lib es5 D DS again we haven't 5635 04:34:28,920 --> 04:34:32,680 covered them how we have access to them 5636 04:34:31,160 --> 04:34:36,600 and all that all of that is coming up 5637 04:34:32,680 --> 04:34:38,400 for now but just focus on this code so 5638 04:34:36,600 --> 04:34:40,039 notice we have this interface again 5639 04:34:38,400 --> 04:34:43,920 something we should be already familiar 5640 04:34:40,039 --> 04:34:46,760 with remember we use it to provide shape 5641 04:34:43,920 --> 04:34:49,359 of an object the name here is an array 5642 04:34:46,760 --> 04:34:52,561 and check it out we have the funny 5643 04:34:49,359 --> 04:34:55,279 looking angle brackets and inside of it 5644 04:34:52,561 --> 04:34:57,760 we have this T now right away I want to 5645 04:34:55,279 --> 04:34:59,279 mention T is convention you can call 5646 04:34:57,760 --> 04:35:03,119 this shake and bake you will see that in 5647 04:34:59,279 --> 04:35:05,959 a second once we set up our own generic 5648 04:35:03,119 --> 04:35:08,959 interface or function or whatever 5649 04:35:05,959 --> 04:35:12,439 basically this 5650 04:35:08,959 --> 04:35:15,480 represents the notion that this type can 5651 04:35:12,439 --> 04:35:18,400 be anything so I can pass here the 5652 04:35:15,480 --> 04:35:22,879 string number Boolean my own type you 5653 04:35:18,400 --> 04:35:25,080 name it and every time not only I'm 5654 04:35:22,879 --> 04:35:28,080 going to get back the array with that 5655 04:35:25,080 --> 04:35:30,080 specific type I'll also get all of the 5656 04:35:28,080 --> 04:35:31,920 properties and methods that we know 5657 04:35:30,080 --> 04:35:35,240 already exist on array so what am I 5658 04:35:31,920 --> 04:35:37,080 talking about well we know that length 5659 04:35:35,240 --> 04:35:39,879 is part of the array correct we can just 5660 04:35:37,080 --> 04:35:42,279 call array. length and we're good to go 5661 04:35:39,879 --> 04:35:44,480 so notice we have over here this method 5662 04:35:42,279 --> 04:35:47,279 so we can definitely use it and then 5663 04:35:44,480 --> 04:35:49,840 let's keep on moving and we'll see again 5664 04:35:47,279 --> 04:35:53,879 all of the functions we already familiar 5665 04:35:49,840 --> 04:35:56,279 with but here's the kicker for example 5666 04:35:53,879 --> 04:35:59,639 pop now if you're not familiar this 5667 04:35:56,279 --> 04:36:01,639 essentially is a function which removes 5668 04:35:59,639 --> 04:36:05,199 the last element from the array and 5669 04:36:01,639 --> 04:36:07,719 returns it but now notice what is the 5670 04:36:05,199 --> 04:36:11,920 return here specified for the pop it's 5671 04:36:07,719 --> 04:36:15,199 not string number or Boolean or your own 5672 04:36:11,920 --> 04:36:18,920 no it's basically this D it's basically 5673 04:36:15,199 --> 04:36:20,799 this generic so whatever I pass in 5674 04:36:18,920 --> 04:36:23,320 whatever array I 5675 04:36:20,799 --> 04:36:25,039 construct well I want to remove that 5676 04:36:23,320 --> 04:36:28,000 last type 5677 04:36:25,039 --> 04:36:30,840 correct I'm not saying hey this is the 5678 04:36:28,000 --> 04:36:34,600 array of strings and now I need to call 5679 04:36:30,840 --> 04:36:38,039 specific method to remove the string no 5680 04:36:34,600 --> 04:36:40,639 basically whatever type we use to set up 5681 04:36:38,039 --> 04:36:42,359 that array well that's the same type we 5682 04:36:40,639 --> 04:36:44,760 remove and 5683 04:36:42,359 --> 04:36:47,879 notice how essentially 5684 04:36:44,760 --> 04:36:50,561 we're saving bunch of the code because 5685 04:36:47,879 --> 04:36:54,000 imagine if you would have to set up such 5686 04:36:50,561 --> 04:36:56,480 interface pretty much for any type that 5687 04:36:54,000 --> 04:36:59,359 we can pass in the array I mean you 5688 04:36:56,480 --> 04:37:01,240 would go most likely insane so that's 5689 04:36:59,359 --> 04:37:05,080 why we use this variable where we say 5690 04:37:01,240 --> 04:37:07,240 okay you can pass in any type and it's 5691 04:37:05,080 --> 04:37:09,799 going to get treated the same way we'll 5692 04:37:07,240 --> 04:37:11,840 create the array the same way we can you 5693 04:37:09,799 --> 04:37:15,520 call the methods the same way so so none 5694 04:37:11,840 --> 04:37:17,760 of that changes so whatever type we pass 5695 04:37:15,520 --> 04:37:20,240 in well that's the type we're going to 5696 04:37:17,760 --> 04:37:24,160 use to operate so hopefully again this 5697 04:37:20,240 --> 04:37:27,480 is clear that it definitely makes sense 5698 04:37:24,160 --> 04:37:29,520 in a lot of instances to set up the 5699 04:37:27,480 --> 04:37:32,160 generic one because you don't want to 5700 04:37:29,520 --> 04:37:35,719 set up essentially the interface for 5701 04:37:32,160 --> 04:37:38,719 every possible type that you can pass 5702 04:37:35,719 --> 04:37:41,080 intoa and now knowing this we can 5703 04:37:38,719 --> 04:37:44,000 actually take a look at the second 5704 04:37:41,080 --> 04:37:45,680 approach we have to construct the arrays 5705 04:37:44,000 --> 04:37:47,279 now I can tell you right away that just 5706 04:37:45,680 --> 04:37:50,400 because we know the generics you won't 5707 04:37:47,279 --> 04:37:53,840 have to use the syntax so you can still 5708 04:37:50,400 --> 04:37:57,080 use this I would say more userfriendly 5709 04:37:53,840 --> 04:37:59,920 syntax but I do want to showcase that 5710 04:37:57,080 --> 04:38:02,879 since this interface is an array we also 5711 04:37:59,920 --> 04:38:04,320 have this option so now let me comment I 5712 04:38:02,879 --> 04:38:06,920 guess these ones out you know what let 5713 04:38:04,320 --> 04:38:09,160 let me start everything from scratch so 5714 04:38:06,920 --> 04:38:11,600 I want to go here with let and again I'm 5715 04:38:09,160 --> 04:38:14,240 going to call this array one and and 5716 04:38:11,600 --> 04:38:16,840 then as far as the type sorry I'm not 5717 04:38:14,240 --> 04:38:20,760 going to go with string I will go with 5718 04:38:16,840 --> 04:38:22,840 array and then let's pass in the string 5719 04:38:20,760 --> 04:38:25,760 and now as far as the result well that 5720 04:38:22,840 --> 04:38:27,240 one is actually exactly the same so I'm 5721 04:38:25,760 --> 04:38:28,840 just saying yeah it's going to be an 5722 04:38:27,240 --> 04:38:30,719 array of 5723 04:38:28,840 --> 04:38:34,359 strings and 5724 04:38:30,719 --> 04:38:35,400 again this doesn't change the only thing 5725 04:38:34,359 --> 04:38:38,719 that's going to 5726 04:38:35,400 --> 04:38:41,439 change the type that we're passing in so 5727 04:38:38,719 --> 04:38:43,561 if we pass in the string and of course 5728 04:38:41,439 --> 04:38:45,279 course we'll get back array of strings 5729 04:38:43,561 --> 04:38:48,320 as well as all the methods that we can 5730 04:38:45,279 --> 04:38:51,080 call so if I call the pop on this one on 5731 04:38:48,320 --> 04:38:53,199 array one guess what it's going to 5732 04:38:51,080 --> 04:38:55,359 remove the last item and the type is 5733 04:38:53,199 --> 04:38:58,119 going to be string and of course we can 5734 04:38:55,359 --> 04:39:00,600 repeat the same for number and bullion 5735 04:38:58,119 --> 04:39:03,039 and with this in place now we can move 5736 04:39:00,600 --> 04:39:05,879 on to the next topic all right and once 5737 04:39:03,039 --> 04:39:08,199 we're familiar with the main Concepts 5738 04:39:05,879 --> 04:39:10,400 let's construct our own let's start easy 5739 04:39:08,199 --> 04:39:12,240 let's just go with the function as well 5740 04:39:10,400 --> 04:39:15,920 as the interface 5741 04:39:12,240 --> 04:39:20,639 so here's the goal we want to create a 5742 04:39:15,920 --> 04:39:25,520 function which is going to take any type 5743 04:39:20,639 --> 04:39:27,600 and return the same type so one approach 5744 04:39:25,520 --> 04:39:30,439 could be following we can go with 5745 04:39:27,600 --> 04:39:32,680 function then let me go with create 5746 04:39:30,439 --> 04:39:34,600 string as far as the argument I'm going 5747 04:39:32,680 --> 04:39:37,878 to call this string basically I'll give 5748 04:39:34,600 --> 04:39:40,000 it a type string it returns a string and 5749 04:39:37,878 --> 04:39:42,040 then from the function we return the 5750 04:39:40,000 --> 04:39:44,440 argument now 5751 04:39:42,040 --> 04:39:49,920 everything is awesome but we pretty much 5752 04:39:44,440 --> 04:39:52,638 need to return the same function for any 5753 04:39:49,920 --> 04:39:56,320 type so we need to create one for the 5754 04:39:52,638 --> 04:40:00,520 number for Boolean and rest of them now 5755 04:39:56,320 --> 04:40:02,798 I'm not going to create function for all 5756 04:40:00,520 --> 04:40:05,878 of the types but you know what let me 5757 04:40:02,798 --> 04:40:08,638 just Showcase with a number so let's say 5758 04:40:05,878 --> 04:40:11,080 if from this function we want to return 5759 04:40:08,638 --> 04:40:13,840 a number this is what we'll need to do 5760 04:40:11,080 --> 04:40:17,040 and as you can see this gets annoying 5761 04:40:13,840 --> 04:40:18,480 pretty fast correct and we only have two 5762 04:40:17,040 --> 04:40:21,360 types so this is what I was mentioning 5763 04:40:18,480 --> 04:40:24,400 in the previous video imagine that array 5764 04:40:21,360 --> 04:40:26,638 interface if it's not a generic that 5765 04:40:24,400 --> 04:40:29,080 pretty much you'll need to copy and 5766 04:40:26,638 --> 04:40:30,320 paste and everywhere where you have a 5767 04:40:29,080 --> 04:40:32,920 string you'll have to change it to 5768 04:40:30,320 --> 04:40:34,958 number and Boolean and then rest of the 5769 04:40:32,920 --> 04:40:36,878 types so what's the solution well this 5770 04:40:34,958 --> 04:40:39,120 is where we use the 5771 04:40:36,878 --> 04:40:41,000 generic I will leave this one for your 5772 04:40:39,120 --> 04:40:43,000 reference or you know what actually you 5773 04:40:41,000 --> 04:40:44,280 have it going read me my bad let's start 5774 04:40:43,000 --> 04:40:45,920 from the scratch I think it's going to 5775 04:40:44,280 --> 04:40:48,600 be better we're going to go with 5776 04:40:45,920 --> 04:40:51,480 function then let's call this something 5777 04:40:48,600 --> 04:40:52,360 generic and I think generic function is 5778 04:40:51,480 --> 04:40:55,920 good 5779 04:40:52,360 --> 04:40:57,400 enough and we will set up the parameter 5780 04:40:55,920 --> 04:41:00,400 we will set up the 5781 04:40:57,400 --> 04:41:03,320 logic now I want to set up the variable 5782 04:41:00,400 --> 04:41:06,560 for my type so instead of hard coding 5783 04:41:03,320 --> 04:41:10,840 instead of number and string I'll say it 5784 04:41:06,560 --> 04:41:13,040 can be any type and both my argument as 5785 04:41:10,840 --> 04:41:16,120 well as what I'm returning are going to 5786 04:41:13,040 --> 04:41:18,440 be that type so we go here with this 5787 04:41:16,120 --> 04:41:21,798 angle bracket like I said you can call 5788 04:41:18,440 --> 04:41:24,920 this shake and bake just make sure you 5789 04:41:21,798 --> 04:41:27,638 use the same name and again the 5790 04:41:24,920 --> 04:41:30,840 convention is just to go with d which 5791 04:41:27,638 --> 04:41:33,000 stands for type so we go here with t 5792 04:41:30,840 --> 04:41:35,680 then we say that yeah argument is 5793 04:41:33,000 --> 04:41:36,920 actually going to be this type so I'm 5794 04:41:35,680 --> 04:41:39,480 not going to say that it's a string 5795 04:41:36,920 --> 04:41:42,160 number whatever no whatever type we have 5796 04:41:39,480 --> 04:41:45,440 over here and as far as the return will 5797 04:41:42,160 --> 04:41:47,680 also go with the T this is what I'm 5798 04:41:45,440 --> 04:41:49,600 returning from this function and now we 5799 04:41:47,680 --> 04:41:53,000 just need to set up the logic where we 5800 04:41:49,600 --> 04:41:57,878 return the argument so now we 5801 04:41:53,000 --> 04:42:01,320 can go here and set up some string value 5802 04:41:57,878 --> 04:42:03,480 set it equal to generic function then 5803 04:42:01,320 --> 04:42:07,160 since I'm creating a string value this 5804 04:42:03,480 --> 04:42:10,000 is the type I want to pass in and then I 5805 04:42:07,160 --> 04:42:12,280 just want to provide a string let's save 5806 04:42:10,000 --> 04:42:16,280 this and now I can do the same thing for 5807 04:42:12,280 --> 04:42:19,240 the number so instead of string we're 5808 04:42:16,280 --> 04:42:22,600 going to go with number and instead of 5809 04:42:19,240 --> 04:42:25,200 type string will set up the number now 5810 04:42:22,600 --> 04:42:27,718 it does need to make sense as far as the 5811 04:42:25,200 --> 04:42:29,320 argument so it's not like I'll randomly 5812 04:42:27,718 --> 04:42:32,440 pass in the string as you can see 5813 04:42:29,320 --> 04:42:36,080 typescript is complaining and we set up 5814 04:42:32,440 --> 04:42:39,400 over here the number so now we can use 5815 04:42:36,080 --> 04:42:42,000 this function with any type even the 5816 04:42:39,400 --> 04:42:45,920 ones that we set up ourselves so this is 5817 04:42:42,000 --> 04:42:47,958 not limited anymore to one specific type 5818 04:42:45,920 --> 04:42:51,200 this right now is generic function where 5819 04:42:47,958 --> 04:42:54,040 we can pass in any type and we'll get 5820 04:42:51,200 --> 04:42:57,280 back the same type and while we're on 5821 04:42:54,040 --> 04:42:59,718 topic let's also take a look at the 5822 04:42:57,280 --> 04:43:02,120 interface so I'm going to go with 5823 04:42:59,718 --> 04:43:03,000 interface and let me right away call 5824 04:43:02,120 --> 04:43:07,280 this 5825 04:43:03,000 --> 04:43:09,440 generic interface same deal if we want 5826 04:43:07,280 --> 04:43:12,040 to set up a generic one we already know 5827 04:43:09,440 --> 04:43:15,080 that we need to go with angle brackets 5828 04:43:12,040 --> 04:43:17,160 let me stick with convention with the T 5829 04:43:15,080 --> 04:43:21,040 and then there's going to be a value 5830 04:43:17,160 --> 04:43:25,200 property the type will be whatever we're 5831 04:43:21,040 --> 04:43:28,080 setting up over here SD that's why they 5832 04:43:25,200 --> 04:43:30,520 need to match and let's also set up the 5833 04:43:28,080 --> 04:43:33,000 get value 5834 04:43:30,520 --> 04:43:36,000 function which is not going to take any 5835 04:43:33,000 --> 04:43:41,480 parameters but it will return the same 5836 04:43:36,000 --> 04:43:43,560 type and now let's create a new instance 5837 04:43:41,480 --> 04:43:45,520 I'm going to call this generic string 5838 04:43:43,560 --> 04:43:46,958 which is somewhat funny but let's just 5839 04:43:45,520 --> 04:43:48,878 stick with that one since I have that in 5840 04:43:46,958 --> 04:43:52,958 a read me so we're going to go with 5841 04:43:48,878 --> 04:43:56,320 generic string we go here with generic 5842 04:43:52,958 --> 04:43:59,000 interface we say that yep as far as the 5843 04:43:56,320 --> 04:44:02,040 type it's actually going to be a string 5844 04:43:59,000 --> 04:44:04,798 and now inside of our instance of course 5845 04:44:02,040 --> 04:44:06,680 we need to follow the same rules so the 5846 04:44:04,798 --> 04:44:09,840 value I'm going to be setting up has to 5847 04:44:06,680 --> 04:44:13,718 be a string so we'll go here with hello 5848 04:44:09,840 --> 04:44:17,680 world and then as far as get value 5849 04:44:13,718 --> 04:44:21,200 one well let's just return this do value 5850 04:44:17,680 --> 04:44:24,878 return this do value let's save it and 5851 04:44:21,200 --> 04:44:27,680 once I save I have right now the string 5852 04:44:24,878 --> 04:44:30,840 instance from my generic interface so 5853 04:44:27,680 --> 04:44:34,718 this is how we can construct our own 5854 04:44:30,840 --> 04:44:37,200 generic functions as well as generic 5855 04:44:34,718 --> 04:44:40,798 interfaces all right and before we start 5856 04:44:37,200 --> 04:44:43,080 working on the more complex examples I 5857 04:44:40,798 --> 04:44:45,320 do you want to showcase something most 5858 04:44:43,080 --> 04:44:47,600 likely as you're looking at these first 5859 04:44:45,320 --> 04:44:51,320 few examples you're like I mean this is 5860 04:44:47,600 --> 04:44:53,560 ridiculous what is the actual need for 5861 04:44:51,320 --> 04:44:56,160 such functions I mean I do understand 5862 04:44:53,560 --> 04:44:57,920 yeah there's angle brackets yeah I can 5863 04:44:56,160 --> 04:45:02,080 use this T and it's going to work with 5864 04:44:57,920 --> 04:45:06,040 any type but in reality this is just 5865 04:45:02,080 --> 04:45:09,280 silly okay hold that thought and let's 5866 04:45:06,040 --> 04:45:11,480 just set up a a sync function and 5867 04:45:09,280 --> 04:45:14,440 remember in JavaScript 5868 04:45:11,480 --> 04:45:17,958 what do async functions return they 5869 04:45:14,440 --> 04:45:20,480 return a promise so let's go here with 5870 04:45:17,958 --> 04:45:23,200 async call this function I'm going to go 5871 04:45:20,480 --> 04:45:25,000 with some funk for now let's not worry 5872 04:45:23,200 --> 04:45:27,280 about the return or you know what no 5873 04:45:25,000 --> 04:45:29,160 let's set it equal to a string do 5874 04:45:27,280 --> 04:45:31,760 definitely want to showcase something so 5875 04:45:29,160 --> 04:45:33,600 we right away have the error this is 5876 04:45:31,760 --> 04:45:37,958 something we're going to deal with for 5877 04:45:33,600 --> 04:45:40,760 now let's just return and hello world so 5878 04:45:37,958 --> 04:45:43,280 technically it's kind of correct we're 5879 04:45:40,760 --> 04:45:45,120 return a string so what is typescript 5880 04:45:43,280 --> 04:45:48,638 complaining well 5881 04:45:45,120 --> 04:45:51,840 again same functions return what they 5882 04:45:48,638 --> 04:45:54,878 return a promise so now let's do the 5883 04:45:51,840 --> 04:45:56,680 same thing like we did with the arrays 5884 04:45:54,878 --> 04:45:58,840 let's look at the type that we're 5885 04:45:56,680 --> 04:46:01,520 getting back and hopefully in the 5886 04:45:58,840 --> 04:46:04,600 process you'll see that even though this 5887 04:46:01,520 --> 04:46:08,400 might look somewhat silly essentially 5888 04:46:04,600 --> 04:46:11,840 this is the foundation that you'll see 5889 04:46:08,400 --> 04:46:14,638 over and over and over in document ation 5890 04:46:11,840 --> 04:46:18,520 so if I'm going to go here with 5891 04:46:14,638 --> 04:46:22,160 result and some funk and we take a look 5892 04:46:18,520 --> 04:46:24,200 at the result I mean it says that it's a 5893 04:46:22,160 --> 04:46:27,120 string but we clearly have this issue 5894 04:46:24,200 --> 04:46:29,280 over here and notice now we have this 5895 04:46:27,120 --> 04:46:33,638 the return type of a sync function or 5896 04:46:29,280 --> 04:46:36,638 method must be the global promise again 5897 04:46:33,638 --> 04:46:40,958 funny looking angle bracket type did you 5898 04:46:36,638 --> 04:46:43,760 mean to write promise and hm interesting 5899 04:46:40,958 --> 04:46:47,240 we actually have the generic again so 5900 04:46:43,760 --> 04:46:50,000 you know what let me remove the return 5901 04:46:47,240 --> 04:46:52,440 type for now I'll rely on typescript to 5902 04:46:50,000 --> 04:46:54,360 infer a type and now let's click on the 5903 04:46:52,440 --> 04:46:57,798 result and type 5904 04:46:54,360 --> 04:46:59,680 definition and same deal noce we have 5905 04:46:57,798 --> 04:47:03,280 over here this 5906 04:46:59,680 --> 04:47:06,200 interface the name is promise and again 5907 04:47:03,280 --> 04:47:09,878 it's a generic and yes it has a little 5908 04:47:06,200 --> 04:47:13,760 bit more code but not just the general 5909 04:47:09,878 --> 04:47:18,520 idea effectively we just get back this 5910 04:47:13,760 --> 04:47:20,360 promise and in there we have the type so 5911 04:47:18,520 --> 04:47:24,520 the point is that every time we have 5912 04:47:20,360 --> 04:47:27,840 this async function we do need to set up 5913 04:47:24,520 --> 04:47:31,760 promise as a return type and we just 5914 04:47:27,840 --> 04:47:33,798 need to remember that it's a generic so 5915 04:47:31,760 --> 04:47:36,958 instead of typing string like I was 5916 04:47:33,798 --> 04:47:40,280 doing before we just go with okay we're 5917 04:47:36,958 --> 04:47:44,280 returning a promise but since it's a 5918 04:47:40,280 --> 04:47:46,840 generic we need to specify the type in 5919 04:47:44,280 --> 04:47:50,558 the angle brackets so again the main 5920 04:47:46,840 --> 04:47:52,480 point is that yes I know that this might 5921 04:47:50,558 --> 04:47:54,680 look silly but keep in mind that this is 5922 04:47:52,480 --> 04:47:58,520 going to be the foundation that you see 5923 04:47:54,680 --> 04:48:00,760 in documentation as silly as it looks it 5924 04:47:58,520 --> 04:48:04,080 actually handles all of the cases that 5925 04:48:00,760 --> 04:48:07,920 we're interested because in here I can 5926 04:48:04,080 --> 04:48:09,920 return whichever type I need in that 5927 04:48:07,920 --> 04:48:13,718 particular instance and I simply just 5928 04:48:09,920 --> 04:48:16,320 need to provide the value over here in 5929 04:48:13,718 --> 04:48:20,480 the angle bracket so let's say if I'm 5930 04:48:16,320 --> 04:48:22,240 going to go with 234 or whatever number 5931 04:48:20,480 --> 04:48:25,280 then is going to complain so now I just 5932 04:48:22,240 --> 04:48:27,600 need to go over here and say yeah I will 5933 04:48:25,280 --> 04:48:30,638 return a promise because it's a sync 5934 04:48:27,600 --> 04:48:34,680 function but the type in this case is 5935 04:48:30,638 --> 04:48:39,200 going to be a number so yes even 5936 04:48:34,680 --> 04:48:41,280 though our first two examples might look 5937 04:48:39,200 --> 04:48:44,360 super silly 5938 04:48:41,280 --> 04:48:47,638 they use the same concept that you're 5939 04:48:44,360 --> 04:48:49,280 going to see later in declaration files 5940 04:48:47,638 --> 04:48:51,958 and with this in place now we can move 5941 04:48:49,280 --> 04:48:54,958 on to the next topic all right and once 5942 04:48:51,958 --> 04:48:58,280 we are familiar with the fundamentals of 5943 04:48:54,958 --> 04:49:01,718 generics let's work on a tiny challenge 5944 04:48:58,280 --> 04:49:04,080 where we want to set up a function in my 5945 04:49:01,718 --> 04:49:07,320 case I'm going to call this create array 5946 04:49:04,080 --> 04:49:11,878 which takes in two things it takes the 5947 04:49:07,320 --> 04:49:14,440 number length so that just controls the 5948 04:49:11,878 --> 04:49:17,440 length of the array so if I pass here 5949 04:49:14,440 --> 04:49:19,958 three then I'm going to have three items 5950 04:49:17,440 --> 04:49:22,638 if I have pass here 15 then obviously 5951 04:49:19,958 --> 04:49:26,920 it's going to be 15 and then the second 5952 04:49:22,638 --> 04:49:29,920 thing is going to be the actual value so 5953 04:49:26,920 --> 04:49:33,160 if I pass in the string then I should be 5954 04:49:29,920 --> 04:49:34,878 getting back the array of strings if 5955 04:49:33,160 --> 04:49:36,760 it's going to be number then it's going 5956 04:49:34,878 --> 04:49:39,798 to be an array of numbers and hopefully 5957 04:49:36,760 --> 04:49:42,000 you see where I'm going with this and I 5958 04:49:39,798 --> 04:49:45,240 guess let's start 5959 04:49:42,000 --> 04:49:46,360 Again by hardcoding hopefully in the 5960 04:49:45,240 --> 04:49:50,920 process we'll 5961 04:49:46,360 --> 04:49:52,760 see why we want to use generics but 5962 04:49:50,920 --> 04:49:54,080 first let's create a function I'm going 5963 04:49:52,760 --> 04:49:57,958 to go with 5964 04:49:54,080 --> 04:50:00,600 generate string array like I said we're 5965 04:49:57,958 --> 04:50:03,718 looking for two things length is going 5966 04:50:00,600 --> 04:50:06,080 to be the length of an array and also 5967 04:50:03,718 --> 04:50:08,360 well what is the actual value and as far 5968 04:50:06,080 --> 04:50:10,760 as the type I'm going to go with string 5969 04:50:08,360 --> 04:50:14,718 now from this function since I'm taking 5970 04:50:10,760 --> 04:50:18,400 in the string type I want to return a 5971 04:50:14,718 --> 04:50:21,920 string and array now when it comes to 5972 04:50:18,400 --> 04:50:26,520 logic I want to create the result 5973 04:50:21,920 --> 04:50:28,680 variable this will actually be string 5974 04:50:26,520 --> 04:50:32,400 and array and for now it's just going to 5975 04:50:28,680 --> 04:50:36,520 be empty array but yes we're going to 5976 04:50:32,400 --> 04:50:40,400 store over here the strings okay good 5977 04:50:36,520 --> 04:50:43,160 then let's assign result to array so 5978 04:50:40,400 --> 04:50:46,040 again we can use our object in there 5979 04:50:43,160 --> 04:50:47,958 first we want to pass in the length so 5980 04:50:46,040 --> 04:50:49,638 how long is going to be the array how 5981 04:50:47,958 --> 04:50:51,920 many items we're going to have over 5982 04:50:49,638 --> 04:50:54,160 there and then let's just call the fill 5983 04:50:51,920 --> 04:50:56,680 method so pretty straightforward 5984 04:50:54,160 --> 04:51:00,840 whatever value we pass over here we'll 5985 04:50:56,680 --> 04:51:02,920 add to our result so we'll call fill and 5986 04:51:00,840 --> 04:51:04,840 we'll pass in the value and then from 5987 04:51:02,920 --> 04:51:07,320 this function since I want to get the 5988 04:51:04,840 --> 04:51:11,480 array of strings let's just go the 5989 04:51:07,320 --> 04:51:15,080 return and result let's save it and 5990 04:51:11,480 --> 04:51:18,878 again everything is awesome but I need 5991 04:51:15,080 --> 04:51:22,360 also the same array for numbers booleans 5992 04:51:18,878 --> 04:51:25,558 or any other type so now we just need to 5993 04:51:22,360 --> 04:51:27,680 make this function generic or you know 5994 04:51:25,558 --> 04:51:31,600 what let me just quickly showus that the 5995 04:51:27,680 --> 04:51:33,920 code actually works so generate string 5996 04:51:31,600 --> 04:51:36,000 array how many items I don't know let's 5997 04:51:33,920 --> 04:51:37,638 go with six because I have the example 5998 04:51:36,000 --> 04:51:40,638 of three so you probably don't believe 5999 04:51:37,638 --> 04:51:43,520 me let me go over here and notice 6000 04:51:40,638 --> 04:51:46,600 everything works so I get back my array 6001 04:51:43,520 --> 04:51:49,878 it's typ string and I have six items 6002 04:51:46,600 --> 04:51:53,718 okay beautiful so now how we can make 6003 04:51:49,878 --> 04:51:55,718 this function generic and again most of 6004 04:51:53,718 --> 04:51:58,000 the logic is actually going to be the 6005 04:51:55,718 --> 04:52:00,160 same the only difference everywhere 6006 04:51:58,000 --> 04:52:03,160 where we have the string we just want to 6007 04:52:00,160 --> 04:52:06,400 convert it to a generic we want to 6008 04:52:03,160 --> 04:52:09,840 convert it to that variable and as a 6009 04:52:06,400 --> 04:52:11,080 result we'll be able to pass pretty much 6010 04:52:09,840 --> 04:52:13,718 any type 6011 04:52:11,080 --> 04:52:17,080 so let me copy and paste instead of 6012 04:52:13,718 --> 04:52:21,120 generate string array we'll just call 6013 04:52:17,080 --> 04:52:23,680 this create array create array length 6014 04:52:21,120 --> 04:52:26,638 stays the same but this one actually is 6015 04:52:23,680 --> 04:52:29,280 going to be equal to at and now we want 6016 04:52:26,638 --> 04:52:32,760 to set up that angle bracket so again we 6017 04:52:29,280 --> 04:52:36,718 set up this variable for a type and we 6018 04:52:32,760 --> 04:52:39,040 pass here DT so now everywhere where we 6019 04:52:36,718 --> 04:52:41,638 want to reference the type we'll use the 6020 04:52:39,040 --> 04:52:43,878 T instead so here instead of string 6021 04:52:41,638 --> 04:52:48,080 instead of hardcoding that we'll go with 6022 04:52:43,878 --> 04:52:50,680 array and pass in the T then instead of 6023 04:52:48,080 --> 04:52:52,958 strings we're going to say that yeah the 6024 04:52:50,680 --> 04:52:56,480 result is going to be just an empty 6025 04:52:52,958 --> 04:52:59,040 array of this specific type this doesn't 6026 04:52:56,480 --> 04:53:02,120 change this doesn't change now we just 6027 04:52:59,040 --> 04:53:04,400 want to invoke it and we'll do the same 6028 04:53:02,120 --> 04:53:07,520 thing like in the read me we're going to 6029 04:53:04,400 --> 04:53:10,280 go with strings then create 6030 04:53:07,520 --> 04:53:13,200 array we want to provide string over 6031 04:53:10,280 --> 04:53:15,760 here here and far as the value let's go 6032 04:53:13,200 --> 04:53:19,680 10 the more the better I 6033 04:53:15,760 --> 04:53:20,958 guess let's copy and paste and let's 6034 04:53:19,680 --> 04:53:23,440 call this 6035 04:53:20,958 --> 04:53:26,160 numbers so 6036 04:53:23,440 --> 04:53:28,638 numbers this will be a 6037 04:53:26,160 --> 04:53:31,840 number uh let's go with 6038 04:53:28,638 --> 04:53:35,040 15 and as far as the value I'm going to 6039 04:53:31,840 --> 04:53:39,360 go with 100 so now we just want to log 6040 04:53:35,040 --> 04:53:41,320 both of these values array of numbers 6041 04:53:39,360 --> 04:53:44,600 and strings 6042 04:53:41,320 --> 04:53:48,400 strings let me move this one 6043 04:53:44,600 --> 04:53:50,440 up just because don't ask me why and if 6044 04:53:48,400 --> 04:53:53,760 everything is correct this is the result 6045 04:53:50,440 --> 04:53:56,680 we should see in the console so this is 6046 04:53:53,760 --> 04:54:00,480 how we can create a function which takes 6047 04:53:56,680 --> 04:54:04,718 any type and returns an array with 6048 04:54:00,480 --> 04:54:06,320 specific length of that particular type 6049 04:54:04,718 --> 04:54:10,760 all right and up next let's see how we 6050 04:54:06,320 --> 04:54:13,440 can set up multiple variable types and 6051 04:54:10,760 --> 04:54:16,360 in my case I'm going to use the function 6052 04:54:13,440 --> 04:54:19,400 example so we want to set up a function 6053 04:54:16,360 --> 04:54:21,400 called pair which takes in two 6054 04:54:19,400 --> 04:54:24,000 parameters however the type is not going 6055 04:54:21,400 --> 04:54:26,680 to be the same and then from the 6056 04:54:24,000 --> 04:54:30,760 function we just return both of those 6057 04:54:26,680 --> 04:54:34,360 parameters but again we need to return 6058 04:54:30,760 --> 04:54:38,320 the same types so let's go here with 6059 04:54:34,360 --> 04:54:40,760 function again the name will be pair we 6060 04:54:38,320 --> 04:54:43,840 start with our good old T so we already 6061 04:54:40,760 --> 04:54:47,440 know that one but if we have multiple 6062 04:54:43,840 --> 04:54:49,840 variables like this we just add a comma 6063 04:54:47,440 --> 04:54:52,798 and then provide the value again it's a 6064 04:54:49,840 --> 04:54:56,440 convention to go with du and then if you 6065 04:54:52,798 --> 04:55:00,520 have more parameters just follow the 6066 04:54:56,440 --> 04:55:03,440 logic over here but you can name them of 6067 04:55:00,520 --> 04:55:07,878 course differently as well then we're 6068 04:55:03,440 --> 04:55:10,798 going to set up our param I guess one so 6069 04:55:07,878 --> 04:55:14,400 this one will be equal to T and and then 6070 04:55:10,798 --> 04:55:16,320 param 2 is going to be equal to U like I 6071 04:55:14,400 --> 04:55:19,480 said from this function I want to return 6072 04:55:16,320 --> 04:55:24,000 an array and I want to keep the same 6073 04:55:19,480 --> 04:55:26,440 types so this will be equal to D and U 6074 04:55:24,000 --> 04:55:30,638 and let's just set up the logic where we 6075 04:55:26,440 --> 04:55:33,320 have Pam one comma and then Pam 2 so now 6076 04:55:30,638 --> 04:55:37,120 let's invoke it and if everything is 6077 04:55:33,320 --> 04:55:38,680 correct we should get the correct result 6078 04:55:37,120 --> 04:55:41,558 so we're going to go with the result 6079 04:55:38,680 --> 04:55:44,480 that one is equal to pair 6080 04:55:41,558 --> 04:55:45,480 and then I'm going to go with number and 6081 04:55:44,480 --> 04:55:48,840 a 6082 04:55:45,480 --> 04:55:51,680 string let's go with one two three and 6083 04:55:48,840 --> 04:55:55,440 hello and check it out typescript is not 6084 04:55:51,680 --> 04:56:00,040 complaining so if you have more variable 6085 04:55:55,440 --> 04:56:02,558 types add a comma and then the common 6086 04:56:00,040 --> 04:56:05,040 approach is to go with you and then rest 6087 04:56:02,558 --> 04:56:07,760 of the letters that follow you if you 6088 04:56:05,040 --> 04:56:09,840 have more parameters or you can come up 6089 04:56:07,760 --> 04:56:13,680 with your own names just make sure you 6090 04:56:09,840 --> 04:56:16,320 use the same name in the parameters as 6091 04:56:13,680 --> 04:56:18,440 well as the function logic and up next 6092 04:56:16,320 --> 04:56:21,480 let me quickly show you that t script 6093 04:56:18,440 --> 04:56:24,280 can still infer the type even if we 6094 04:56:21,480 --> 04:56:26,798 don't provide the types here in the 6095 04:56:24,280 --> 04:56:29,400 angle brackets but just like before we 6096 04:56:26,798 --> 04:56:32,000 need to be careful yes if you're 6097 04:56:29,400 --> 04:56:35,958 expecting to get back the number and a 6098 04:56:32,000 --> 04:56:38,200 string everything is awesome but if by 6099 04:56:35,958 --> 04:56:40,360 mistake you pass in some other type it's 6100 04:56:38,200 --> 04:56:43,798 not like typescript can magically figure 6101 04:56:40,360 --> 04:56:46,600 this out so therefore if you don't pass 6102 04:56:43,798 --> 04:56:49,760 in the types You're Expecting just make 6103 04:56:46,600 --> 04:56:52,400 sure that you pass in the correct values 6104 04:56:49,760 --> 04:56:53,718 hopefully that is clear and as aen this 6105 04:56:52,400 --> 04:56:57,160 is something that we're going to see 6106 04:56:53,718 --> 04:57:00,920 later when we start working with react 6107 04:56:57,160 --> 04:57:03,280 where use State hook actually is a 6108 04:57:00,920 --> 04:57:06,160 generic which again if we think about it 6109 04:57:03,280 --> 04:57:09,958 kind of makes sense right I 6110 04:57:06,160 --> 04:57:10,878 mean we can pass in string into use 6111 04:57:09,958 --> 04:57:13,638 state 6112 04:57:10,878 --> 04:57:17,558 same how we can pass in the array so 6113 04:57:13,638 --> 04:57:19,878 this used State hook needs to accept 6114 04:57:17,558 --> 04:57:23,958 whatever value we pass in whether that 6115 04:57:19,878 --> 04:57:25,080 is a string number all the way to array 6116 04:57:23,958 --> 04:57:28,120 of 6117 04:57:25,080 --> 04:57:32,240 objects and essentially since it's a 6118 04:57:28,120 --> 04:57:34,360 generic we can technically bypass the 6119 04:57:32,240 --> 04:57:38,080 angle brackets so let's say if it's a 6120 04:57:34,360 --> 04:57:39,520 string I just let typescript to infer 6121 04:57:38,080 --> 04:57:41,840 that yes for sure it's going to be a 6122 04:57:39,520 --> 04:57:44,160 string since I'm passing in the empty 6123 04:57:41,840 --> 04:57:46,798 string but for example when we're going 6124 04:57:44,160 --> 04:57:49,558 to be setting up the Rays typescript is 6125 04:57:46,798 --> 04:57:51,840 going to complain and at that point 6126 04:57:49,558 --> 04:57:54,920 we'll need to specifically let 6127 04:57:51,840 --> 04:57:57,638 typescript know hey is it going to be an 6128 04:57:54,920 --> 04:58:00,400 array of strings numbers and in this 6129 04:57:57,638 --> 04:58:03,000 case I'm just coming up with some fake 6130 04:58:00,400 --> 04:58:06,400 product type so hopefully that is clear 6131 04:58:03,000 --> 04:58:07,480 yes typescript can still infer the type 6132 04:58:06,400 --> 04:58:11,040 just be 6133 04:58:07,480 --> 04:58:13,040 mindful when you use such a pro approach 6134 04:58:11,040 --> 04:58:16,280 and also I want to show you how we can 6135 04:58:13,040 --> 04:58:20,718 set up the constraint on a type 6136 04:58:16,280 --> 04:58:23,280 essentially I can limit the type options 6137 04:58:20,718 --> 04:58:26,440 I can pass in so let's imagine this I 6138 04:58:23,280 --> 04:58:31,120 have a function of process value 6139 04:58:26,440 --> 04:58:34,878 function process value and in here I 6140 04:58:31,120 --> 04:58:38,040 have my generic T but I only want to 6141 04:58:34,878 --> 04:58:41,480 limit this to a string so the keyword 6142 04:58:38,040 --> 04:58:44,160 we're looking for is extends and then in 6143 04:58:41,480 --> 04:58:46,600 here we pass in the specific type now in 6144 04:58:44,160 --> 04:58:48,480 the following video I'll show you a 6145 04:58:46,600 --> 04:58:50,760 different example so don't think we can 6146 04:58:48,480 --> 04:58:52,878 only use it here with primitive types 6147 04:58:50,760 --> 04:58:54,520 but let's just start nice and easy so 6148 04:58:52,878 --> 04:58:57,160 we're going to go over here with the 6149 04:58:54,520 --> 04:58:58,840 string then we want to set up a 6150 04:58:57,160 --> 04:59:01,440 parameter again I'm going to say that 6151 04:58:58,840 --> 04:59:04,280 it's equal to at and you know what let's 6152 04:59:01,440 --> 04:59:08,080 say that we're also going to return it 6153 04:59:04,280 --> 04:59:10,680 so in here let's log it let's say value 6154 04:59:08,080 --> 04:59:14,480 and let's also return the value let me 6155 04:59:10,680 --> 04:59:16,760 save it let me call Process value now in 6156 04:59:14,480 --> 04:59:19,558 this case I'm just going to infert the 6157 04:59:16,760 --> 04:59:21,878 type and I'm going to say hello and as 6158 04:59:19,558 --> 04:59:25,080 you're looking at it you're like well I 6159 04:59:21,878 --> 04:59:27,280 mean at this point we can simply set up 6160 04:59:25,080 --> 04:59:29,040 a function without the generic correct 6161 04:59:27,280 --> 04:59:31,600 so I can simply say that yeah I'm going 6162 04:59:29,040 --> 04:59:34,920 to be passing in the string and I'm 6163 04:59:31,600 --> 04:59:36,958 expecting a string back and you're 100% 6164 04:59:34,920 --> 04:59:40,200 correct so with this particular example 6165 04:59:36,958 --> 04:59:42,360 I mean there is no real benefit of 6166 04:59:40,200 --> 04:59:47,360 setting up the type and then extending 6167 04:59:42,360 --> 04:59:50,240 it only down to a string but again it is 6168 04:59:47,360 --> 04:59:52,280 a option and in a following video you'll 6169 04:59:50,240 --> 04:59:55,080 see a more complex example where 6170 04:59:52,280 --> 04:59:57,760 actually it definitely makes sense now 6171 04:59:55,080 --> 04:59:59,638 we can also use the union type so 6172 04:59:57,760 --> 05:00:03,638 effectively I can go with string or 6173 04:59:59,638 --> 05:00:07,240 number and at this point if I call in 6174 05:00:03,638 --> 05:00:09,320 process value I'm able to pass in the 6175 05:00:07,240 --> 05:00:11,958 number as well so for example I can go 6176 05:00:09,320 --> 05:00:14,280 with 12 now if I'm going to go with 6177 05:00:11,958 --> 05:00:16,240 process value and then pass in I don't 6178 05:00:14,280 --> 05:00:18,320 know true then of course typescript is 6179 05:00:16,240 --> 05:00:22,600 going to complain because we can only 6180 05:00:18,320 --> 05:00:24,840 pass in string or number in this generic 6181 05:00:22,600 --> 05:00:28,798 function and once we're familiar with 6182 05:00:24,840 --> 05:00:32,400 the extends keyword now let's see a more 6183 05:00:28,798 --> 05:00:35,160 complex example where we have our own 6184 05:00:32,400 --> 05:00:38,440 types so let's imagine the scenario 6185 05:00:35,160 --> 05:00:41,120 where I have type car two properties 6186 05:00:38,440 --> 05:00:44,320 branded model both of them strings then 6187 05:00:41,120 --> 05:00:46,280 I create the instance same with product 6188 05:00:44,320 --> 05:00:48,798 only in this case the properties are 6189 05:00:46,280 --> 05:00:51,760 name and price and I have string and 6190 05:00:48,798 --> 05:00:53,878 number again I create the instance and 6191 05:00:51,760 --> 05:00:57,320 the same with students so the properties 6192 05:00:53,878 --> 05:01:01,000 is the same the first one name it's a 6193 05:00:57,320 --> 05:01:03,320 string but then the second one is age 6194 05:01:01,000 --> 05:01:05,360 and in here I want to come up with a 6195 05:01:03,320 --> 05:01:08,040 function I guess I'm going to go with 6196 05:01:05,360 --> 05:01:10,920 print name because why not where I just 6197 05:01:08,040 --> 05:01:15,080 want to access the name prop property so 6198 05:01:10,920 --> 05:01:18,718 let's go with our function print name 6199 05:01:15,080 --> 05:01:20,440 then let's provide the type the generic 6200 05:01:18,718 --> 05:01:23,160 in this case we're going to say that 6201 05:01:20,440 --> 05:01:24,680 input is also going to be D and we're 6202 05:01:23,160 --> 05:01:28,080 not going to return it so I'm going to 6203 05:01:24,680 --> 05:01:31,080 go with void now even if we just try to 6204 05:01:28,080 --> 05:01:35,840 conso log input that name typescript is 6205 05:01:31,080 --> 05:01:38,840 going to complain why because it has no 6206 05:01:35,840 --> 05:01:41,680 guarantee that there will be that name 6207 05:01:38,840 --> 05:01:45,440 properly so in this scenario we're not 6208 05:01:41,680 --> 05:01:49,680 going to extend to a string we actually 6209 05:01:45,440 --> 05:01:52,360 need to look for the types which have 6210 05:01:49,680 --> 05:01:54,718 that name property correct and in our 6211 05:01:52,360 --> 05:01:57,400 case we can either go with the student 6212 05:01:54,718 --> 05:02:00,878 or with the product we just set up over 6213 05:01:57,400 --> 05:02:02,440 here extends and we go with student so 6214 05:02:00,878 --> 05:02:05,280 just like before with string number 6215 05:02:02,440 --> 05:02:09,558 Boolean or whatever we can use our own 6216 05:02:05,280 --> 05:02:13,440 types as well so now if I go and invoke 6217 05:02:09,558 --> 05:02:15,320 the print name in here and if I'm going 6218 05:02:13,440 --> 05:02:18,280 to pass in the student everything is 6219 05:02:15,320 --> 05:02:21,360 going to work now it's going to fail if 6220 05:02:18,280 --> 05:02:24,558 I pass in the product even though it has 6221 05:02:21,360 --> 05:02:26,600 the name correct let's try it out so I'm 6222 05:02:24,558 --> 05:02:30,080 going to copy and paste and once we 6223 05:02:26,600 --> 05:02:31,920 invoke the not print name print name 6224 05:02:30,080 --> 05:02:33,558 sorry we're looking for a product of 6225 05:02:31,920 --> 05:02:37,400 course typescript is going to complain 6226 05:02:33,558 --> 05:02:40,320 because the type is product in this case 6227 05:02:37,400 --> 05:02:43,200 so yes we can still use over here the 6228 05:02:40,320 --> 05:02:45,798 union type and passing the product but 6229 05:02:43,200 --> 05:02:47,920 there's actually a better way because if 6230 05:02:45,798 --> 05:02:51,280 we think about it what are we going to 6231 05:02:47,920 --> 05:02:53,480 do now we'll look for all of the types 6232 05:02:51,280 --> 05:02:57,000 where we have the name and then 6233 05:02:53,480 --> 05:03:00,160 essentially just use the union type and 6234 05:02:57,000 --> 05:03:02,080 add it to extend I mean technically it's 6235 05:03:00,160 --> 05:03:04,920 an option but like I mentioned there's 6236 05:03:02,080 --> 05:03:07,798 actually a better way and that better 6237 05:03:04,920 --> 05:03:10,558 way is that we can set up an object we 6238 05:03:07,798 --> 05:03:13,200 can set which property we're looking for 6239 05:03:10,558 --> 05:03:15,240 and then we can pass here in the extends 6240 05:03:13,200 --> 05:03:18,920 now we can set it up as a separate 6241 05:03:15,240 --> 05:03:22,558 variable or we can pass here directly so 6242 05:03:18,920 --> 05:03:25,040 instead of passing in my student or 6243 05:03:22,558 --> 05:03:28,160 product types I can simply set up an 6244 05:03:25,040 --> 05:03:31,320 object and I can say Hey listen I'm 6245 05:03:28,160 --> 05:03:34,440 looking for name property and it has to 6246 05:03:31,320 --> 05:03:37,120 be a string so now that script is smart 6247 05:03:34,440 --> 05:03:38,760 where it says okay so student yeah it 6248 05:03:37,120 --> 05:03:42,000 does have a name so everything is going 6249 05:03:38,760 --> 05:03:44,760 to be fine same with the product but if 6250 05:03:42,000 --> 05:03:46,400 I'm going to try to call it with a car 6251 05:03:44,760 --> 05:03:48,798 it's not going to work because there is 6252 05:03:46,400 --> 05:03:51,600 no such property so if I pass in this 6253 05:03:48,798 --> 05:03:55,080 instance notice entually it complains 6254 05:03:51,600 --> 05:03:57,680 that the name property is missing in the 6255 05:03:55,080 --> 05:04:00,600 car so this is an interesting way how we 6256 05:03:57,680 --> 05:04:03,480 can use extends not only with a 6257 05:04:00,600 --> 05:04:06,958 primitive type or our own custom type 6258 05:04:03,480 --> 05:04:10,480 but also we can simply narrow down to 6259 05:04:06,958 --> 05:04:12,120 the shape that we are expecting okay and 6260 05:04:10,480 --> 05:04:14,958 at the very end of generics let me show 6261 05:04:12,120 --> 05:04:18,000 you how to set up default type just 6262 05:04:14,958 --> 05:04:21,480 please keep in mind that it's not going 6263 05:04:18,000 --> 05:04:24,878 to be super useful in our examples since 6264 05:04:21,480 --> 05:04:27,120 we're hardcoding all of our data anyway 6265 05:04:24,878 --> 05:04:30,160 basically they're going to be far more 6266 05:04:27,120 --> 05:04:33,680 useful when we don't know what is going 6267 05:04:30,160 --> 05:04:35,400 to be the shape of our data so let's 6268 05:04:33,680 --> 05:04:38,718 imagine we have 6269 05:04:35,400 --> 05:04:41,480 interface it's going to be a generic and 6270 05:04:38,718 --> 05:04:44,320 I'm going to call this store data I'm 6271 05:04:41,480 --> 05:04:46,320 going to say that I'm expecting T and 6272 05:04:44,320 --> 05:04:49,520 then inside of it there's going to be a 6273 05:04:46,320 --> 05:04:53,000 data and it's going to be equal to an 6274 05:04:49,520 --> 05:04:56,878 array of types so whatever we pass over 6275 05:04:53,000 --> 05:05:00,760 here then let's set up store numbers so 6276 05:04:56,878 --> 05:05:04,040 store numbers and first let's set up the 6277 05:05:00,760 --> 05:05:07,798 type so we go with store data then 6278 05:05:04,040 --> 05:05:09,878 number and then we set it equal to data 6279 05:05:07,798 --> 05:05:12,280 and in here since I want to store 6280 05:05:09,878 --> 05:05:18,040 numbers I'm just going to go with one 6281 05:05:12,280 --> 05:05:21,440 two three and four okay so this works 6282 05:05:18,040 --> 05:05:23,360 but what if let's say I have some random 6283 05:05:21,440 --> 05:05:26,558 stuff what if I don't know what I'm 6284 05:05:23,360 --> 05:05:28,480 going to have in the array well let's 6285 05:05:26,558 --> 05:05:33,558 try it out so we're going to go with 6286 05:05:28,480 --> 05:05:35,638 random stuff set it equal to store 6287 05:05:33,558 --> 05:05:38,600 data for now we're not going to pass 6288 05:05:35,638 --> 05:05:41,440 anything in we're going to go with data 6289 05:05:38,600 --> 05:05:43,920 and then we'll set up some random stuff 6290 05:05:41,440 --> 05:05:47,440 so I'm going to go with my string over 6291 05:05:43,920 --> 05:05:51,320 here as well as the number now store 6292 05:05:47,440 --> 05:05:54,240 data is expecting the type so at the 6293 05:05:51,320 --> 05:05:56,798 moment essentially we have two options 6294 05:05:54,240 --> 05:05:59,600 either we set up over here the default 6295 05:05:56,798 --> 05:06:04,718 one then we won't get the error over 6296 05:05:59,600 --> 05:06:07,878 here or since let's say I not sure what 6297 05:06:04,718 --> 05:06:11,600 data I'm expecting back technically I 6298 05:06:07,878 --> 05:06:14,680 can hardcode here any I can say yeah I 6299 05:06:11,600 --> 05:06:16,120 mean I'm not sure what values I'm going 6300 05:06:14,680 --> 05:06:19,958 to have in this array so I'm just going 6301 05:06:16,120 --> 05:06:21,200 to go with type any now if we want to 6302 05:06:19,958 --> 05:06:24,878 bypass 6303 05:06:21,200 --> 05:06:27,958 this essentially passing in any if we 6304 05:06:24,878 --> 05:06:31,200 don't know what data we're expecting we 6305 05:06:27,958 --> 05:06:33,120 can simply set D equal to any or any 6306 05:06:31,200 --> 05:06:35,638 other default type as a side note you 6307 05:06:33,120 --> 05:06:39,240 can go here for example with string or 6308 05:06:35,638 --> 05:06:43,320 whatever is just somewhat common to see 6309 05:06:39,240 --> 05:06:46,320 and as default type and in that case we 6310 05:06:43,320 --> 05:06:50,160 don't need to even provide it so if I 6311 05:06:46,320 --> 05:06:53,280 don't provide the type then right away 6312 05:06:50,160 --> 05:06:56,120 typescript is going to use any as my 6313 05:06:53,280 --> 05:06:58,958 default one again kind of hard to see 6314 05:06:56,120 --> 05:07:01,958 over here because we can clearly see 6315 05:06:58,958 --> 05:07:04,680 basically what we have in Array but just 6316 05:07:01,958 --> 05:07:08,718 to Showcase that I'm not making this up 6317 05:07:04,680 --> 05:07:09,958 let me grab the example from the readme 6318 05:07:08,718 --> 05:07:12,958 so if you're interested that you can 6319 05:07:09,958 --> 05:07:14,798 also copy and paste now there might be 6320 05:07:12,958 --> 05:07:16,798 some errors since I don't have those 6321 05:07:14,798 --> 05:07:19,520 values over here don't worry about it 6322 05:07:16,798 --> 05:07:20,360 too much I just mainly want to showcase 6323 05:07:19,520 --> 05:07:22,760 some 6324 05:07:20,360 --> 05:07:25,840 stuff most likely you're familiar with 6325 05:07:22,760 --> 05:07:29,798 the library called axios which is super 6326 05:07:25,840 --> 05:07:31,440 popular to make HTTP requests and 6327 05:07:29,798 --> 05:07:34,798 normally the syntax is following where 6328 05:07:31,440 --> 05:07:37,280 we go with AOS then the method Now by 6329 05:07:34,798 --> 05:07:40,200 default it's get but in here I hardcoded 6330 05:07:37,280 --> 05:07:41,600 get as well and we pass it the URL and 6331 05:07:40,200 --> 05:07:45,400 the data is 6332 05:07:41,600 --> 05:07:48,240 located in the object in data property 6333 05:07:45,400 --> 05:07:52,400 now if we take a look at their 6334 05:07:48,240 --> 05:07:55,760 documentation we can actually see that 6335 05:07:52,400 --> 05:07:59,320 get is a generic what does that mean 6336 05:07:55,760 --> 05:08:03,000 well it's looking for the type but if 6337 05:07:59,320 --> 05:08:06,160 it's not passed in over here it uses any 6338 05:08:03,000 --> 05:08:10,638 so if let's say you just called axios 6339 05:08:06,160 --> 05:08:12,958 doget it's going to set data as any so 6340 05:08:10,638 --> 05:08:15,680 whatever we're getting back and we can 6341 05:08:12,958 --> 05:08:18,600 actually see that if we continue 6342 05:08:15,680 --> 05:08:21,638 inspecting the documentation notice 6343 05:08:18,600 --> 05:08:24,200 there's also a second one which is r and 6344 05:08:21,638 --> 05:08:27,958 that one by default is equal to aio's 6345 05:08:24,200 --> 05:08:31,760 response and it uses the type we pass in 6346 05:08:27,958 --> 05:08:36,320 so essentially this is another generic 6347 05:08:31,760 --> 05:08:39,558 interface which is used as a second type 6348 05:08:36,320 --> 05:08:42,600 over here and essentially it uses the T 6349 05:08:39,558 --> 05:08:45,240 that we pass in now if we take a look at 6350 05:08:42,600 --> 05:08:46,958 the axio response in our generic one we 6351 05:08:45,240 --> 05:08:49,798 can see that we're getting back this 6352 05:08:46,958 --> 05:08:53,878 data and this is set equal to T but 6353 05:08:49,798 --> 05:08:55,840 again the default value is any so for 6354 05:08:53,878 --> 05:08:59,840 example if we don't 6355 05:08:55,840 --> 05:09:03,120 provide any info on a type it's going to 6356 05:08:59,840 --> 05:09:05,160 be equal to any because that's the 6357 05:09:03,120 --> 05:09:07,798 default as we can see it's used over 6358 05:09:05,160 --> 05:09:10,600 here it's passed down to AIO response 6359 05:09:07,798 --> 05:09:15,000 and the same deal in in the interface 6360 05:09:10,600 --> 05:09:17,878 now if we set up the angle brackets and 6361 05:09:15,000 --> 05:09:20,840 specifically say that Hey listen I'm 6362 05:09:17,878 --> 05:09:22,638 fetching this data but I know that 6363 05:09:20,840 --> 05:09:27,718 structure is going to be following I'm 6364 05:09:22,638 --> 05:09:31,240 going to get back the array of object 6365 05:09:27,718 --> 05:09:33,600 and in there I'm going to have name 6366 05:09:31,240 --> 05:09:38,798 which is going to be a string in that 6367 05:09:33,600 --> 05:09:40,360 case this data will have this shape so 6368 05:09:38,798 --> 05:09:42,798 if we 6369 05:09:40,360 --> 05:09:45,080 provide essentially a shape that we're 6370 05:09:42,798 --> 05:09:48,240 expecting then the Ste becomes that 6371 05:09:45,080 --> 05:09:52,440 shape and goes all the way down to data 6372 05:09:48,240 --> 05:09:56,000 now if not then they use the default 6373 05:09:52,440 --> 05:09:58,680 parameter and essentially set equal to 6374 05:09:56,000 --> 05:10:01,520 any now I know it's a lot of code in 6375 05:09:58,680 --> 05:10:04,040 here a lot of angle brackets and that 6376 05:10:01,520 --> 05:10:07,600 but hopefully it's clear now we can 6377 05:10:04,040 --> 05:10:09,360 nicely use type any if we don't know 6378 05:10:07,600 --> 05:10:12,000 what shape we're getting back because 6379 05:10:09,360 --> 05:10:15,400 again think about it this is an HTTP 6380 05:10:12,000 --> 05:10:17,200 request so the shape can literally be 6381 05:10:15,400 --> 05:10:20,080 anything and therefore the default one 6382 05:10:17,200 --> 05:10:22,520 is any and then if we provide specific 6383 05:10:20,080 --> 05:10:26,080 shape well that's the shape we're going 6384 05:10:22,520 --> 05:10:29,440 to have here in this data property okay 6385 05:10:26,080 --> 05:10:31,520 and while we're still on a topic of HTTP 6386 05:10:29,440 --> 05:10:35,920 let's also see how we can fetch data 6387 05:10:31,520 --> 05:10:39,840 with typescript now normally I use axos 6388 05:10:35,920 --> 05:10:42,760 library with react query simply because 6389 05:10:39,840 --> 05:10:46,000 the setup is super straightforward and 6390 05:10:42,760 --> 05:10:49,920 react query is really good at inferring 6391 05:10:46,000 --> 05:10:53,160 the types but let's start nice and easy 6392 05:10:49,920 --> 05:10:56,840 and cover up the straightup pitch API 6393 05:10:53,160 --> 05:10:59,000 first and also keep in mind that in this 6394 05:10:56,840 --> 05:11:01,840 example we'll just log the result so we 6395 05:10:59,000 --> 05:11:03,878 won't set any state or display pretty 6396 05:11:01,840 --> 05:11:06,680 much anything in a browser we'll just 6397 05:11:03,878 --> 05:11:09,680 check for correct results in developer 6398 05:11:06,680 --> 05:11:13,520 tools all of that is coming up just be 6399 05:11:09,680 --> 05:11:15,920 patient and wait for the react part of 6400 05:11:13,520 --> 05:11:20,160 the course now also you will need the 6401 05:11:15,920 --> 05:11:22,760 URL so in the read me I provided a URL 6402 05:11:20,160 --> 05:11:26,080 back to my server where we can just 6403 05:11:22,760 --> 05:11:28,280 fetch some random tours we were using 6404 05:11:26,080 --> 05:11:30,798 this one in my react 6405 05:11:28,280 --> 05:11:33,360 course basically build one of the 6406 05:11:30,798 --> 05:11:36,280 projects so this is totally up to you if 6407 05:11:33,360 --> 05:11:39,240 you want to use this URL feel free to do 6408 05:11:36,280 --> 05:11:41,760 so if not if you have some other URL you 6409 05:11:39,240 --> 05:11:44,638 you can of course use that as well and 6410 05:11:41,760 --> 05:11:47,958 for now we'll just simply set up a fetch 6411 05:11:44,638 --> 05:11:51,080 function we will log the result we'll 6412 05:11:47,958 --> 05:11:52,480 see the issue if we don't have any type 6413 05:11:51,080 --> 05:11:54,600 and then in the following video we'll 6414 05:11:52,480 --> 05:11:57,840 have a challenge where you'll have to 6415 05:11:54,600 --> 05:12:00,280 set up the type and proper returns and 6416 05:11:57,840 --> 05:12:03,360 all that cool stuff so for now let me 6417 05:12:00,280 --> 05:12:05,840 just grab I guess the URL so we're 6418 05:12:03,360 --> 05:12:08,320 looking for this one over here and we 6419 05:12:05,840 --> 05:12:10,000 want to come up with a function so in my 6420 05:12:08,320 --> 05:12:14,040 case I'm going to go with 6421 05:12:10,000 --> 05:12:16,480 sync function let's call this fetch data 6422 05:12:14,040 --> 05:12:19,760 it's going to be looking for URL it will 6423 05:12:16,480 --> 05:12:22,040 be a string over here for now no returns 6424 05:12:19,760 --> 05:12:24,440 we just want to set up the TR catch 6425 05:12:22,040 --> 05:12:27,480 since we have a sync operation it's 6426 05:12:24,440 --> 05:12:30,040 always a good start now in here let's go 6427 05:12:27,480 --> 05:12:33,320 with response so this one will be equal 6428 05:12:30,040 --> 05:12:35,080 to a weight fetch we pass in the URL so 6429 05:12:33,320 --> 05:12:38,798 notice how we can right away start using 6430 05:12:35,080 --> 05:12:40,920 Fetch and actually we'll cover that in I 6431 05:12:38,798 --> 05:12:45,200 believe two three videos how come we 6432 05:12:40,920 --> 05:12:48,080 have access to this function without any 6433 05:12:45,200 --> 05:12:50,558 issues and typescript doesn't complain 6434 05:12:48,080 --> 05:12:55,000 and fetch has this interesting setup 6435 05:12:50,558 --> 05:12:59,000 where it doesn't treat for example 404 6436 05:12:55,000 --> 05:13:01,440 errors as errors it only treats the 6437 05:12:59,000 --> 05:13:03,558 network errors as errors basically if 6438 05:13:01,440 --> 05:13:06,440 you'll have 404 over here it's not going 6439 05:13:03,558 --> 05:13:10,520 to come up in the catch block so we will 6440 05:13:06,440 --> 05:13:12,440 do a manual check where else say if 6441 05:13:10,520 --> 05:13:16,680 response. 6442 05:13:12,440 --> 05:13:18,920 okay is not set well then I'll throw my 6443 05:13:16,680 --> 05:13:21,040 own error again this is just an extra 6444 05:13:18,920 --> 05:13:22,520 check we need to do with fetch for 6445 05:13:21,040 --> 05:13:26,638 example you don't need to do that with 6446 05:13:22,520 --> 05:13:30,920 axio so go row new error then let's set 6447 05:13:26,638 --> 05:13:34,958 up the template string we go with HTTP 6448 05:13:30,920 --> 05:13:37,480 error and the status here let's go with 6449 05:13:34,958 --> 05:13:39,798 status and we're going to set it equal 6450 05:13:37,480 --> 05:13:42,200 to a variable 6451 05:13:39,798 --> 05:13:45,320 let me access that we're going to go 6452 05:13:42,200 --> 05:13:49,120 response that status so now we're 6453 05:13:45,320 --> 05:13:51,798 checking even if we'll have 404 error it 6454 05:13:49,120 --> 05:13:54,160 will actually trigger the error and we 6455 05:13:51,798 --> 05:13:57,040 will return whatever we have in catch 6456 05:13:54,160 --> 05:13:59,638 block and now let's access the data 6457 05:13:57,040 --> 05:14:02,558 again we're looking for the array of 6458 05:13:59,638 --> 05:14:04,280 objects that's what we have here and you 6459 05:14:02,558 --> 05:14:07,280 can definitely see that if you navigate 6460 05:14:04,280 --> 05:14:10,400 to this URL in the browser 6461 05:14:07,280 --> 05:14:14,120 and we want to set up another 6462 05:14:10,400 --> 05:14:17,400 await let's go with the response and 6463 05:14:14,120 --> 05:14:19,958 Json let's invoke that and at the very 6464 05:14:17,400 --> 05:14:23,558 end of the tri block let's go with 6465 05:14:19,958 --> 05:14:26,760 return data now in the catch block let's 6466 05:14:23,558 --> 05:14:29,680 right away construct the message so I'm 6467 05:14:26,760 --> 05:14:33,240 going to go with error message and this 6468 05:14:29,680 --> 05:14:37,120 one will be equal to ter operator where 6469 05:14:33,240 --> 05:14:41,920 I'll check if the error is instance of 6470 05:14:37,120 --> 05:14:46,440 error well then I'll will set the error 6471 05:14:41,920 --> 05:14:48,160 message variable equal to error. message 6472 05:14:46,440 --> 05:14:50,798 since I know that the property is going 6473 05:14:48,160 --> 05:14:54,240 to be there if not then I'll just go 6474 05:14:50,798 --> 05:14:58,480 with something generic there was an 6475 05:14:54,240 --> 05:15:00,400 error so let's set this one up and then 6476 05:14:58,480 --> 05:15:02,520 let's log just so we can see what's 6477 05:15:00,400 --> 05:15:05,000 happening we're going to go with error 6478 05:15:02,520 --> 05:15:07,480 message here and then at the very end of 6479 05:15:05,000 --> 05:15:11,120 the catch block we want to return empty 6480 05:15:07,480 --> 05:15:12,798 array so if we're not able to get all of 6481 05:15:11,120 --> 05:15:17,080 the tours will return 6482 05:15:12,798 --> 05:15:20,080 MTR now I guess for now let's just log 6483 05:15:17,080 --> 05:15:23,760 what we have in the data and let's 6484 05:15:20,080 --> 05:15:26,878 invoke the fetch data fetch data let's 6485 05:15:23,760 --> 05:15:30,320 pass in the URL if everything is correct 6486 05:15:26,878 --> 05:15:33,920 in the console we should see this array 6487 05:15:30,320 --> 05:15:36,680 of objects and if I go over here and 6488 05:15:33,920 --> 05:15:39,000 mess with the URL since I have this 6489 05:15:36,680 --> 05:15:41,320 check over here now the response is not 6490 05:15:39,000 --> 05:15:43,958 going to be okay and I can clearly see 6491 05:15:41,320 --> 05:15:45,440 that over here so we're nicely throwing 6492 05:15:43,958 --> 05:15:50,040 the error as well and this is going to 6493 05:15:45,440 --> 05:15:53,600 be my error message so this works now 6494 05:15:50,040 --> 05:15:58,280 where is the issue well let's go over 6495 05:15:53,600 --> 05:15:59,798 here set the tours away to TS so now I'm 6496 05:15:58,280 --> 05:16:01,360 essentially assigning this to some kind 6497 05:15:59,798 --> 05:16:04,440 of variable and notice something 6498 05:16:01,360 --> 05:16:06,798 interesting what is the type well it's 6499 05:16:04,440 --> 05:16:09,760 any because typescript doesn't know what 6500 05:16:06,798 --> 05:16:12,280 I'm getting back so so yeah I was able 6501 05:16:09,760 --> 05:16:15,798 to fetch data I have access to it I have 6502 05:16:12,280 --> 05:16:18,040 no issues but this is equal to type any 6503 05:16:15,798 --> 05:16:21,480 now why is that such an issue well let's 6504 05:16:18,040 --> 05:16:24,160 try doing this let's go towards map we 6505 05:16:21,480 --> 05:16:27,680 know that we're iterating over correct 6506 05:16:24,160 --> 05:16:29,840 our array and we should have access to 6507 05:16:27,680 --> 05:16:32,638 each and every object over there but if 6508 05:16:29,840 --> 05:16:35,958 I type tour check it out typescript 6509 05:16:32,638 --> 05:16:39,520 immediately complains says okay I have 6510 05:16:35,958 --> 05:16:42,600 no info on this this is type any so how 6511 05:16:39,520 --> 05:16:45,120 am I supposed to know what is this tour 6512 05:16:42,600 --> 05:16:47,160 so we're not getting any suggestions 6513 05:16:45,120 --> 05:16:49,120 we're not getting Auto completions 6514 05:16:47,160 --> 05:16:52,840 anything like that so yeah I can set 6515 05:16:49,120 --> 05:16:58,240 this equal to tour and and 6516 05:16:52,840 --> 05:17:00,400 then go with tour. name but I mean this 6517 05:16:58,240 --> 05:17:02,920 is not going to be sustainable in the 6518 05:17:00,400 --> 05:17:06,160 long run so yeah I can nicely see 6519 05:17:02,920 --> 05:17:08,440 everything in browser's console but 6520 05:17:06,160 --> 05:17:12,160 since we're working with typescript we 6521 05:17:08,440 --> 05:17:15,638 will have to figure out how to provide 6522 05:17:12,160 --> 05:17:17,520 the shape of our data to typescript so 6523 05:17:15,638 --> 05:17:20,440 typescript knows that Hey listen this 6524 05:17:17,520 --> 05:17:23,080 tours is going to be an array of Tours 6525 05:17:20,440 --> 05:17:25,958 where I have following properties so I 6526 05:17:23,080 --> 05:17:27,360 have ID name info and rest of them and 6527 05:17:25,958 --> 05:17:30,440 this is something that we're going to do 6528 05:17:27,360 --> 05:17:32,760 in the next video as a challenge all 6529 05:17:30,440 --> 05:17:35,760 right and once we see the issue now 6530 05:17:32,760 --> 05:17:38,160 let's fix it how well we're working with 6531 05:17:35,760 --> 05:17:42,000 typescript so we need to provide the 6532 05:17:38,160 --> 05:17:45,638 shape and set up the correct return type 6533 05:17:42,000 --> 05:17:49,000 for our fetch data and once we do that 6534 05:17:45,638 --> 05:17:51,000 typescript is going to know what we have 6535 05:17:49,000 --> 05:17:53,360 there and then we'll have Auto 6536 05:17:51,000 --> 05:17:55,638 completion suggestions and all that cool 6537 05:17:53,360 --> 05:17:59,160 stuff so here's the 6538 05:17:55,638 --> 05:18:03,760 challenge set up to type a hint you can 6539 05:17:59,160 --> 05:18:07,000 use the URL as your reference and also 6540 05:18:03,760 --> 05:18:11,320 set up a proper return type for the 6541 05:18:07,000 --> 05:18:14,680 fetch data again hint remember that it's 6542 05:18:11,320 --> 05:18:16,958 a promise correct because it's a sync so 6543 05:18:14,680 --> 05:18:19,440 first let me navigate to the browser and 6544 05:18:16,958 --> 05:18:22,040 I can see that essentially each and 6545 05:18:19,440 --> 05:18:25,520 every object is going to have ID name 6546 05:18:22,040 --> 05:18:27,760 info image and price and guess what it's 6547 05:18:25,520 --> 05:18:30,080 pretty straightforward all of them are 6548 05:18:27,760 --> 05:18:31,120 strings so now I just want to navigate 6549 05:18:30,080 --> 05:18:34,040 back to 6550 05:18:31,120 --> 05:18:36,840 tutorial somewhere over here set up my 6551 05:18:34,040 --> 05:18:38,240 type I'm going to go with type and name 6552 05:18:36,840 --> 05:18:39,718 is up to you in my case I'm going to go 6553 05:18:38,240 --> 05:18:42,520 with tool 6554 05:18:39,718 --> 05:18:45,600 ID is going to be equal to 6555 05:18:42,520 --> 05:18:47,878 string then I have the same for name 6556 05:18:45,600 --> 05:18:49,958 info image and price so you know what 6557 05:18:47,878 --> 05:18:52,600 let me just copy and paste hopefully 6558 05:18:49,958 --> 05:18:57,798 it's going to be faster in the long run 6559 05:18:52,600 --> 05:19:00,558 so we have info here then we have image 6560 05:18:57,798 --> 05:19:04,558 and at the end we also have the price 6561 05:19:00,558 --> 05:19:06,958 let's save this and there's two places 6562 05:19:04,558 --> 05:19:08,920 where we want to set up this type so 6563 05:19:06,958 --> 05:19:11,360 first we want to set it here where we 6564 05:19:08,920 --> 05:19:14,000 have the return and I'm also going to 6565 05:19:11,360 --> 05:19:16,320 set it over here where I have the data 6566 05:19:14,000 --> 05:19:20,440 so I'm going to say that I'm expecting 6567 05:19:16,320 --> 05:19:23,280 from this. Json back my array and it's 6568 05:19:20,440 --> 05:19:26,080 going to be an array of Tours and the 6569 05:19:23,280 --> 05:19:28,520 same over here for my fetch data I'm 6570 05:19:26,080 --> 05:19:30,878 going to go with my return and remember 6571 05:19:28,520 --> 05:19:34,520 this is a sync correct so what do we 6572 05:19:30,878 --> 05:19:37,280 need to pass in a promise promis is what 6573 05:19:34,520 --> 05:19:41,520 it's a generic so we go over here with 6574 05:19:37,280 --> 05:19:44,920 tour and then we pass here the array as 6575 05:19:41,520 --> 05:19:46,400 well now don't worry if it's empty it's 6576 05:19:44,920 --> 05:19:50,600 still technically going to match 6577 05:19:46,400 --> 05:19:52,840 whatever we have in our return and now 6578 05:19:50,600 --> 05:19:55,240 we don't want to set this as any of 6579 05:19:52,840 --> 05:19:58,280 course we can actually go back and we'll 6580 05:19:55,240 --> 05:20:00,480 nicely see that this is a tour why is 6581 05:19:58,280 --> 05:20:03,080 that super beneficial because check it 6582 05:20:00,480 --> 05:20:07,160 out if I'm going to go here with log and 6583 05:20:03,080 --> 05:20:09,480 tour dot notice I have all of these 6584 05:20:07,160 --> 05:20:11,798 properties right away avail ailable I 6585 05:20:09,480 --> 05:20:13,558 can go with name save it again the 6586 05:20:11,798 --> 05:20:17,200 result in the browser is not going to 6587 05:20:13,558 --> 05:20:20,440 change but essentially this is just a 6588 05:20:17,200 --> 05:20:24,320 type Safe Way how we can fetch data 6589 05:20:20,440 --> 05:20:27,920 where we come up with the type we set up 6590 05:20:24,320 --> 05:20:31,000 proper return and after that can start 6591 05:20:27,920 --> 05:20:34,558 using this data in a type Safe Way in 6592 05:20:31,000 --> 05:20:36,520 our project all right and the moment we 6593 05:20:34,558 --> 05:20:38,958 feel good about ourselves that we have 6594 05:20:36,520 --> 05:20:41,160 nicely provided the type and Everything 6595 05:20:38,958 --> 05:20:46,240 is Awesome we can start using in our 6596 05:20:41,160 --> 05:20:48,480 project let me throw you a m grenade so 6597 05:20:46,240 --> 05:20:51,040 what do you think is going to happen if 6598 05:20:48,480 --> 05:20:54,240 I'm going to remove some Properties or 6599 05:20:51,040 --> 05:20:56,440 I'm going to add some extra ones so 6600 05:20:54,240 --> 05:20:58,360 let's say if I'm going to go with 6601 05:20:56,440 --> 05:21:01,320 something and then I'm going to set it 6602 05:20:58,360 --> 05:21:03,400 equal to a bullion what do you think is 6603 05:21:01,320 --> 05:21:05,360 going to happen you think I'll have 6604 05:21:03,400 --> 05:21:07,360 access to it you think typescript is 6605 05:21:05,360 --> 05:21:08,920 going to complain and I guess you can 6606 05:21:07,360 --> 05:21:11,600 clearly see that typescript is is not 6607 05:21:08,920 --> 05:21:15,638 complaining and actually if I go over 6608 05:21:11,600 --> 05:21:18,798 here technically I can even select it 6609 05:21:15,638 --> 05:21:21,480 but what is the result I'm getting back 6610 05:21:18,798 --> 05:21:24,798 undefined so here's the 6611 05:21:21,480 --> 05:21:28,920 kicker yes this is definitely something 6612 05:21:24,798 --> 05:21:32,080 we want to do we do want to provide a 6613 05:21:28,920 --> 05:21:35,240 correct shape to the typescript but 6614 05:21:32,080 --> 05:21:36,320 there's no way for typescript to check 6615 05:21:35,240 --> 05:21:39,600 this at 6616 05:21:36,320 --> 05:21:43,320 runtime so yes this is super useful for 6617 05:21:39,600 --> 05:21:44,920 us to later use in our code you saw 6618 05:21:43,320 --> 05:21:47,000 previously what happens if we don't do 6619 05:21:44,920 --> 05:21:50,040 that then basically typescript thinks 6620 05:21:47,000 --> 05:21:53,000 it's any but also just because we set up 6621 05:21:50,040 --> 05:21:57,798 this type over here at build time it 6622 05:21:53,000 --> 05:22:00,480 doesn't 100% guarantee that at run time 6623 05:21:57,798 --> 05:22:02,558 we'll get the correct results because 6624 05:22:00,480 --> 05:22:04,680 this is just build time in here we're 6625 05:22:02,558 --> 05:22:06,798 saying yeah this is going to be my shape 6626 05:22:04,680 --> 05:22:08,160 so I can type here whatever I want and 6627 05:22:06,798 --> 05:22:09,040 typescript is going to be like Hey 6628 05:22:08,160 --> 05:22:11,878 listen 6629 05:22:09,040 --> 05:22:15,280 you told me that this promise is going 6630 05:22:11,878 --> 05:22:18,798 to return the array of Tours and this is 6631 05:22:15,280 --> 05:22:20,840 going to be the shape for the tour but 6632 05:22:18,798 --> 05:22:22,798 what if the something is actually not 6633 05:22:20,840 --> 05:22:25,240 there I mean there's no way for 6634 05:22:22,798 --> 05:22:27,920 typescript to check that and therefore 6635 05:22:25,240 --> 05:22:30,878 in the following video we'll take a look 6636 05:22:27,920 --> 05:22:34,280 how we can do the runtime checks with 6637 05:22:30,878 --> 05:22:37,520 the help of extra Library by the name of 6638 05:22:34,280 --> 05:22:38,958 Zod okay and up next let's see how we 6639 05:22:37,520 --> 05:22:42,000 can validate 6640 05:22:38,958 --> 05:22:45,000 data at runtime first off let me just 6641 05:22:42,000 --> 05:22:47,798 mention that technically you can do it 6642 05:22:45,000 --> 05:22:50,760 manually so in this case yes we can 6643 05:22:47,798 --> 05:22:54,080 iterate over data check if the specific 6644 05:22:50,760 --> 05:22:56,878 property exists as well as has the 6645 05:22:54,080 --> 05:22:59,920 proper type and even though it sounds 6646 05:22:56,878 --> 05:23:03,840 super cute in reality it can get messy 6647 05:22:59,920 --> 05:23:07,200 pretty fast so instead if you ever need 6648 05:23:03,840 --> 05:23:09,840 to validate data in such a way Zod 6649 05:23:07,200 --> 05:23:11,680 library is a very valid Choice simply 6650 05:23:09,840 --> 05:23:14,840 because it provides bunch of options out 6651 05:23:11,680 --> 05:23:18,840 of the box and it's far better battle 6652 05:23:14,840 --> 05:23:21,920 tested than our own validation code now 6653 05:23:18,840 --> 05:23:25,240 you can use Zod in other scenarios as 6654 05:23:21,920 --> 05:23:28,440 well for example in my nextjs course we 6655 05:23:25,240 --> 05:23:32,320 use Zod to validate form submissions on 6656 05:23:28,440 --> 05:23:35,280 the client as well as server actions on 6657 05:23:32,320 --> 05:23:40,160 the server if you interested in learning 6658 05:23:35,280 --> 05:23:44,360 more about Zod just visit Zod Dev or you 6659 05:23:40,160 --> 05:23:46,520 can follow the link I left in the read 6660 05:23:44,360 --> 05:23:49,520 me first thing we'll need to do is 6661 05:23:46,520 --> 05:23:53,558 install the library so I'm going to open 6662 05:23:49,520 --> 05:23:57,120 up the terminal now I'm going to open a 6663 05:23:53,558 --> 05:23:58,080 new window technically you don't have to 6664 05:23:57,120 --> 05:24:01,520 just make 6665 05:23:58,080 --> 05:24:04,000 sure if you stop the server to start it 6666 05:24:01,520 --> 05:24:06,760 up again otherwise stuff is not going to 6667 05:24:04,000 --> 05:24:08,878 work so in this window I simply want to 6668 05:24:06,760 --> 05:24:12,040 run npm install 6669 05:24:08,878 --> 05:24:13,840 and S so now we install this library and 6670 05:24:12,040 --> 05:24:16,840 as a side note in few videos we're going 6671 05:24:13,840 --> 05:24:17,958 to talk about declaration files so 6672 05:24:16,840 --> 05:24:20,040 there's another reason why I'm 6673 05:24:17,958 --> 05:24:21,638 installing this Library so we install 6674 05:24:20,040 --> 05:24:23,958 Library okay everything's beautiful I 6675 05:24:21,638 --> 05:24:27,798 can close this window there's really no 6676 05:24:23,958 --> 05:24:30,798 need then we want to import Z from Zod 6677 05:24:27,798 --> 05:24:33,798 and essentially it's a named import and 6678 05:24:30,798 --> 05:24:35,920 then we want to construct our schema so 6679 05:24:33,798 --> 05:24:38,600 let's set this one up and you know what 6680 05:24:35,920 --> 05:24:40,480 I can just type it scratch I guess so 6681 05:24:38,600 --> 05:24:44,400 let me go over here let me go with 6682 05:24:40,480 --> 05:24:46,718 import then Z from and I'm looking for 6683 05:24:44,400 --> 05:24:48,718 my Zod Library again if we have 6684 05:24:46,718 --> 05:24:50,798 successfully installed should be 6685 05:24:48,718 --> 05:24:55,200 somewhere over here notice I have 6686 05:24:50,798 --> 05:24:58,600 dependency and there I have the Zod one 6687 05:24:55,200 --> 05:25:01,240 so we import the e z and then we want to 6688 05:24:58,600 --> 05:25:03,360 construct our schema so this is very 6689 05:25:01,240 --> 05:25:08,400 similar to what we have over here this 6690 05:25:03,360 --> 05:25:11,798 is going to be shape of our data but 6691 05:25:08,400 --> 05:25:14,240 difference well we'll use Zod methods 6692 05:25:11,798 --> 05:25:17,160 and this is going to be checked at run 6693 05:25:14,240 --> 05:25:20,920 time so I'm going to come up with t 6694 05:25:17,160 --> 05:25:23,120 schema and as a side note eventually 6695 05:25:20,920 --> 05:25:25,558 we'll set this equal to a different 6696 05:25:23,120 --> 05:25:29,400 value but not for now so in here we want 6697 05:25:25,558 --> 05:25:31,200 to go with z and then the object is the 6698 05:25:29,400 --> 05:25:33,320 method we're looking for so I'm pretty 6699 05:25:31,200 --> 05:25:35,718 much saying that I'm constructing an 6700 05:25:33,320 --> 05:25:38,480 object and here yes we pretty much want 6701 05:25:35,718 --> 05:25:41,160 to provide the same properties and and 6702 05:25:38,480 --> 05:25:43,958 as a side note I think I can remove this 6703 05:25:41,160 --> 05:25:45,840 something here and this one here as well 6704 05:25:43,958 --> 05:25:49,440 for now we'll figure out what we're 6705 05:25:45,840 --> 05:25:53,280 going to do there in a second and yes we 6706 05:25:49,440 --> 05:25:55,160 want to set up the validation for the 6707 05:25:53,280 --> 05:25:58,320 same property so this is not going to 6708 05:25:55,160 --> 05:26:00,520 change however in this case Zod provides 6709 05:25:58,320 --> 05:26:02,480 bunch of methods now again I'm not going 6710 05:26:00,520 --> 05:26:04,240 to go through each and every method in 6711 05:26:02,480 --> 05:26:05,958 this case we simply want to check 6712 05:26:04,240 --> 05:26:08,718 whether this is going to be a string and 6713 05:26:05,958 --> 05:26:12,680 this is super easy I can go with ID and 6714 05:26:08,718 --> 05:26:14,520 then Z Dot and then string so this 6715 05:26:12,680 --> 05:26:16,600 method essentially is going to check is 6716 05:26:14,520 --> 05:26:19,240 it a string or not and if it's not a 6717 05:26:16,600 --> 05:26:21,600 string if it's not present then 6718 05:26:19,240 --> 05:26:23,840 basically it's going to yell so now 6719 05:26:21,600 --> 05:26:26,798 let's copy and paste and we simply want 6720 05:26:23,840 --> 05:26:29,558 to provide different values so we're 6721 05:26:26,798 --> 05:26:30,760 looking here for name then we want to 6722 05:26:29,558 --> 05:26:35,600 get the 6723 05:26:30,760 --> 05:26:41,120 info we also want to set up an 6724 05:26:35,600 --> 05:26:46,638 image over here so 6725 05:26:41,120 --> 05:26:49,000 image um lastly we also have the price 6726 05:26:46,638 --> 05:26:51,440 so all of them are strings but the comms 6727 05:26:49,000 --> 05:26:54,360 are missing over here that's why I have 6728 05:26:51,440 --> 05:26:57,878 the errors so let me set this one up and 6729 05:26:54,360 --> 05:27:00,638 then as far as the tour we can actually 6730 05:26:57,878 --> 05:27:02,680 infer the type so I'm not going to leave 6731 05:27:00,638 --> 05:27:04,798 this for your reference since you have 6732 05:27:02,680 --> 05:27:08,480 access to it in a read me but 6733 05:27:04,798 --> 05:27:11,680 essentially I can go with type door and 6734 05:27:08,480 --> 05:27:15,160 instead of hard coding Zar has this 6735 05:27:11,680 --> 05:27:17,320 method of infer and it is actually a 6736 05:27:15,160 --> 05:27:19,320 generic and we should be familiar with 6737 05:27:17,320 --> 05:27:21,160 this one so notice the syntax 6738 05:27:19,320 --> 05:27:23,840 effectively we just go over here with 6739 05:27:21,160 --> 05:27:26,080 these angle brackets and then inside of 6740 05:27:23,840 --> 05:27:30,480 the angle brackets as far as the type we 6741 05:27:26,080 --> 05:27:33,440 go type off and then tour schema so now 6742 05:27:30,480 --> 05:27:35,958 we infer this T type and notice pretty 6743 05:27:33,440 --> 05:27:37,600 much the result is the same but now 6744 05:27:35,958 --> 05:27:40,600 we're nicely getting this from Tor 6745 05:27:37,600 --> 05:27:42,400 schema and like I said you have more 6746 05:27:40,600 --> 05:27:45,120 options for 6747 05:27:42,400 --> 05:27:47,638 example ID has to be a string but I can 6748 05:27:45,120 --> 05:27:49,680 also add min max and all kinds of 6749 05:27:47,638 --> 05:27:52,200 methods again this is just scratching 6750 05:27:49,680 --> 05:27:54,600 the surface and notice how we don't need 6751 05:27:52,200 --> 05:27:58,200 to change anything here in return we're 6752 05:27:54,600 --> 05:28:01,840 also here in a data but we do want to 6753 05:27:58,200 --> 05:28:04,480 right now add some checks at runtime and 6754 05:28:01,840 --> 05:28:09,000 in order to do that I'm going to rename 6755 05:28:04,480 --> 05:28:10,878 this to Raw data so this this is not 6756 05:28:09,000 --> 05:28:14,440 something we're going to be 6757 05:28:10,878 --> 05:28:18,558 passing and instead we want to go with 6758 05:28:14,440 --> 05:28:22,400 const result is equal to then we go with 6759 05:28:18,558 --> 05:28:25,200 our schema it has an awesome method of 6760 05:28:22,400 --> 05:28:27,480 array which we can invoke we're not 6761 05:28:25,200 --> 05:28:30,440 going to pass anything in and we also 6762 05:28:27,480 --> 05:28:32,840 have this safe pars so essentially safe 6763 05:28:30,440 --> 05:28:34,958 pars is not going to trigger 6764 05:28:32,840 --> 05:28:37,400 automatically the catch block we'll do 6765 05:28:34,958 --> 05:28:40,600 it ourselves so we're going to go save 6766 05:28:37,400 --> 05:28:42,718 Parts with we pass in our raw data so 6767 05:28:40,600 --> 05:28:46,600 now we're going to check this at runtime 6768 05:28:42,718 --> 05:28:49,000 actually not only at build time and then 6769 05:28:46,600 --> 05:28:52,920 since we have a safe part since this is 6770 05:28:49,000 --> 05:28:55,240 not going to trigger immediate error we 6771 05:28:52,920 --> 05:28:59,480 want to check for the success property 6772 05:28:55,240 --> 05:29:01,878 in the result so if essentially we don't 6773 05:28:59,480 --> 05:29:05,878 have the success property over there 6774 05:29:01,878 --> 05:29:08,798 then we'll go throw new 6775 05:29:05,878 --> 05:29:12,840 error and let's again and come up with 6776 05:29:08,798 --> 05:29:17,520 some message let's go with invalid data 6777 05:29:12,840 --> 05:29:21,400 for example let's access the result Dot 6778 05:29:17,520 --> 05:29:23,480 and we're looking for the error so if 6779 05:29:21,400 --> 05:29:26,080 we're not able to parse then we'll throw 6780 05:29:23,480 --> 05:29:29,200 the error manually and now where we have 6781 05:29:26,080 --> 05:29:31,000 the return instead of data which we 6782 05:29:29,200 --> 05:29:34,200 don't have anymore we're going to go 6783 05:29:31,000 --> 05:29:36,120 with result. data so that's where our 6784 05:29:34,200 --> 05:29:38,360 data is going to be located and again if 6785 05:29:36,120 --> 05:29:40,760 we're successful this doesn't change 6786 05:29:38,360 --> 05:29:43,160 still getting back that array but 6787 05:29:40,760 --> 05:29:45,600 difference will actually check 6788 05:29:43,160 --> 05:29:48,280 everything at runtime so now let me 6789 05:29:45,600 --> 05:29:50,760 navigate to the browser I don't believe 6790 05:29:48,280 --> 05:29:52,360 I'm doing anything so you know what let 6791 05:29:50,760 --> 05:29:54,440 me just log the results just so you can 6792 05:29:52,360 --> 05:29:57,160 see what we're getting back let's go 6793 05:29:54,440 --> 05:29:59,718 with the result notice we're successful 6794 05:29:57,160 --> 05:30:02,320 so success is true so we bypass that 6795 05:29:59,718 --> 05:30:04,878 error one and then we get back the data 6796 05:30:02,320 --> 05:30:08,000 over here so now I can navigate to my 6797 05:30:04,878 --> 05:30:10,958 map one and again I have access to the 6798 05:30:08,000 --> 05:30:14,480 door so let's log something let's go 6799 05:30:10,958 --> 05:30:17,000 with door Dot and again let's access the 6800 05:30:14,480 --> 05:30:18,798 name so this works now here's the 6801 05:30:17,000 --> 05:30:22,160 biggest difference if I'm going to go 6802 05:30:18,798 --> 05:30:24,718 over here and let's say add a property 6803 05:30:22,160 --> 05:30:27,080 which I'm expecting but it's not there 6804 05:30:24,718 --> 05:30:29,320 will actually get the error so if I'm 6805 05:30:27,080 --> 05:30:32,320 going to go with something and set it 6806 05:30:29,320 --> 05:30:35,440 equal to whatever number it doesn't 6807 05:30:32,320 --> 05:30:38,200 really matter if I go over here notice 6808 05:30:35,440 --> 05:30:41,798 how immediately this is going to trigger 6809 05:30:38,200 --> 05:30:45,000 the error so success is equal to 6810 05:30:41,798 --> 05:30:48,120 false and now I have this error message 6811 05:30:45,000 --> 05:30:50,638 in the console and again the cool thing 6812 05:30:48,120 --> 05:30:53,958 is that we're checking this at runtime 6813 05:30:50,638 --> 05:30:56,600 not just at build time okay and I think 6814 05:30:53,958 --> 05:30:59,718 at this point we're ready to talk about 6815 05:30:56,600 --> 05:31:02,600 typescript declaration files and why 6816 05:30:59,718 --> 05:31:07,320 they're so important and let's start 6817 05:31:02,600 --> 05:31:08,840 with a small recap if I were to go to 6818 05:31:07,320 --> 05:31:10,360 tutorial 6819 05:31:08,840 --> 05:31:15,440 and you know what no let me create a 6820 05:31:10,360 --> 05:31:18,520 file first so I want to create types DS 6821 05:31:15,440 --> 05:31:22,680 on here I'll write away export and I'm 6822 05:31:18,520 --> 05:31:25,798 going to go with export type and 6823 05:31:22,680 --> 05:31:30,878 random and here let's just go with name 6824 05:31:25,798 --> 05:31:34,600 and string if I were to import this so 6825 05:31:30,878 --> 05:31:36,920 import I mean with the type keyword or 6826 05:31:34,600 --> 05:31:39,240 without everything is going to work 6827 05:31:36,920 --> 05:31:41,920 correct pick because typescript knows 6828 05:31:39,240 --> 05:31:44,080 everything about this type random so I 6829 05:31:41,920 --> 05:31:47,320 can start right away using it in our 6830 05:31:44,080 --> 05:31:50,638 project so far so good but can you 6831 05:31:47,320 --> 05:31:54,200 explain to me this how come I right away 6832 05:31:50,638 --> 05:31:57,160 have access to the document object so if 6833 05:31:54,200 --> 05:31:59,200 I type over here press dot have access 6834 05:31:57,160 --> 05:32:00,718 to all of the methods and all of the 6835 05:31:59,200 --> 05:32:04,000 properties and of course I'm not going 6836 05:32:00,718 --> 05:32:05,718 to double check that so I'm not going to 6837 05:32:04,000 --> 05:32:10,320 prove that it's definitely a document 6838 05:32:05,718 --> 05:32:11,798 object but trust me the same one and 6839 05:32:10,320 --> 05:32:13,798 again good luck trying to build 6840 05:32:11,798 --> 05:32:16,400 something without this document object 6841 05:32:13,798 --> 05:32:18,440 so how come we have access to it because 6842 05:32:16,400 --> 05:32:21,200 I can guarantee you that I didn't set it 6843 05:32:18,440 --> 05:32:24,520 up anywhere it's not like I went to the 6844 05:32:21,200 --> 05:32:27,520 types and then started constructing the 6845 05:32:24,520 --> 05:32:31,520 document type I mean that would be just 6846 05:32:27,520 --> 05:32:34,760 a work for I'm not sure half a year so 6847 05:32:31,520 --> 05:32:37,558 what's happening over here well you see 6848 05:32:34,760 --> 05:32:40,480 when we work with typescript there's 6849 05:32:37,558 --> 05:32:42,160 something called declaration files and 6850 05:32:40,480 --> 05:32:43,920 the same is going to work for libraries 6851 05:32:42,160 --> 05:32:47,160 something we're going to cover in a 6852 05:32:43,920 --> 05:32:51,000 second but when it comes to typescript 6853 05:32:47,160 --> 05:32:52,840 they get included and then 6854 05:32:51,000 --> 05:32:56,200 essentially as we're working with 6855 05:32:52,840 --> 05:32:59,120 typescript it already knows everything 6856 05:32:56,200 --> 05:33:02,760 about the document so if we go here to a 6857 05:32:59,120 --> 05:33:04,958 type definition there is a definition 6858 05:33:02,760 --> 05:33:06,080 for this document like I said it's going 6859 05:33:04,958 --> 05:33:08,680 to be 6860 05:33:06,080 --> 05:33:11,360 giant but good luck 6861 05:33:08,680 --> 05:33:14,200 working without the document so in 6862 05:33:11,360 --> 05:33:16,920 here there's an info about every 6863 05:33:14,200 --> 05:33:19,480 property every method what it's doing 6864 05:33:16,920 --> 05:33:23,080 what um parameters the function is 6865 05:33:19,480 --> 05:33:25,040 expecting and that sort of thing so in 6866 05:33:23,080 --> 05:33:26,400 here technically there is no 6867 05:33:25,040 --> 05:33:28,718 functionality that's going to be running 6868 05:33:26,400 --> 05:33:29,680 in this file but there's going to be 6869 05:33:28,718 --> 05:33:32,280 type 6870 05:33:29,680 --> 05:33:34,600 definitions since when we work with 6871 05:33:32,280 --> 05:33:39,200 typescript typescript needs to know 6872 05:33:34,600 --> 05:33:41,638 everything about the shape of our data 6873 05:33:39,200 --> 05:33:44,440 otherwise as you saw with the straight 6874 05:33:41,638 --> 05:33:47,798 up JavaScript file it's just going to 6875 05:33:44,440 --> 05:33:48,638 complain so I guess first I'll show you 6876 05:33:47,798 --> 05:33:51,558 where 6877 05:33:48,638 --> 05:33:53,400 the Declaration files are located and 6878 05:33:51,558 --> 05:33:56,280 then I'll show you how we can add them 6879 05:33:53,400 --> 05:33:58,400 or remove them from the project now as a 6880 05:33:56,280 --> 05:34:00,600 side note since we're using the 6881 05:33:58,400 --> 05:34:03,840 boilerplate V 6882 05:34:00,600 --> 05:34:05,520 application it's already done for us but 6883 05:34:03,840 --> 05:34:07,400 yes if you're starting everything from 6884 05:34:05,520 --> 05:34:10,080 scratch you can add those things 6885 05:34:07,400 --> 05:34:12,920 manually ually Stars if we navigate to a 6886 05:34:10,080 --> 05:34:16,280 node modules and typescript which is a 6887 05:34:12,920 --> 05:34:18,080 package we're using under the lib notice 6888 05:34:16,280 --> 05:34:20,958 this long list well these are 6889 05:34:18,080 --> 05:34:23,840 declaration files again once you 6890 05:34:20,958 --> 05:34:25,878 navigate over here let me just find 6891 05:34:23,840 --> 05:34:28,520 something uh maybe this one over here 6892 05:34:25,878 --> 05:34:30,600 right notice you have these type 6893 05:34:28,520 --> 05:34:31,878 definitions it's not like in here you 6894 05:34:30,600 --> 05:34:34,400 have some kind of function that's 6895 05:34:31,878 --> 05:34:37,840 running no but you have clear 6896 05:34:34,400 --> 05:34:39,878 description of type definitions so so 6897 05:34:37,840 --> 05:34:42,040 again if you want to start using 6898 05:34:39,878 --> 05:34:46,000 something from the 6899 05:34:42,040 --> 05:34:48,558 Dom you can only do it if there's a type 6900 05:34:46,000 --> 05:34:52,120 definition for that something in one of 6901 05:34:48,558 --> 05:34:53,798 the files again you'll see in a second 6902 05:34:52,120 --> 05:34:56,160 if let's say we don't have access to 6903 05:34:53,798 --> 05:35:00,240 these files typescript is not going to 6904 05:34:56,160 --> 05:35:02,958 know what is a document or any other web 6905 05:35:00,240 --> 05:35:05,520 apis and the way we control that let me 6906 05:35:02,958 --> 05:35:09,080 just close the node modules again it's 6907 05:35:05,520 --> 05:35:13,280 already set up for us but you can do it 6908 05:35:09,080 --> 05:35:16,360 over here in this lib notice this Dum 6909 05:35:13,280 --> 05:35:19,520 essentially in the lib you specify the 6910 05:35:16,360 --> 05:35:20,558 set of bundle Library declaration files 6911 05:35:19,520 --> 05:35:24,440 that 6912 05:35:20,558 --> 05:35:27,480 describe the target runtime environment 6913 05:35:24,440 --> 05:35:30,440 and notice over here this D well if we 6914 05:35:27,480 --> 05:35:32,040 remove it notice something interesting 6915 05:35:30,440 --> 05:35:34,200 immediately typescript is going to 6916 05:35:32,040 --> 05:35:36,760 complain immediately typescript is going 6917 05:35:34,200 --> 05:35:38,400 to say Hey listen what document I don't 6918 05:35:36,760 --> 05:35:40,680 know what you're talking about 6919 05:35:38,400 --> 05:35:44,160 so essentially we can either set it up 6920 05:35:40,680 --> 05:35:45,638 ourselves or we need to have those type 6921 05:35:44,160 --> 05:35:48,558 definitions without those type 6922 05:35:45,638 --> 05:35:50,680 definitions we won't be able to 6923 05:35:48,558 --> 05:35:54,120 essentially successfully build our 6924 05:35:50,680 --> 05:35:57,680 project so let me just close this one 6925 05:35:54,120 --> 05:36:00,600 and navigate back to TS config let me 6926 05:35:57,680 --> 05:36:03,360 add the Dom since I do want to use it 6927 05:36:00,600 --> 05:36:06,680 since we're building for the web and 6928 05:36:03,360 --> 05:36:09,200 this leads me to the second Point yes 6929 05:36:06,680 --> 05:36:12,958 the same works with any Library you want 6930 05:36:09,200 --> 05:36:14,000 to use in our project remember how in 6931 05:36:12,958 --> 05:36:19,120 previous 6932 05:36:14,000 --> 05:36:22,280 video we were able to import Zod from 6933 05:36:19,120 --> 05:36:24,718 the Zod library and we were good to go 6934 05:36:22,280 --> 05:36:27,120 so notice how again typescript is not 6935 05:36:24,718 --> 05:36:29,798 complaining and in fact typescript knows 6936 05:36:27,120 --> 05:36:31,920 everything that there is to know about 6937 05:36:29,798 --> 05:36:33,840 the import it knows the types it 6938 05:36:31,920 --> 05:36:38,160 contains it knows the methods properties 6939 05:36:33,840 --> 05:36:40,440 and all that how well let's check it out 6940 05:36:38,160 --> 05:36:43,320 again we're navigating to node modules I 6941 05:36:40,440 --> 05:36:47,000 want to close this one we are looking 6942 05:36:43,320 --> 05:36:51,718 for a Zod in this case and under the lib 6943 05:36:47,000 --> 05:36:54,480 guess what again we have the type 6944 05:36:51,718 --> 05:36:57,920 declaration files and in there we have 6945 05:36:54,480 --> 05:36:59,558 type definition and therefore once we 6946 05:36:57,920 --> 05:37:02,480 start using the 6947 05:36:59,558 --> 05:37:05,000 library typescript has no issues it 6948 05:37:02,480 --> 05:37:07,360 right away knows what the method is 6949 05:37:05,000 --> 05:37:09,440 expecting what is the interface and all 6950 05:37:07,360 --> 05:37:12,280 that that which leads me to another 6951 05:37:09,440 --> 05:37:14,718 Point yes if you're going to install 6952 05:37:12,280 --> 05:37:16,840 Library which doesn't have the 6953 05:37:14,718 --> 05:37:19,558 Declaration files basically there are no 6954 05:37:16,840 --> 05:37:21,878 type definitions you won't be able to 6955 05:37:19,558 --> 05:37:24,440 use it now the good news that there is a 6956 05:37:21,878 --> 05:37:27,080 repo which essentially 6957 05:37:24,440 --> 05:37:29,520 contains the types for a bunch of 6958 05:37:27,080 --> 05:37:32,440 libraries but first let me show you what 6959 05:37:29,520 --> 05:37:34,520 happens if we install such Library uh 6960 05:37:32,440 --> 05:37:36,440 what is the result and again this is 6961 05:37:34,520 --> 05:37:39,440 just random example I'm going to use 6962 05:37:36,440 --> 05:37:42,080 bcrypt j s which is a password hashing 6963 05:37:39,440 --> 05:37:44,040 library and technically this is optional 6964 05:37:42,080 --> 05:37:46,440 I mean you don't have to follow along I 6965 05:37:44,040 --> 05:37:50,440 just want to showcase something so let 6966 05:37:46,440 --> 05:37:54,400 me go to a separate terminal window let 6967 05:37:50,440 --> 05:37:56,958 me install it okay I installed the 6968 05:37:54,400 --> 05:38:00,280 package clearly see that it's in my 6969 05:37:56,958 --> 05:38:04,120 dependencies okay good let me remove all 6970 05:38:00,280 --> 05:38:08,520 of this I'm going to go with import 6971 05:38:04,120 --> 05:38:12,558 bcrypt Js from and definitely have the 6972 05:38:08,520 --> 05:38:14,798 library but check it out so essentially 6973 05:38:12,558 --> 05:38:18,520 typescript is complaining there is no 6974 05:38:14,798 --> 05:38:21,120 declaration file so same issue like we 6975 05:38:18,520 --> 05:38:23,320 had with the straightup Javascript file 6976 05:38:21,120 --> 05:38:26,600 and like I mentioned before the solution 6977 05:38:23,320 --> 05:38:29,280 is following there is a repo which 6978 05:38:26,600 --> 05:38:31,280 contains bunch of types and I left the 6979 05:38:29,280 --> 05:38:34,638 link in read me so let me try to 6980 05:38:31,280 --> 05:38:37,920 navigate over here and in this repo 6981 05:38:34,638 --> 05:38:40,200 under the types you'll be able to most 6982 05:38:37,920 --> 05:38:43,920 likely find the library you just 6983 05:38:40,200 --> 05:38:48,320 installed because it contains like What 6984 05:38:43,920 --> 05:38:51,878 7,802 entries so essentially you have 6985 05:38:48,320 --> 05:38:53,638 typescript types for all of these 6986 05:38:51,878 --> 05:38:56,240 libraries and the one that we're looking 6987 05:38:53,638 --> 05:38:59,080 for in this case is bcrypt JS otherwise 6988 05:38:56,240 --> 05:39:01,478 we cannot use it in a project and the 6989 05:38:59,080 --> 05:39:03,920 way we install the types is following so 6990 05:39:01,478 --> 05:39:08,080 we go with npm install we're going to go 6991 05:39:03,920 --> 05:39:12,280 with save dev then at type 6992 05:39:08,080 --> 05:39:15,320 and then bcrypt JS so our specific 6993 05:39:12,280 --> 05:39:18,878 Library let me grab this line of 6994 05:39:15,320 --> 05:39:21,558 code and the moment we do that 6995 05:39:18,878 --> 05:39:24,760 typescript is going to know everything 6996 05:39:21,558 --> 05:39:26,878 that there is to know about this bpjs so 6997 05:39:24,760 --> 05:39:30,558 if I'm going to go over here type dot 6998 05:39:26,878 --> 05:39:34,120 notice I have access to all of the 6999 05:39:30,558 --> 05:39:36,440 methods the import provides hopefully 7000 05:39:34,120 --> 05:39:39,080 it's clear why typescript declaration 7001 05:39:36,440 --> 05:39:41,520 files are so important when we're 7002 05:39:39,080 --> 05:39:43,558 building the typescript project and with 7003 05:39:41,520 --> 05:39:47,040 this in place we can move on to the next 7004 05:39:43,558 --> 05:39:50,638 topic okay and up next let's cover 7005 05:39:47,040 --> 05:39:54,120 typescript config file where as the name 7006 05:39:50,638 --> 05:39:56,680 already suggests we can configure our 7007 05:39:54,120 --> 05:39:58,478 typescript setup now if I'm being 7008 05:39:56,680 --> 05:40:01,360 totally honest I actually find this 7009 05:39:58,478 --> 05:40:03,320 topic super boring and therefore I'm 7010 05:40:01,360 --> 05:40:05,798 planning to cover only the properties 7011 05:40:03,320 --> 05:40:08,718 which in my opinion are the most 7012 05:40:05,798 --> 05:40:12,360 important ones but if you are interested 7013 05:40:08,718 --> 05:40:15,200 in specific property or maybe you feel 7014 05:40:12,360 --> 05:40:18,080 like a rebel and you want to explore all 7015 05:40:15,200 --> 05:40:20,360 of them feel free to utilize this link 7016 05:40:18,080 --> 05:40:22,878 in the read me so this is the link that 7017 05:40:20,360 --> 05:40:24,798 you're looking for as you can see it 7018 05:40:22,878 --> 05:40:26,600 guides us back to 7019 05:40:24,798 --> 05:40:28,600 documentation and you can start 7020 05:40:26,600 --> 05:40:31,638 scrolling and you'll see that this is a 7021 05:40:28,600 --> 05:40:33,680 giant file where pretty much you can 7022 05:40:31,638 --> 05:40:37,440 find everything that there is to know 7023 05:40:33,680 --> 05:40:40,798 about the DS config again big picture 7024 05:40:37,440 --> 05:40:42,958 this is where we control our typescript 7025 05:40:40,798 --> 05:40:46,718 configuration I just want to mention 7026 05:40:42,958 --> 05:40:48,840 that we have this include and in here we 7027 05:40:46,718 --> 05:40:51,040 point to where the typescript files are 7028 05:40:48,840 --> 05:40:53,320 going to be located so in our case where 7029 05:40:51,040 --> 05:40:55,638 it is well it's in the source this is 7030 05:40:53,320 --> 05:40:58,958 where we're setting up our project then 7031 05:40:55,638 --> 05:41:01,920 we have compiler options and let's start 7032 05:40:58,958 --> 05:41:04,440 with the target one over here and with 7033 05:41:01,920 --> 05:41:09,240 this one essentially we control the 7034 05:41:04,440 --> 05:41:12,320 output code so navigate to a typescript 7035 05:41:09,240 --> 05:41:15,798 playground notice I have this export con 7036 05:41:12,320 --> 05:41:20,360 and then the arrow function now if my 7037 05:41:15,798 --> 05:41:22,718 target is a JavaScript language version 7038 05:41:20,360 --> 05:41:24,600 which has the arrow function already 7039 05:41:22,718 --> 05:41:28,200 then as you can see the output code is 7040 05:41:24,600 --> 05:41:30,440 going to be pretty much the same but if 7041 05:41:28,200 --> 05:41:33,680 let's say I change this to 7042 05:41:30,440 --> 05:41:36,440 ES3 notice how it didn't have the arrow 7043 05:41:33,680 --> 05:41:38,400 function therefore we get good old 7044 05:41:36,440 --> 05:41:41,000 regular function so this is what we 7045 05:41:38,400 --> 05:41:42,798 control over here in the Target now as 7046 05:41:41,000 --> 05:41:45,160 far as the module 7047 05:41:42,798 --> 05:41:47,400 remember with this one we're controlling 7048 05:41:45,160 --> 05:41:50,760 what kind of module syntax again we're 7049 05:41:47,400 --> 05:41:53,320 going to get back in the output code in 7050 05:41:50,760 --> 05:41:55,680 the lib this is where we were 7051 05:41:53,320 --> 05:42:00,280 controlling the Declaration files that 7052 05:41:55,680 --> 05:42:01,760 we're getting and pretty much after that 7053 05:42:00,280 --> 05:42:04,840 the one that we're interested is the 7054 05:42:01,760 --> 05:42:08,718 strict one so this is this flag which 7055 05:42:04,840 --> 05:42:10,558 controls how bunch of the errors and the 7056 05:42:08,718 --> 05:42:12,520 warnings are treated so if we're going 7057 05:42:10,558 --> 05:42:14,320 to go with false then bunch of the 7058 05:42:12,520 --> 05:42:16,400 things that technically should fail are 7059 05:42:14,320 --> 05:42:18,440 not going to and we'll be able to build 7060 05:42:16,400 --> 05:42:22,400 the project now if we set this one to 7061 05:42:18,440 --> 05:42:24,680 true then we have the strict type 7062 05:42:22,400 --> 05:42:28,600 checking set to true and then for 7063 05:42:24,680 --> 05:42:31,680 example we have this no unused locals so 7064 05:42:28,600 --> 05:42:35,920 if I were to go to tutorial and come up 7065 05:42:31,680 --> 05:42:38,920 with let something is equal to something 7066 05:42:35,920 --> 05:42:42,680 notice how I get this warning that 7067 05:42:38,920 --> 05:42:47,080 something is declared but I'm not using 7068 05:42:42,680 --> 05:42:50,878 it well if we go here and set it equal 7069 05:42:47,080 --> 05:42:53,160 to pulse or just remove it then 7070 05:42:50,878 --> 05:42:56,958 essentially we'll have no warning 7071 05:42:53,160 --> 05:42:59,840 message anymore again just a example of 7072 05:42:56,958 --> 05:43:02,840 one of the properties that we can use 7073 05:42:59,840 --> 05:43:04,920 this concludes the TS config and with 7074 05:43:02,840 --> 05:43:07,478 this in place we can move on to the next 7075 05:43:04,920 --> 05:43:10,400 topic and up next let's quickly cover 7076 05:43:07,478 --> 05:43:13,638 class syntax in typescript now if you 7077 05:43:10,400 --> 05:43:16,680 don't use classes in your project feel 7078 05:43:13,638 --> 05:43:19,080 free to skip this section Al together 7079 05:43:16,680 --> 05:43:21,680 since most likely you'll see very little 7080 05:43:19,080 --> 05:43:24,360 benefit and first let's start with a 7081 05:43:21,680 --> 05:43:27,920 recap that classes in JavaScript serve 7082 05:43:24,360 --> 05:43:29,478 as blueprints for creating object and 7083 05:43:27,920 --> 05:43:30,360 they also have this special method 7084 05:43:29,478 --> 05:43:32,440 called 7085 05:43:30,360 --> 05:43:35,440 Constructor which is automatically 7086 05:43:32,440 --> 05:43:37,958 called when we create a new class 7087 05:43:35,440 --> 05:43:39,920 instance with the new keyword 7088 05:43:37,958 --> 05:43:43,440 and this keyword inside of the 7089 05:43:39,920 --> 05:43:47,200 Constructor refers to the object being 7090 05:43:43,440 --> 05:43:50,478 created so in this case I have a class 7091 05:43:47,200 --> 05:43:53,400 notice we need to use the keyword class 7092 05:43:50,478 --> 05:43:55,360 then the name is book now the convention 7093 05:43:53,400 --> 05:43:58,718 is to go with uppercase but technically 7094 05:43:55,360 --> 05:44:00,878 you don't have to and it has a 7095 05:43:58,718 --> 05:44:03,280 Constructor it's a typical method 7096 05:44:00,878 --> 05:44:06,000 basically we can pass here pams and in 7097 05:44:03,280 --> 05:44:08,400 this case I have title and the author so 7098 05:44:06,000 --> 05:44:11,798 every time I'm going to go new book and 7099 05:44:08,400 --> 05:44:14,280 pass in the values they will be set 7100 05:44:11,798 --> 05:44:16,718 inside of this object inside of this 7101 05:44:14,280 --> 05:44:19,120 class instance and in order to set those 7102 05:44:16,718 --> 05:44:21,160 properties I need to go with this dot 7103 05:44:19,120 --> 05:44:24,080 then the name of the property in this 7104 05:44:21,160 --> 05:44:26,320 case title and I just set it equal to a 7105 05:44:24,080 --> 05:44:28,840 value just being passed in so in this 7106 05:44:26,320 --> 05:44:31,680 case it's deep work and then the author 7107 05:44:28,840 --> 05:44:35,080 name now since we're working typescript 7108 05:44:31,680 --> 05:44:37,320 notice how we have few red Squigly lines 7109 05:44:35,080 --> 05:44:39,160 how do we fix them for STS we we need to 7110 05:44:37,320 --> 05:44:41,840 set up the annotations over here in the 7111 05:44:39,160 --> 05:44:46,200 Constructor so what is going to be the 7112 05:44:41,840 --> 05:44:49,360 type of the title and author and here 7113 05:44:46,200 --> 05:44:51,040 let's just go with string and string and 7114 05:44:49,360 --> 05:44:53,200 of course we can use the Boolean we can 7115 05:44:51,040 --> 05:44:56,200 use the number pretty much everything 7116 05:44:53,200 --> 05:44:58,798 that we have learned up to this point 7117 05:44:56,200 --> 05:45:02,240 but here's the kicker I still have the 7118 05:44:58,798 --> 05:45:04,120 red squigglies inside of the Constructor 7119 05:45:02,240 --> 05:45:05,958 and you would think okay we can go over 7120 05:45:04,120 --> 05:45:10,680 here and say that this is going to be a 7121 05:45:05,958 --> 05:45:13,240 string and number or Boolean but when it 7122 05:45:10,680 --> 05:45:15,798 comes to classes and typescript we 7123 05:45:13,240 --> 05:45:19,760 actually need to go inside of the class 7124 05:45:15,798 --> 05:45:22,400 and explicitly set the type for the 7125 05:45:19,760 --> 05:45:24,320 properties we are setting inside of the 7126 05:45:22,400 --> 05:45:27,160 Constructor so in this case since I have 7127 05:45:24,320 --> 05:45:30,478 two of them I have title and author yes 7128 05:45:27,160 --> 05:45:32,718 I need to go above my Constructor and 7129 05:45:30,478 --> 05:45:35,520 essentially set this one equal to a 7130 05:45:32,718 --> 05:45:38,718 string and the same is going to go for 7131 05:45:35,520 --> 05:45:40,320 author so I set both of them up and then 7132 05:45:38,718 --> 05:45:41,400 notice now I don't have the red 7133 05:45:40,320 --> 05:45:45,478 squigglies 7134 05:45:41,400 --> 05:45:47,878 and I'm able to create those instances 7135 05:45:45,478 --> 05:45:51,040 again result is going to be exactly the 7136 05:45:47,878 --> 05:45:53,878 same I will still have my instance in 7137 05:45:51,040 --> 05:45:57,320 this case de work with the title and 7138 05:45:53,878 --> 05:45:59,638 author properties the difference now 7139 05:45:57,320 --> 05:46:02,958 typescript is happy as well so I'll be 7140 05:45:59,638 --> 05:46:05,600 able to build my project just like a 7141 05:46:02,958 --> 05:46:07,920 normal JavaScript we can also set 7142 05:46:05,600 --> 05:46:11,360 instance properties or they're also 7143 05:46:07,920 --> 05:46:14,160 referred to as default properties and 7144 05:46:11,360 --> 05:46:16,638 essentially the idea is that we set this 7145 05:46:14,160 --> 05:46:19,680 property inside of the class instead of 7146 05:46:16,638 --> 05:46:22,718 the Constructor and then they get 7147 05:46:19,680 --> 05:46:25,360 automatically added to all the instances 7148 05:46:22,718 --> 05:46:28,040 we create now how's that going to look 7149 05:46:25,360 --> 05:46:32,120 like well imagine we have this checked 7150 05:46:28,040 --> 05:46:34,200 out property and it's a bullan and 7151 05:46:32,120 --> 05:46:36,320 initially it's always going to be false 7152 05:46:34,200 --> 05:46:38,320 so yes definitely we can go here with 7153 05:46:36,320 --> 05:46:41,638 this Dot 7154 05:46:38,320 --> 05:46:43,680 checked out and again notice we're not 7155 05:46:41,638 --> 05:46:46,160 passing anything in a Constructor it's 7156 05:46:43,680 --> 05:46:48,760 going to be set by default and I'm going 7157 05:46:46,160 --> 05:46:53,600 to go with false so now I can navigate 7158 05:46:48,760 --> 05:46:54,638 over here and say checked out and set it 7159 05:46:53,600 --> 05:46:58,000 equal 7160 05:46:54,638 --> 05:47:01,638 to Boolean correct so this is going to 7161 05:46:58,000 --> 05:47:03,798 work but since I'm not passing this as a 7162 05:47:01,638 --> 05:47:06,958 parameter we might as well set this one 7163 05:47:03,798 --> 05:47:10,040 up as a default property so I can just 7164 05:47:06,958 --> 05:47:11,280 move this line of code and where I have 7165 05:47:10,040 --> 05:47:14,120 the checked 7166 05:47:11,280 --> 05:47:16,080 out yes I can still keep it as Boolean 7167 05:47:14,120 --> 05:47:18,320 and set it equal to false but we can 7168 05:47:16,080 --> 05:47:21,680 also rely on the fact that typescript is 7169 05:47:18,320 --> 05:47:25,958 going to infer this value so this is up 7170 05:47:21,680 --> 05:47:28,000 to you you can go like this or you can 7171 05:47:25,958 --> 05:47:30,000 set up the checked 7172 05:47:28,000 --> 05:47:33,558 out like 7173 05:47:30,000 --> 05:47:35,760 so equal to false for example or true 7174 05:47:33,558 --> 05:47:38,718 and then typescript will immediately 7175 05:47:35,760 --> 05:47:41,120 infer type now again just like other 7176 05:47:38,718 --> 05:47:45,040 times when typescript infers the type 7177 05:47:41,120 --> 05:47:47,840 just be mindful of potential pitfalls 7178 05:47:45,040 --> 05:47:51,878 you might face and if let's say we're 7179 05:47:47,840 --> 05:47:54,798 going to go with deep work and checked 7180 05:47:51,878 --> 05:47:56,840 out we should see the value in the 7181 05:47:54,798 --> 05:48:00,680 console so at the moment it's false and 7182 05:47:56,840 --> 05:48:02,840 let's say if we decide to set it equal 7183 05:48:00,680 --> 05:48:05,558 to true that's also an option so let's 7184 05:48:02,840 --> 05:48:07,760 go with deep work checked out and let's 7185 05:48:05,558 --> 05:48:09,840 set it equal to true now since it's a 7186 05:48:07,760 --> 05:48:12,920 Boolean we can definitely do so now it's 7187 05:48:09,840 --> 05:48:16,478 equal to true but if I'm going to change 7188 05:48:12,920 --> 05:48:18,440 it to hello world of course this is not 7189 05:48:16,478 --> 05:48:21,718 going to work typescript is going to 7190 05:48:18,440 --> 05:48:25,400 complain because it is a bullion so this 7191 05:48:21,718 --> 05:48:28,160 is how we can set up default properties 7192 05:48:25,400 --> 05:48:30,798 in classes in typescript when we work 7193 05:48:28,160 --> 05:48:33,000 with classes in typescript we also have 7194 05:48:30,798 --> 05:48:36,160 this read only 7195 05:48:33,000 --> 05:48:37,478 modifier which does exactly what the 7196 05:48:36,160 --> 05:48:39,558 name suggest 7197 05:48:37,478 --> 05:48:43,040 essentially if we add this read only 7198 05:48:39,558 --> 05:48:45,280 modifier to any of the properties we 7199 05:48:43,040 --> 05:48:48,160 won't be able to modify them for example 7200 05:48:45,280 --> 05:48:51,718 if I'm going to go to my title add read 7201 05:48:48,160 --> 05:48:53,840 only yes I can still read the value for 7202 05:48:51,718 --> 05:48:56,760 example I can go with deep work and 7203 05:48:53,840 --> 05:48:59,200 title and for some reason I keep 7204 05:48:56,760 --> 05:49:03,958 forgetting to set up my 7205 05:48:59,200 --> 05:49:06,520 logs so we can still nicely access the 7206 05:49:03,958 --> 05:49:10,798 value problem is going to be if we try 7207 05:49:06,520 --> 05:49:14,120 to change it so if we go here with deep 7208 05:49:10,798 --> 05:49:16,760 work. title and set it equal to 7209 05:49:14,120 --> 05:49:19,400 something else so this is not going to 7210 05:49:16,760 --> 05:49:22,280 fly because now we have this read only 7211 05:49:19,400 --> 05:49:24,600 modifier added to the title property 7212 05:49:22,280 --> 05:49:25,638 also in typescript we have private and 7213 05:49:24,600 --> 05:49:28,600 public 7214 05:49:25,638 --> 05:49:31,520 modifiers which control whether we can 7215 05:49:28,600 --> 05:49:34,160 access the property or the method from 7216 05:49:31,520 --> 05:49:37,240 the outside now as a side note by 7217 05:49:34,160 --> 05:49:39,478 default everything is public therefore 7218 05:49:37,240 --> 05:49:42,360 if we go with deep work and type do 7219 05:49:39,478 --> 05:49:45,680 notice we have access to author checked 7220 05:49:42,360 --> 05:49:49,000 out and title but if I'm going to 7221 05:49:45,680 --> 05:49:52,440 navigate over here and let's say 7222 05:49:49,000 --> 05:49:54,600 set checked out to private notice how 7223 05:49:52,440 --> 05:49:58,600 still will go deep work and now I only 7224 05:49:54,600 --> 05:50:00,320 have access to author and title so yes 7225 05:49:58,600 --> 05:50:02,718 of course I can go over here and say 7226 05:50:00,320 --> 05:50:05,680 public just keep in mind that's the 7227 05:50:02,718 --> 05:50:07,840 default Behavior let me just set it up 7228 05:50:05,680 --> 05:50:10,440 so I'm going to go with with public over 7229 05:50:07,840 --> 05:50:13,240 here and also as a side not you can 7230 05:50:10,440 --> 05:50:16,200 combine them so notice in this case I 7231 05:50:13,240 --> 05:50:18,600 have already read only four the title 7232 05:50:16,200 --> 05:50:22,840 but I can also say that this is going to 7233 05:50:18,600 --> 05:50:26,120 be a public one now typically the way we 7234 05:50:22,840 --> 05:50:27,920 control some private property is by 7235 05:50:26,120 --> 05:50:30,320 setting up some kind of method which we 7236 05:50:27,920 --> 05:50:34,558 can call from the outside just to 7237 05:50:30,320 --> 05:50:37,798 Showcase that let me go with the 7238 05:50:34,558 --> 05:50:39,760 method and I'm going to call this check 7239 05:50:37,798 --> 05:50:42,120 out 7240 05:50:39,760 --> 05:50:44,040 then let's set up the functionality 7241 05:50:42,120 --> 05:50:45,718 where I'm going to go with this dot 7242 05:50:44,040 --> 05:50:48,520 checked out and I'm going to set it 7243 05:50:45,718 --> 05:50:52,638 equal to true so as you can see I'm 7244 05:50:48,520 --> 05:50:55,958 still able to access the property from 7245 05:50:52,638 --> 05:50:58,798 within the class but like we just saw we 7246 05:50:55,958 --> 05:51:01,798 were not able to see the checked out 7247 05:50:58,798 --> 05:51:05,200 value from outside so in this case I can 7248 05:51:01,798 --> 05:51:08,280 go with deep work then let me call the 7249 05:51:05,200 --> 05:51:10,200 method check out out and now if we'll 7250 05:51:08,280 --> 05:51:13,718 take a look at the Deep work again in a 7251 05:51:10,200 --> 05:51:16,160 console the checked out should be set 7252 05:51:13,718 --> 05:51:18,240 equal to True correct so let's navigate 7253 05:51:16,160 --> 05:51:21,200 back over here and notice of course the 7254 05:51:18,240 --> 05:51:23,638 value right now is true so this is 7255 05:51:21,200 --> 05:51:25,360 typically how we would access the value 7256 05:51:23,638 --> 05:51:27,360 we set this equal to private so we 7257 05:51:25,360 --> 05:51:29,878 cannot access it from the outside but 7258 05:51:27,360 --> 05:51:32,558 then we set up a method which actually 7259 05:51:29,878 --> 05:51:36,200 controls this value and just to hammer 7260 05:51:32,558 --> 05:51:39,558 home that we can mix and match I'll set 7261 05:51:36,200 --> 05:51:42,120 up few more methods one is going to be 7262 05:51:39,558 --> 05:51:43,958 is checked out where we're just going to 7263 05:51:42,120 --> 05:51:47,120 check the value and then I'm going to 7264 05:51:43,958 --> 05:51:49,558 set up the private method so again this 7265 05:51:47,120 --> 05:51:51,878 is a bit of Overkill but I'm just going 7266 05:51:49,558 --> 05:51:54,000 to add this public one over here as you 7267 05:51:51,878 --> 05:51:56,760 can see that's the default Behavior 7268 05:51:54,000 --> 05:51:59,240 everything is public then we're going to 7269 05:51:56,760 --> 05:52:00,240 go with another one where we're just 7270 05:51:59,240 --> 05:52:04,000 going 7271 05:52:00,240 --> 05:52:06,240 to check what is the value so we don't 7272 05:52:04,000 --> 05:52:09,520 need to take a look at the console so in 7273 05:52:06,240 --> 05:52:11,600 here I simply want to return this Dot 7274 05:52:09,520 --> 05:52:14,558 and checked 7275 05:52:11,600 --> 05:52:17,638 out and then also I want to set up the 7276 05:52:14,558 --> 05:52:22,400 private one where we're going to toggle 7277 05:52:17,638 --> 05:52:26,520 so this will be a private method toggle 7278 05:52:22,400 --> 05:52:28,600 and let's come up with the name status 7279 05:52:26,520 --> 05:52:31,558 that sounds like a good one hopefully I 7280 05:52:28,600 --> 05:52:34,558 have no spelling errors and from this 7281 05:52:31,558 --> 05:52:36,280 function I simply want to return the 7282 05:52:34,558 --> 05:52:39,558 opposite value so I'm going to go with 7283 05:52:36,280 --> 05:52:41,840 this Dot and we're looking for checked 7284 05:52:39,558 --> 05:52:43,840 out now since this is a private one 7285 05:52:41,840 --> 05:52:46,760 again I'm not going to be able to call 7286 05:52:43,840 --> 05:52:48,958 it here where I have my instance instead 7287 05:52:46,760 --> 05:52:51,400 where I have the checkout one I'll just 7288 05:52:48,958 --> 05:52:54,718 set it equal to this dot so now I'm 7289 05:52:51,400 --> 05:52:58,200 accessing the private method and I'm 7290 05:52:54,718 --> 05:53:01,240 invoking the toggle checked out status 7291 05:52:58,200 --> 05:53:05,400 like so and now let's just navigate over 7292 05:53:01,240 --> 05:53:07,798 here I guess let's clean up our code a 7293 05:53:05,400 --> 05:53:10,798 little bit and and let's just invoke few 7294 05:53:07,798 --> 05:53:14,958 functions so for STS let me go with deep 7295 05:53:10,798 --> 05:53:17,160 work we have checkout that's the one 7296 05:53:14,958 --> 05:53:19,558 that we want to invoke and after that 7297 05:53:17,160 --> 05:53:22,080 we're going to go with log and again 7298 05:53:19,558 --> 05:53:25,798 we're looking for our instance and now 7299 05:53:22,080 --> 05:53:28,040 we want to check the actual status and 7300 05:53:25,798 --> 05:53:31,558 notice over here this is set equal to 7301 05:53:28,040 --> 05:53:35,558 true so by default it was set to false 7302 05:53:31,558 --> 05:53:38,400 then I ran the checkout method which 7303 05:53:35,558 --> 05:53:42,240 essentially just toggles the value and 7304 05:53:38,400 --> 05:53:44,600 then I logged in a console that it's 7305 05:53:42,240 --> 05:53:47,040 equal to true now let me try this one 7306 05:53:44,600 --> 05:53:49,680 out let me run it two times and now this 7307 05:53:47,040 --> 05:53:52,400 should be set equal to false because 7308 05:53:49,680 --> 05:53:55,280 we're just toggling back and forth so 7309 05:53:52,400 --> 05:53:56,718 this is how we can use public private 7310 05:53:55,280 --> 05:53:59,878 and read only 7311 05:53:56,718 --> 05:54:02,878 modifiers in classes when we work with 7312 05:53:59,878 --> 05:54:05,520 typescript and up next let's see an 7313 05:54:02,878 --> 05:54:07,600 awesome shortcut we can use which is 7314 05:54:05,520 --> 05:54:09,958 going to say was quite a few lines of 7315 05:54:07,600 --> 05:54:12,680 code now for starters I do want to clean 7316 05:54:09,958 --> 05:54:13,558 this up since I want to discuss Getters 7317 05:54:12,680 --> 05:54:16,478 and 7318 05:54:13,558 --> 05:54:19,440 Setters in the upcoming videos and I 7319 05:54:16,478 --> 05:54:22,638 think it's going to be too much logic 7320 05:54:19,440 --> 05:54:25,798 inside of one class and also let me 7321 05:54:22,638 --> 05:54:28,360 remove the first two properties as well 7322 05:54:25,798 --> 05:54:31,400 as all of this code over here in the 7323 05:54:28,360 --> 05:54:33,600 Constructor so the shortcut is following 7324 05:54:31,400 --> 05:54:35,798 at the moment notice if I go with my 7325 05:54:33,600 --> 05:54:38,760 deep work and type dot I don't see 7326 05:54:35,798 --> 05:54:41,680 anything correct since we didn't set the 7327 05:54:38,760 --> 05:54:48,320 properties correctly but what we can 7328 05:54:41,680 --> 05:54:52,760 also do is add readon public or private 7329 05:54:48,320 --> 05:54:55,440 modifier in front of the parameters and 7330 05:54:52,760 --> 05:54:59,280 if we do that it will be automatically 7331 05:54:55,440 --> 05:55:00,958 set as a property with the value inside 7332 05:54:59,280 --> 05:55:03,840 of the instance so let me show you what 7333 05:55:00,958 --> 05:55:07,000 I'm talking about we go here with read 7334 05:55:03,840 --> 05:55:09,160 only and yes even though 7335 05:55:07,000 --> 05:55:13,680 it might seem redundant since by default 7336 05:55:09,160 --> 05:55:16,520 technically this is public if we go here 7337 05:55:13,680 --> 05:55:18,638 and set up the public this will be set 7338 05:55:16,520 --> 05:55:20,798 in my instance now we're not going to 7339 05:55:18,638 --> 05:55:23,360 use this value but just to Showcase that 7340 05:55:20,798 --> 05:55:26,240 same is going to work with private I'm 7341 05:55:23,360 --> 05:55:30,320 simply going to go over here and say 7342 05:55:26,240 --> 05:55:33,440 some value and let me set it equal to a 7343 05:55:30,320 --> 05:55:36,280 number so now if I go to my deep work 7344 05:55:33,440 --> 05:55:39,000 right now and type that notice I 7345 05:55:36,280 --> 05:55:41,280 immediately have access to author and 7346 05:55:39,000 --> 05:55:43,920 the title so this is the shortcut we 7347 05:55:41,280 --> 05:55:47,120 don't need to use this anymore over here 7348 05:55:43,920 --> 05:55:50,320 we don't need to explicitly set these 7349 05:55:47,120 --> 05:55:54,320 properties in a class we simply can use 7350 05:55:50,320 --> 05:55:58,120 either read only or public or private 7351 05:55:54,320 --> 05:55:59,840 and effectively immediately tcri behind 7352 05:55:58,120 --> 05:56:02,440 the scenes is going to set those 7353 05:55:59,840 --> 05:56:06,400 properties with the appropriate values 7354 05:56:02,440 --> 05:56:08,958 when we create the instance now since 7355 05:56:06,400 --> 05:56:13,478 you might not believe me about the Su 7356 05:56:08,958 --> 05:56:16,200 value let's come up with some random 7357 05:56:13,478 --> 05:56:18,558 function let's just explicitly say that 7358 05:56:16,200 --> 05:56:23,520 this is going to be public and I'm going 7359 05:56:18,558 --> 05:56:27,200 to call this get some value okay and 7360 05:56:23,520 --> 05:56:30,000 then we'll return this Dot and some 7361 05:56:27,200 --> 05:56:33,040 value so notice inside of the class I do 7362 05:56:30,000 --> 05:56:35,878 have access to it so let me call get 7363 05:56:33,040 --> 05:56:39,080 some value and we do want to place it in 7364 05:56:35,878 --> 05:56:43,000 a con log I guess so get some value 7365 05:56:39,080 --> 05:56:46,360 let's invoke this let's set up our log 7366 05:56:43,000 --> 05:56:47,320 let's move this inside and the moment it 7367 05:56:46,360 --> 05:56:49,718 says 7368 05:56:47,320 --> 05:56:51,920 undefined because I did not provide the 7369 05:56:49,718 --> 05:56:55,638 value okay so notice of course it's 7370 05:56:51,920 --> 05:56:57,718 missing so I have some value and since 7371 05:56:55,638 --> 05:56:59,120 I'm looking for a number I'm going to go 7372 05:56:57,718 --> 05:57:02,400 with 7373 05:56:59,120 --> 05:57:05,040 45 so once we log right now everything 7374 05:57:02,400 --> 05:57:07,080 is correct so again this is an awesome 7375 05:57:05,040 --> 05:57:10,840 shortcut we can use 7376 05:57:07,080 --> 05:57:14,520 in the Constructor we can add one of the 7377 05:57:10,840 --> 05:57:17,680 modifiers and this will automatically 7378 05:57:14,520 --> 05:57:19,760 set those properties in our instance and 7379 05:57:17,680 --> 05:57:22,798 up next let's work on the example with 7380 05:57:19,760 --> 05:57:26,240 Getters and Setters which are special 7381 05:57:22,798 --> 05:57:28,160 methods that are access like properties 7382 05:57:26,240 --> 05:57:31,240 more specifically let's take a look how 7383 05:57:28,160 --> 05:57:32,440 we can combine them with the private and 7384 05:57:31,240 --> 05:57:35,160 public 7385 05:57:32,440 --> 05:57:37,360 modifiers which we covered in the 7386 05:57:35,160 --> 05:57:40,040 previous video 7387 05:57:37,360 --> 05:57:42,920 so one cool thing that we can do with 7388 05:57:40,040 --> 05:57:45,400 the getter is to set up a computed 7389 05:57:42,920 --> 05:57:48,878 property so essentially I can create the 7390 05:57:45,400 --> 05:57:51,478 property on a fly now in order to create 7391 05:57:48,878 --> 05:57:54,680 the getter or the setter we want to go 7392 05:57:51,478 --> 05:57:57,240 with get or set eord then we want to 7393 05:57:54,680 --> 05:58:01,280 come up with a name so this essentially 7394 05:57:57,240 --> 05:58:04,840 will be a property on this deep work and 7395 05:58:01,280 --> 05:58:07,240 in here I just want to compute some 7396 05:58:04,840 --> 05:58:09,680 value so in this case case I'm going to 7397 05:58:07,240 --> 05:58:13,600 go with the template string first I'll 7398 05:58:09,680 --> 05:58:17,718 access the title and I'll combine it 7399 05:58:13,600 --> 05:58:20,400 with the author this dot author let's 7400 05:58:17,718 --> 05:58:22,920 save it and now we want to go with our 7401 05:58:20,400 --> 05:58:25,478 deep work and notice we have essentially 7402 05:58:22,920 --> 05:58:27,840 this info property now since we're 7403 05:58:25,478 --> 05:58:30,760 returning the value I keep forgetting 7404 05:58:27,840 --> 05:58:33,718 that we want to place this inside of the 7405 05:58:30,760 --> 05:58:36,160 log and in console now we have title 7406 05:58:33,718 --> 05:58:39,240 followed by the author again what's 7407 05:58:36,160 --> 05:58:42,200 super cool is that we can create this on 7408 05:58:39,240 --> 05:58:45,840 a fly so I can just come up with the 7409 05:58:42,200 --> 05:58:47,320 property then do my computation and I 7410 05:58:45,840 --> 05:58:50,600 didn't need to set anything in the 7411 05:58:47,320 --> 05:58:53,360 Constructor or above the Constructor and 7412 05:58:50,600 --> 05:58:56,080 the same way we can set up the 7413 05:58:53,360 --> 05:58:58,478 setter the keyword we're looking for is 7414 05:58:56,080 --> 05:59:01,840 set and in this case again I'm going to 7415 05:58:58,478 --> 05:59:05,558 control that checked out property first 7416 05:59:01,840 --> 05:59:06,680 let me start with a set name now just 7417 05:59:05,558 --> 05:59:08,120 like 7418 05:59:06,680 --> 05:59:10,760 good old 7419 05:59:08,120 --> 05:59:13,600 method we can provide over here 7420 05:59:10,760 --> 05:59:16,160 parameters and in this case I'm going to 7421 05:59:13,600 --> 05:59:18,920 call my parameter checked out now we do 7422 05:59:16,160 --> 05:59:21,080 need to provide a type and then inside 7423 05:59:18,920 --> 05:59:25,000 of the setter we're going to go with 7424 05:59:21,080 --> 05:59:28,320 checked out and we'll set it equal to 7425 05:59:25,000 --> 05:59:31,040 our parameter value and then let's 7426 05:59:28,320 --> 05:59:33,840 navigate to the console log and right 7427 05:59:31,040 --> 05:59:36,080 after that let's invoke our Setter so 7428 05:59:33,840 --> 05:59:39,120 we're going to go with our instance then 7429 05:59:36,080 --> 05:59:41,840 and set our name and in this case again 7430 05:59:39,120 --> 05:59:44,680 this is the property so we set the value 7431 05:59:41,840 --> 05:59:46,638 now if I'll try to go with hello and 7432 05:59:44,680 --> 05:59:49,558 notice how typescript immediately 7433 05:59:46,638 --> 05:59:52,200 complains so let me go back and yep I 7434 05:59:49,558 --> 05:59:53,920 want to go true and now I don't see 7435 05:59:52,200 --> 05:59:57,000 anything in a console since I'm not 7436 05:59:53,920 --> 05:59:58,638 logging let me try one more time where 7437 05:59:57,000 --> 06:00:02,200 I'm going to go deep work and if 7438 05:59:58,638 --> 06:00:06,320 everything's correct the checked out 7439 06:00:02,200 --> 06:00:09,200 should be equal to true and we can use 7440 06:00:06,320 --> 06:00:11,718 the private and public as you can see 7441 06:00:09,200 --> 06:00:15,320 again by default everything is already 7442 06:00:11,718 --> 06:00:19,600 public but let me just showcase if we go 7443 06:00:15,320 --> 06:00:21,600 here with private now we can only invoke 7444 06:00:19,600 --> 06:00:23,760 this inside of the class so this is not 7445 06:00:21,600 --> 06:00:29,520 going to work let me comment this one 7446 06:00:23,760 --> 06:00:32,840 out and let me set up two more 7447 06:00:29,520 --> 06:00:35,200 Getters one is going to be for the 7448 06:00:32,840 --> 06:00:39,160 checked out value and in the second one 7449 06:00:35,200 --> 06:00:42,160 will actually we use the check out 7450 06:00:39,160 --> 06:00:44,400 method a lot of checkouts so let's go 7451 06:00:42,160 --> 06:00:47,120 over here with get now as you can see 7452 06:00:44,400 --> 06:00:49,840 the name can be exactly the same because 7453 06:00:47,120 --> 06:00:52,000 one is a Setter and other one is a 7454 06:00:49,840 --> 06:00:56,080 getter by default it's going to be 7455 06:00:52,000 --> 06:00:58,240 public and then from this one we want to 7456 06:00:56,080 --> 06:01:01,120 return checked 7457 06:00:58,240 --> 06:01:04,520 out and now Just for kicks let's set up 7458 06:01:01,120 --> 06:01:06,478 a getter but inside of it let's invoke 7459 06:01:04,520 --> 06:01:09,240 the setter just to make it more 7460 06:01:06,478 --> 06:01:11,400 confusing so let's go here with public 7461 06:01:09,240 --> 06:01:13,440 even though again technically it doesn't 7462 06:01:11,400 --> 06:01:15,878 change anything in this case I'm going 7463 06:01:13,440 --> 06:01:20,280 to go with some info we're not going to 7464 06:01:15,878 --> 06:01:22,638 pass anything in and in order to access 7465 06:01:20,280 --> 06:01:26,638 our Setter our private one we want to go 7466 06:01:22,638 --> 06:01:29,718 with this. checkout set it equal to true 7467 06:01:26,638 --> 06:01:32,400 and then after that we want to return 7468 06:01:29,718 --> 06:01:35,040 and since I want to return the same 7469 06:01:32,400 --> 06:01:37,558 value you know what let me copy from the 7470 06:01:35,040 --> 06:01:41,320 info and and let me just 7471 06:01:37,558 --> 06:01:45,638 overwrite in here so now let's navigate 7472 06:01:41,320 --> 06:01:48,520 to a console first we want to go with 7473 06:01:45,638 --> 06:01:52,520 log I guess we're going to go with deep 7474 06:01:48,520 --> 06:01:55,160 work then some info so this is going to 7475 06:01:52,520 --> 06:01:58,680 set our checkout equal to true and also 7476 06:01:55,160 --> 06:02:02,718 return this value and also 7477 06:01:58,680 --> 06:02:05,320 let's copy and paste and set up one more 7478 06:02:02,718 --> 06:02:07,878 and in this case I'm looking for my 7479 06:02:05,320 --> 06:02:10,478 getter the checkout one and once I 7480 06:02:07,878 --> 06:02:13,160 navigate to a console as you can see at 7481 06:02:10,478 --> 06:02:14,360 this point it's false then I get back 7482 06:02:13,160 --> 06:02:18,200 the 7483 06:02:14,360 --> 06:02:20,160 same info the title followed by the 7484 06:02:18,200 --> 06:02:23,240 author and at the very end we can see 7485 06:02:20,160 --> 06:02:26,240 the checked out value which is now equal 7486 06:02:23,240 --> 06:02:28,520 to true and that's how we can use 7487 06:02:26,240 --> 06:02:31,400 Getters and Setters with public and 7488 06:02:28,520 --> 06:02:34,840 private modifiers and up next let me 7489 06:02:31,400 --> 06:02:38,040 showcase how we can implement the 7490 06:02:34,840 --> 06:02:41,520 interface by the class and if you 7491 06:02:38,040 --> 06:02:44,520 remember the video types versus 7492 06:02:41,520 --> 06:02:46,320 interfaces it was one of the things 7493 06:02:44,520 --> 06:02:48,878 which we could do with the interface but 7494 06:02:46,320 --> 06:02:51,440 we cannot do with the type so let's 7495 06:02:48,878 --> 06:02:54,680 imagine we have the interface and the 7496 06:02:51,440 --> 06:02:57,200 name is I person which essentially 7497 06:02:54,680 --> 06:03:00,200 stands for interface person because 7498 06:02:57,200 --> 06:03:02,200 there will be a class of person let's 7499 06:03:00,200 --> 06:03:04,798 come up with some random properties so 7500 06:03:02,200 --> 06:03:08,958 we're looking for string then also we 7501 06:03:04,798 --> 06:03:12,520 want to set up the age number and you 7502 06:03:08,958 --> 06:03:14,280 know what let's go with greet method and 7503 06:03:12,520 --> 06:03:16,958 it's not going to return anything and 7504 06:03:14,280 --> 06:03:20,160 it's also not going to take any 7505 06:03:16,958 --> 06:03:22,920 parameters then we're going to go with 7506 06:03:20,160 --> 06:03:26,600 class let's come up with the name in my 7507 06:03:22,920 --> 06:03:30,798 case person and we go with this 7508 06:03:26,600 --> 06:03:33,440 Implement which essentially is promising 7509 06:03:30,798 --> 06:03:36,478 that it will provide all of the 7510 06:03:33,440 --> 06:03:39,120 properties and methods defined in in the 7511 06:03:36,478 --> 06:03:41,798 interface so in this case we're looking 7512 06:03:39,120 --> 06:03:44,798 for I person that's the interface I want 7513 06:03:41,798 --> 06:03:47,478 to use and in here let's go with the 7514 06:03:44,798 --> 06:03:50,200 shortcut which we covered before so I 7515 06:03:47,478 --> 06:03:54,040 can go with public now what is the name 7516 06:03:50,200 --> 06:03:56,878 it's a string correct so let's set up 7517 06:03:54,040 --> 06:03:59,840 and also we want to go here with public 7518 06:03:56,878 --> 06:04:01,080 age now this will be a number we're not 7519 06:03:59,840 --> 06:04:03,680 going to set up anything in the 7520 06:04:01,080 --> 06:04:06,600 Constructor well we don't have to we 7521 06:04:03,680 --> 06:04:09,440 still have a red squiggly line because 7522 06:04:06,600 --> 06:04:12,878 there is no method so right after 7523 06:04:09,440 --> 06:04:15,478 Constructor let's go with greet it's our 7524 06:04:12,878 --> 06:04:19,760 function and let's just log some stuff 7525 06:04:15,478 --> 06:04:21,638 over here in this case I'm will go with 7526 06:04:19,760 --> 06:04:24,600 hello my 7527 06:04:21,638 --> 06:04:29,638 name is Slim 7528 06:04:24,600 --> 06:04:35,360 Shady and this is name and also I 7529 06:04:29,638 --> 06:04:37,520 am this. age let me access this. Ag and 7530 06:04:35,360 --> 06:04:41,240 we're going to go with 7531 06:04:37,520 --> 06:04:44,478 years old now we do want to set up our 7532 06:04:41,240 --> 06:04:48,320 instance so let's go with 7533 06:04:44,478 --> 06:04:52,240 con and hipster we're looking for a new 7534 06:04:48,320 --> 06:04:55,680 person let's provide a name Shake and 7535 06:04:52,240 --> 06:04:58,240 Bake and as far as the years well let's 7536 06:04:55,680 --> 06:04:59,718 go with 100 and then at the very end 7537 06:04:58,240 --> 06:05:03,760 let's invoke 7538 06:04:59,718 --> 06:05:06,680 hipster GRE and if everything is correct 7539 06:05:03,760 --> 06:05:07,958 we should see the text in the com so so 7540 06:05:06,680 --> 06:05:12,280 that's how we 7541 06:05:07,958 --> 06:05:14,958 can implement the interface with the 7542 06:05:12,280 --> 06:05:18,878 class again something we canot do with 7543 06:05:14,958 --> 06:05:21,280 the good old type Alias okay and once 7544 06:05:18,878 --> 06:05:24,360 we're done with tutorial let's put our 7545 06:05:21,280 --> 06:05:27,878 typescript knowledge to a good use and 7546 06:05:24,360 --> 06:05:30,160 create a small tasks application as far 7547 06:05:27,878 --> 06:05:34,080 as the setup for this project I'm going 7548 06:05:30,160 --> 06:05:36,920 to set my code editor and browser side 7549 06:05:34,080 --> 06:05:38,920 by side but you you don't have to it's 7550 06:05:36,920 --> 06:05:41,240 just my preference since that way I 7551 06:05:38,920 --> 06:05:45,958 don't have to switch screens like 7552 06:05:41,240 --> 06:05:48,320 gazillion times also I will set up a new 7553 06:05:45,958 --> 06:05:49,840 page in my case I'm going to call this 7554 06:05:48,320 --> 06:05:52,478 tasks 7555 06:05:49,840 --> 06:05:55,040 HTML keep in mind technically you can 7556 06:05:52,478 --> 06:05:58,878 build everything in index HTML I just 7557 06:05:55,040 --> 06:06:02,440 think that it's going to be easier if we 7558 06:05:58,878 --> 06:06:04,958 separate the tutorial from the project 7559 06:06:02,440 --> 06:06:06,760 so here's what we want to do navigate to 7560 06:06:04,958 --> 06:06:09,200 the route again please don't set this 7561 06:06:06,760 --> 06:06:11,958 one up in the source we want to create a 7562 06:06:09,200 --> 06:06:16,160 new file and I'm going to call this 7563 06:06:11,958 --> 06:06:19,760 tasks HTML now I will create a basic 7564 06:06:16,160 --> 06:06:22,680 HTML structure and in the vs code 7565 06:06:19,760 --> 06:06:25,478 essentially we just do that by getting 7566 06:06:22,680 --> 06:06:28,400 the exclamation point I believe I mean I 7567 06:06:25,478 --> 06:06:31,200 haven't created the straight up HTML 7568 06:06:28,400 --> 06:06:33,200 project in so long that I already forgot 7569 06:06:31,200 --> 06:06:36,760 so yeah basically we want to click shift 7570 06:06:33,200 --> 06:06:39,360 and back tick and notice we get the this 7571 06:06:36,760 --> 06:06:42,360 suggestion from EMT that you can create 7572 06:06:39,360 --> 06:06:45,878 a HTML structure now in my case I'm 7573 06:06:42,360 --> 06:06:49,080 going to call this tasks app or tasks 7574 06:06:45,878 --> 06:06:52,280 then also there's going to be a link for 7575 06:06:49,080 --> 06:06:57,000 CSS so that is coming up and in here for 7576 06:06:52,280 --> 06:06:59,798 now let's just say tasks app let's save 7577 06:06:57,000 --> 06:07:02,320 it now this is totally up to you you can 7578 06:06:59,798 --> 06:07:06,638 navigate in the browser and just stay 7579 06:07:02,320 --> 06:07:10,360 there or we can actually go to to main 7580 06:07:06,638 --> 06:07:12,558 DS and notice we have both of these 7581 06:07:10,360 --> 06:07:15,120 logos well at the moment these links 7582 06:07:12,558 --> 06:07:18,160 navigate somewhere externally basically 7583 06:07:15,120 --> 06:07:20,638 back to typescript and vat and we can 7584 06:07:18,160 --> 06:07:23,120 actually change this around where I'm 7585 06:07:20,638 --> 06:07:25,360 going to go with forward slash 7586 06:07:23,120 --> 06:07:27,840 and 7587 06:07:25,360 --> 06:07:30,360 tasks and of course if you named your 7588 06:07:27,840 --> 06:07:33,840 page differently please make sure that 7589 06:07:30,360 --> 06:07:36,200 the H matches and I'll do the same for 7590 06:07:33,840 --> 06:07:38,160 this one so now once I click on one of 7591 06:07:36,200 --> 06:07:40,440 them I'm going to navigate to the tasks 7592 06:07:38,160 --> 06:07:43,080 app now I'm not going to set up the link 7593 06:07:40,440 --> 06:07:46,240 back I don't think there's any need for 7594 06:07:43,080 --> 06:07:50,080 it for now I think we can close the main 7595 06:07:46,240 --> 06:07:54,200 one but we will return to it and also 7596 06:07:50,080 --> 06:07:56,920 let's create an Source tasks DS so this 7597 06:07:54,200 --> 06:08:00,080 is going to be our typescript file where 7598 06:07:56,920 --> 06:08:01,840 we'll set up the logic let's go with Ds 7599 06:08:00,080 --> 06:08:04,760 now in this case we're not going to 7600 06:08:01,840 --> 06:08:07,878 import that in the main DS we actually 7601 06:08:04,760 --> 06:08:11,080 want to go there directly to 7602 06:08:07,878 --> 06:08:13,000 tasks somewhere here the bottom of the 7603 06:08:11,080 --> 06:08:15,558 body we want to go with 7604 06:08:13,000 --> 06:08:19,638 script then we're going to add type 7605 06:08:15,558 --> 06:08:21,958 module and now let's look for 7606 06:08:19,638 --> 06:08:25,040 Source we're looking 7607 06:08:21,958 --> 06:08:29,120 for tasks I believe yep that was the 7608 06:08:25,040 --> 06:08:32,200 file and I always prefer to set up some 7609 06:08:29,120 --> 06:08:35,798 log just so I don't run around like a 7610 06:08:32,200 --> 06:08:38,280 headless chicken looking for a bug but 7611 06:08:35,798 --> 06:08:41,160 maybe there's an UT wrong or something 7612 06:08:38,280 --> 06:08:43,320 along those lines now for some reason 7613 06:08:41,160 --> 06:08:45,878 yep everything is fine so I see over 7614 06:08:43,320 --> 06:08:50,798 here the tasks so I know 7615 06:08:45,878 --> 06:08:53,360 that my logic works then let's navigate 7616 06:08:50,798 --> 06:08:58,160 uh back to tasks 7617 06:08:53,360 --> 06:09:00,080 HTML and now above the script 7618 06:08:58,160 --> 06:09:02,680 effectively we want to set up the 7619 06:09:00,080 --> 06:09:05,840 structure for the application so I just 7620 06:09:02,680 --> 06:09:08,440 want to add some HTML elements now this 7621 06:09:05,840 --> 06:09:13,600 is totally up to you but I also provided 7622 06:09:08,440 --> 06:09:17,120 some CSS just so it looks a bit more 7623 06:09:13,600 --> 06:09:19,760 presentable now you have two options you 7624 06:09:17,120 --> 06:09:21,718 can either get it from the final folder 7625 06:09:19,760 --> 06:09:24,760 so navigate to the final 7626 06:09:21,718 --> 06:09:27,600 repo look for typescript tutorial and 7627 06:09:24,760 --> 06:09:30,240 then more specifically look for task CSS 7628 06:09:27,600 --> 06:09:32,840 so I didn't want to add this code in the 7629 06:09:30,240 --> 06:09:35,920 main CSS file so we're going to create a 7630 06:09:32,840 --> 06:09:40,040 separate file for that task CSS and then 7631 06:09:35,920 --> 06:09:42,400 just grab that code or you can nicely 7632 06:09:40,040 --> 06:09:44,958 navigate to the end of the readme and 7633 06:09:42,400 --> 06:09:48,520 just copy this code again there's a 7634 06:09:44,958 --> 06:09:51,558 bunch of Base Styles so let's not worry 7635 06:09:48,520 --> 06:09:53,798 about the CSS too much as far as the 7636 06:09:51,558 --> 06:09:57,080 actual CSS for the project there's not 7637 06:09:53,798 --> 06:09:59,360 that much code but I'm not going to go 7638 06:09:57,080 --> 06:10:02,160 over it because this is definitely not a 7639 06:09:59,360 --> 06:10:04,478 CSS course so just select everything 7640 06:10:02,160 --> 06:10:05,920 inside of the back dick where you see 7641 06:10:04,478 --> 06:10:07,000 the CSS 7642 06:10:05,920 --> 06:10:10,440 then we want 7643 06:10:07,000 --> 06:10:12,680 to go I guess let's set up in a source 7644 06:10:10,440 --> 06:10:17,360 doesn't really matter we're going to go 7645 06:10:12,680 --> 06:10:22,360 tasks CSS copy and paste and now where 7646 06:10:17,360 --> 06:10:27,718 we have a tasks HTML we want to set up a 7647 06:10:22,360 --> 06:10:30,718 link we're looking for the CSS one and 7648 06:10:27,718 --> 06:10:32,760 basically it's in the same or it's not 7649 06:10:30,718 --> 06:10:36,200 sorry it's not in the same folder we're 7650 06:10:32,760 --> 06:10:39,798 actually looking for t 7651 06:10:36,200 --> 06:10:42,240 CSS so once we add that I mean at the 7652 06:10:39,798 --> 06:10:45,400 moment most likely we won't see anything 7653 06:10:42,240 --> 06:10:48,360 but once we add our elements we should 7654 06:10:45,400 --> 06:10:51,160 have a nice CSS as well and with this in 7655 06:10:48,360 --> 06:10:52,718 place now let's set up the structure and 7656 06:10:51,160 --> 06:10:56,280 I'm not sure whether I mentioned that 7657 06:10:52,718 --> 06:10:58,680 technically the CSS part is optional so 7658 06:10:56,280 --> 06:11:00,958 I mean it's not going to affect any of 7659 06:10:58,680 --> 06:11:05,638 our logic our application is just going 7660 06:11:00,958 --> 06:11:07,520 to look tiny bit more representable then 7661 06:11:05,638 --> 06:11:10,160 we want to go with tasks of some kind of 7662 06:11:07,520 --> 06:11:12,840 value as a sign note we might need to 7663 06:11:10,160 --> 06:11:15,680 refresh yep now everything works now I 7664 06:11:12,840 --> 06:11:18,040 can see my tasks over here then let's 7665 06:11:15,680 --> 06:11:21,718 create a form now we won't have the 7666 06:11:18,040 --> 06:11:24,520 action but I do want to add the class 7667 06:11:21,718 --> 06:11:26,080 and even if you're not using my CSS 7668 06:11:24,520 --> 06:11:28,718 please make sure that you use the same 7669 06:11:26,080 --> 06:11:30,760 classes or if you use a different class 7670 06:11:28,718 --> 06:11:32,200 make sure that you remember that when we 7671 06:11:30,760 --> 06:11:35,080 are setting up the 7672 06:11:32,200 --> 06:11:38,320 logic then let's go with input inside of 7673 06:11:35,080 --> 06:11:40,120 it the type is going to be text then we 7674 06:11:38,320 --> 06:11:44,080 want to go with another class and we're 7675 06:11:40,120 --> 06:11:46,920 going to go with form input save this 7676 06:11:44,080 --> 06:11:48,638 okay this is what we should see then 7677 06:11:46,920 --> 06:11:53,040 also let's add a 7678 06:11:48,638 --> 06:11:55,760 button type will be equal to submit okay 7679 06:11:53,040 --> 06:12:00,558 beautiful let's add a class name 7680 06:11:55,760 --> 06:12:03,680 BTN let's call this add task okay again 7681 06:12:00,558 --> 06:12:08,160 I'll have to refresh and then right 7682 06:12:03,680 --> 06:12:10,718 after the form we have anord list class 7683 06:12:08,160 --> 06:12:13,360 let's go with list we're not going to 7684 06:12:10,718 --> 06:12:15,400 place any items so we will place the 7685 06:12:13,360 --> 06:12:17,558 items dynamically with the help of 7686 06:12:15,400 --> 06:12:21,280 JavaScript and then at the very end 7687 06:12:17,558 --> 06:12:22,840 let's just add a test button because 7688 06:12:21,280 --> 06:12:25,040 there's something we need to discuss 7689 06:12:22,840 --> 06:12:27,400 first so technically this is not going 7690 06:12:25,040 --> 06:12:31,000 to be part of the project but it's super 7691 06:12:27,400 --> 06:12:32,798 important we cover two things and 7692 06:12:31,000 --> 06:12:33,638 therefore we're going to go here with 7693 06:12:32,798 --> 06:12:37,360 the 7694 06:12:33,638 --> 06:12:41,958 button so we're creating the good old 7695 06:12:37,360 --> 06:12:45,080 button let's add a class of test BTN and 7696 06:12:41,958 --> 06:12:48,718 let's just right click me let me save 7697 06:12:45,080 --> 06:12:51,240 Let me refresh and this is what we 7698 06:12:48,718 --> 06:12:53,600 should see in the browser and if you 7699 06:12:51,240 --> 06:12:56,080 have the same result now we can move on 7700 06:12:53,600 --> 06:12:58,478 to the next step okay and before we 7701 06:12:56,080 --> 06:13:02,000 start typing away let's address these 7702 06:12:58,478 --> 06:13:05,600 two important points first when we use 7703 06:13:02,000 --> 06:13:08,558 Query selector we actually get back the 7704 06:13:05,600 --> 06:13:10,558 element which is the most General Base 7705 06:13:08,558 --> 06:13:12,920 Class that only has the methods and 7706 06:13:10,558 --> 06:13:16,080 properties common to all kinds of 7707 06:13:12,920 --> 06:13:19,280 elements so that's Point number one 7708 06:13:16,080 --> 06:13:22,878 second there's also potential for null 7709 06:13:19,280 --> 06:13:25,440 and we also need to address that and no 7710 06:13:22,878 --> 06:13:27,958 we cannot build this project unless we 7711 06:13:25,440 --> 06:13:30,840 cover both of these things extensively 7712 06:13:27,958 --> 06:13:33,920 now once we do it should be a smooth 7713 06:13:30,840 --> 06:13:36,840 sailing so first let me just showcase 7714 06:13:33,920 --> 06:13:39,160 the two important points 7715 06:13:36,840 --> 06:13:41,280 if we go with cons button we know we 7716 06:13:39,160 --> 06:13:43,360 have access to a document correct 7717 06:13:41,280 --> 06:13:46,520 because we have the Declaration files 7718 06:13:43,360 --> 06:13:49,280 and document has this awesome method 7719 06:13:46,520 --> 06:13:52,478 called query selector where we just pass 7720 06:13:49,280 --> 06:13:55,280 the class in this case and we can select 7721 06:13:52,478 --> 06:13:59,718 the element and everything is awesome 7722 06:13:55,280 --> 06:14:03,280 but as I hover over the button notice it 7723 06:13:59,718 --> 06:14:06,120 can either be an element so it's going 7724 06:14:03,280 --> 06:14:07,718 to be an issue if I try to 7725 06:14:06,120 --> 06:14:09,920 get some properties and methods that are 7726 06:14:07,718 --> 06:14:12,000 only specific to buttons which is 7727 06:14:09,920 --> 06:14:16,840 actually going to be our second point 7728 06:14:12,000 --> 06:14:19,200 and also it can potentially be null now 7729 06:14:16,840 --> 06:14:23,280 why do we care about that well let's say 7730 06:14:19,200 --> 06:14:25,520 if I want to go with BTN and then Dot 7731 06:14:23,280 --> 06:14:27,840 and event listener it's not going to 7732 06:14:25,520 --> 06:14:30,040 work well technically it's going to work 7733 06:14:27,840 --> 06:14:33,400 because typescript is going to fix my 7734 06:14:30,040 --> 06:14:36,200 code but notice it's only going to work 7735 06:14:33,400 --> 06:14:38,520 if we go here with this question Mark so 7736 06:14:36,200 --> 06:14:40,240 this is one approach how we can fix this 7737 06:14:38,520 --> 06:14:43,280 because as far as typescript is 7738 06:14:40,240 --> 06:14:45,478 concerned there's no way to tell whether 7739 06:14:43,280 --> 06:14:49,080 yes for sure we're getting back to 7740 06:14:45,478 --> 06:14:51,160 button unfortunately again this is built 7741 06:14:49,080 --> 06:14:53,638 time this is not 7742 06:14:51,160 --> 06:14:55,760 runtime so typescript is like okay 7743 06:14:53,638 --> 06:14:59,320 you're selecting the element from the 7744 06:14:55,760 --> 06:15:01,200 dumb potentially well this can be wrong 7745 06:14:59,320 --> 06:15:02,798 correct and in that case you're not 7746 06:15:01,200 --> 06:15:06,200 getting back the element you're actually 7747 06:15:02,798 --> 06:15:09,638 getting back null and we need to handle 7748 06:15:06,200 --> 06:15:11,760 that also don't be surprised if in few 7749 06:15:09,638 --> 06:15:12,600 cases and I sign not yes we'll cover 7750 06:15:11,760 --> 06:15:16,280 that 7751 06:15:12,600 --> 06:15:19,478 actually when we talk about the element 7752 06:15:16,280 --> 06:15:21,520 point you might need to do more checking 7753 06:15:19,478 --> 06:15:24,000 you might need to do something like this 7754 06:15:21,520 --> 06:15:26,718 where you go with if button and only 7755 06:15:24,000 --> 06:15:30,920 then run the code so this is one 7756 06:15:26,718 --> 06:15:35,760 approach how we can handle the case 7757 06:15:30,920 --> 06:15:37,520 where element is null and yes every time 7758 06:15:35,760 --> 06:15:40,440 you'll want to do something with the 7759 06:15:37,520 --> 06:15:44,638 element you'll essentially need to first 7760 06:15:40,440 --> 06:15:48,040 check whether the element actually 7761 06:15:44,638 --> 06:15:52,718 exists now another approach to deal with 7762 06:15:48,040 --> 06:15:54,040 the null issue is by setting up the non 7763 06:15:52,718 --> 06:15:57,440 null 7764 06:15:54,040 --> 06:16:01,680 assertion operator so effectively in 7765 06:15:57,440 --> 06:16:05,558 here I'm telling T script yo I know for 7766 06:16:01,680 --> 06:16:07,240 sure that the element exists over here 7767 06:16:05,558 --> 06:16:10,160 and in that case notice I can go here 7768 06:16:07,240 --> 06:16:13,878 with BTN then Dot and then add event 7769 06:16:10,160 --> 06:16:16,718 listener and I don't need to use anymore 7770 06:16:13,878 --> 06:16:18,680 the optional chaining and the same is 7771 06:16:16,718 --> 06:16:21,400 going to work if I'll try to run for 7772 06:16:18,680 --> 06:16:22,638 example disabled on a button I won't 7773 06:16:21,400 --> 06:16:25,558 have to check 7774 06:16:22,638 --> 06:16:27,958 for the element whether it's actually 7775 06:16:25,558 --> 06:16:31,360 present so in here I'm just telling T 7776 06:16:27,958 --> 06:16:34,558 script listen I know a bit more about my 7777 06:16:31,360 --> 06:16:37,280 project than you do and I know for sure 7778 06:16:34,558 --> 06:16:40,320 that the element is there and actually 7779 06:16:37,280 --> 06:16:43,000 if we take a look at the main TS I 7780 06:16:40,320 --> 06:16:45,840 believe it was over here now don't worry 7781 06:16:43,000 --> 06:16:47,920 about the code after essentially they're 7782 06:16:45,840 --> 06:16:50,920 just dynamically inserting the elements 7783 06:16:47,920 --> 06:16:52,638 and all that notice this one over here 7784 06:16:50,920 --> 06:16:56,558 where they go with document query 7785 06:16:52,638 --> 06:17:00,718 selector they grab the development 7786 06:16:56,558 --> 06:17:02,200 they're looking for the ID over here and 7787 06:17:00,718 --> 06:17:04,958 then they just tell typescript Hey 7788 06:17:02,200 --> 06:17:07,240 listen we know that the element is going 7789 06:17:04,958 --> 06:17:09,558 to be there now how do they know that 7790 06:17:07,240 --> 06:17:14,680 well we take a look at the index 7791 06:17:09,558 --> 06:17:16,000 HTML notice this ID it's app correct so 7792 06:17:14,680 --> 06:17:19,360 you have two 7793 06:17:16,000 --> 06:17:22,120 approaches you'll see both of them used 7794 06:17:19,360 --> 06:17:24,878 extensively as far as your project it 7795 06:17:22,120 --> 06:17:27,478 really depends if you like this approach 7796 06:17:24,878 --> 06:17:31,000 where essentially you let typescript 7797 06:17:27,478 --> 06:17:32,760 know just be mindful if the element is 7798 06:17:31,000 --> 06:17:35,320 not going to be there then of course you 7799 06:17:32,760 --> 06:17:37,958 will have runtime errors or or you can 7800 06:17:35,320 --> 06:17:40,160 use the optional chaining and then here 7801 06:17:37,958 --> 06:17:43,760 and there you might need to actually 7802 06:17:40,160 --> 06:17:45,478 check whether the element is present so 7803 06:17:43,760 --> 06:17:49,120 that's the first point that I wanted to 7804 06:17:45,478 --> 06:17:51,400 address now the second one is following 7805 06:17:49,120 --> 06:17:54,080 I use over here this nonn assertion 7806 06:17:51,400 --> 06:17:57,718 operator okay everything is awesome but 7807 06:17:54,080 --> 06:18:00,718 what if I want to run disabled method I 7808 06:17:57,718 --> 06:18:04,120 know that the button has this particular 7809 06:18:00,718 --> 06:18:08,798 method so let's say I go here with BTN 7810 06:18:04,120 --> 06:18:11,000 Dot and then I'm trying to type but I 7811 06:18:08,798 --> 06:18:14,400 can see that it's not present so 7812 06:18:11,000 --> 06:18:17,120 something is off why I have such issue 7813 06:18:14,400 --> 06:18:19,958 well remember we are getting back what 7814 06:18:17,120 --> 06:18:22,840 we're getting back actually an element 7815 06:18:19,958 --> 06:18:24,520 which is the most General base class 7816 06:18:22,840 --> 06:18:28,958 from which all 7817 06:18:24,520 --> 06:18:32,160 elements in a document inherit now this 7818 06:18:28,958 --> 06:18:35,240 one the element has only the methods and 7819 06:18:32,160 --> 06:18:38,240 properties common to all the elements so 7820 06:18:35,240 --> 06:18:40,958 where I'm going with this well there's 7821 06:18:38,240 --> 06:18:44,840 going to be instances where you select 7822 06:18:40,958 --> 06:18:47,080 elements and you need a specific method 7823 06:18:44,840 --> 06:18:50,798 or a property which is only on that 7824 06:18:47,080 --> 06:18:52,958 element and this is the case the button 7825 06:18:50,798 --> 06:18:54,520 I know it has disabled one and I know 7826 06:18:52,958 --> 06:18:58,000 for sure that I definitely want to use 7827 06:18:54,520 --> 06:19:01,160 it but I'm not able to because I'm 7828 06:18:58,000 --> 06:19:03,600 actually getting back the element now 7829 06:19:01,160 --> 06:19:06,000 let's take a look why is that a case 7830 06:19:03,600 --> 06:19:07,400 let's go here to type Ty definition and 7831 06:19:06,000 --> 06:19:10,200 notice the query 7832 06:19:07,400 --> 06:19:13,400 selector so query selector surprise 7833 06:19:10,200 --> 06:19:17,040 surprise is what a 7834 06:19:13,400 --> 06:19:19,080 generic now it's looking for the type 7835 06:19:17,040 --> 06:19:21,798 and notice how there is actually type 7836 06:19:19,080 --> 06:19:25,478 constraint where it says yeah you can 7837 06:19:21,798 --> 06:19:29,160 pass in the element but it needs to 7838 06:19:25,478 --> 06:19:31,440 extend from the element so this is a 7839 06:19:29,160 --> 06:19:33,638 more interesting case for type 7840 06:19:31,440 --> 06:19:36,520 constraint instead of our string and 7841 06:19:33,638 --> 06:19:40,520 number our still EX examples actually 7842 06:19:36,520 --> 06:19:43,958 this limits the element to the element 7843 06:19:40,520 --> 06:19:47,040 and not only that notice how it actually 7844 06:19:43,958 --> 06:19:50,400 provides this one as a default and what 7845 06:19:47,040 --> 06:19:53,718 does that return it actually Returns the 7846 06:19:50,400 --> 06:19:56,160 element we passing over here or it 7847 06:19:53,718 --> 06:19:58,920 returns null so all of the things we 7848 06:19:56,160 --> 06:20:01,200 learn we can now nicely use when we 7849 06:19:58,920 --> 06:20:03,958 inspect the documentation so now it's 7850 06:20:01,200 --> 06:20:06,600 very easy for us to look at this 7851 06:20:03,958 --> 06:20:10,160 documentation and be like okay so we do 7852 06:20:06,600 --> 06:20:13,638 need to provide the element we want to 7853 06:20:10,160 --> 06:20:16,520 select it needs to extend from the 7854 06:20:13,638 --> 06:20:19,520 element and if we're successful we're 7855 06:20:16,520 --> 06:20:24,000 actually going to H it back so now let's 7856 06:20:19,520 --> 06:20:26,320 navigate back and in a query selector we 7857 06:20:24,000 --> 06:20:28,520 want to set up the angle brackets and 7858 06:20:26,320 --> 06:20:31,958 then we want to look for 7859 06:20:28,520 --> 06:20:34,718 HTML button and element so that's the 7860 06:20:31,958 --> 06:20:39,040 one that we want to pass in so now we're 7861 06:20:34,718 --> 06:20:41,718 not getting back the element or null 7862 06:20:39,040 --> 06:20:44,958 we're actually getting back our HTML 7863 06:20:41,718 --> 06:20:49,080 button element so now if we start typing 7864 06:20:44,958 --> 06:20:52,080 notice BTN disabled and we can nicely 7865 06:20:49,080 --> 06:20:55,680 set it equal to true now before I show 7866 06:20:52,080 --> 06:20:57,920 you the type assertion option which is 7867 06:20:55,680 --> 06:21:02,360 another approach you'll see let me just 7868 06:20:57,920 --> 06:21:05,080 remove this non null assertion operator 7869 06:21:02,360 --> 06:21:07,600 and showcase that here and there you'll 7870 06:21:05,080 --> 06:21:10,440 have to actually check for the button so 7871 06:21:07,600 --> 06:21:14,320 remember our second option where every 7872 06:21:10,440 --> 06:21:18,040 time we will use a method for example ad 7873 06:21:14,320 --> 06:21:20,798 event listener we need to add the 7874 06:21:18,040 --> 06:21:24,240 optional training which as a side note 7875 06:21:20,798 --> 06:21:27,000 is already done by typescript in this 7876 06:21:24,240 --> 06:21:29,080 case in this case in the case of 7877 06:21:27,000 --> 06:21:31,320 disabled it's going to be a little bit 7878 06:21:29,080 --> 06:21:33,760 different so if we'll try to do that 7879 06:21:31,320 --> 06:21:35,760 typescript is actually going to complain 7880 06:21:33,760 --> 06:21:38,878 it's going to say hey L and you canot do 7881 06:21:35,760 --> 06:21:42,558 that so instead we'll have to check 7882 06:21:38,878 --> 06:21:45,280 button and then we can set it equal to 7883 06:21:42,558 --> 06:21:47,080 True again this just brings me back to 7884 06:21:45,280 --> 06:21:50,760 the point that we have two options over 7885 06:21:47,080 --> 06:21:53,798 here and we can also always add this 7886 06:21:50,760 --> 06:21:55,920 exclamation point and as far as the 7887 06:21:53,798 --> 06:21:58,760 second option if we don't want to pass 7888 06:21:55,920 --> 06:22:01,160 here in the generic we can also use the 7889 06:21:58,760 --> 06:22:03,440 type assertion so let me just go here 7890 06:22:01,160 --> 06:22:05,240 with disabled and let's set it equal to 7891 06:22:03,440 --> 06:22:08,400 True again 7892 06:22:05,240 --> 06:22:11,160 and let me remove now once I do that 7893 06:22:08,400 --> 06:22:13,798 typescript is going to complain and 7894 06:22:11,160 --> 06:22:17,120 another approach that you'll see is this 7895 06:22:13,798 --> 06:22:18,958 one where we select yes we're getting 7896 06:22:17,120 --> 06:22:22,360 back the element but then we use that 7897 06:22:18,958 --> 06:22:26,400 type assertion and we say yeah actually 7898 06:22:22,360 --> 06:22:30,000 this is going to be HTML and then button 7899 06:22:26,400 --> 06:22:31,478 element now I personally prefer the 7900 06:22:30,000 --> 06:22:34,558 other approach and that's why throughout 7901 06:22:31,478 --> 06:22:36,798 the course you'll see that one but 7902 06:22:34,558 --> 06:22:39,520 definitely don't be surprised if you see 7903 06:22:36,798 --> 06:22:41,360 something like this in the code base 7904 06:22:39,520 --> 06:22:43,280 this is also an option we can take where 7905 06:22:41,360 --> 06:22:45,958 we select the element and then we just 7906 06:22:43,280 --> 06:22:48,958 use the type assertion and you have the 7907 06:22:45,958 --> 06:22:50,718 example in the read me so I guess I can 7908 06:22:48,958 --> 06:22:52,718 just remove pretty much everything over 7909 06:22:50,718 --> 06:22:55,040 here because we'll start everything from 7910 06:22:52,718 --> 06:22:57,638 the scratch and we'll start working with 7911 06:22:55,040 --> 06:23:01,240 our form so you know what let me clean 7912 06:22:57,638 --> 06:23:04,600 this up we also don't need that silly 7913 06:23:01,240 --> 06:23:06,958 button over here since we covered two 7914 06:23:04,600 --> 06:23:09,958 important points and now we're ready to 7915 06:23:06,958 --> 06:23:13,000 move on to the next step okay so let's 7916 06:23:09,958 --> 06:23:16,000 start working on our project and in this 7917 06:23:13,000 --> 06:23:18,478 video I want to select three elements I 7918 06:23:16,000 --> 06:23:22,160 want to select the form I want to select 7919 06:23:18,478 --> 06:23:24,798 the form input as well as the list so 7920 06:23:22,160 --> 06:23:27,080 form input is where we'll get the value 7921 06:23:24,798 --> 06:23:30,280 We'll add the event listener on the form 7922 06:23:27,080 --> 06:23:33,120 so every time we submit the form we will 7923 06:23:30,280 --> 06:23:35,920 grab the input value and also we want to 7924 06:23:33,120 --> 06:23:40,200 get the list since every time we'll 7925 06:23:35,920 --> 06:23:42,440 provide here a task if the value is 7926 06:23:40,200 --> 06:23:46,400 there then we'll basically add it to our 7927 06:23:42,440 --> 06:23:49,080 list and also this is where typescript 7928 06:23:46,400 --> 06:23:51,520 is going to come into play we'll need to 7929 06:23:49,080 --> 06:23:52,958 set up our type so this is something 7930 06:23:51,520 --> 06:23:54,840 interesting every time you work on a 7931 06:23:52,958 --> 06:23:57,440 typescript project yeah pretty much one 7932 06:23:54,840 --> 06:24:01,760 of the first things you'll want to do is 7933 06:23:57,440 --> 06:24:04,680 to start at least setting up the shape 7934 06:24:01,760 --> 06:24:06,280 of your data this is at Le at least in 7935 06:24:04,680 --> 06:24:08,878 my opinion biggest differences between 7936 06:24:06,280 --> 06:24:11,558 the regular project and a typescript one 7937 06:24:08,878 --> 06:24:14,478 you'll always always have the types or 7938 06:24:11,558 --> 06:24:17,400 interfaces somewhere because typescript 7939 06:24:14,478 --> 06:24:20,240 needs to know about the shape of your 7940 06:24:17,400 --> 06:24:23,360 data so let's start I guess 7941 06:24:20,240 --> 06:24:25,440 by selecting all three of them I'm going 7942 06:24:23,360 --> 06:24:28,718 to go with task form now I'm not going 7943 06:24:25,440 --> 06:24:30,798 to use the nonnull assertion 7944 06:24:28,718 --> 06:24:33,200 operator throughout the project I'm just 7945 06:24:30,798 --> 06:24:35,280 going to use that optional chaining so 7946 06:24:33,200 --> 06:24:38,798 we're going to go here with do docent 7947 06:24:35,280 --> 06:24:40,520 then query selector I do want to set up 7948 06:24:38,798 --> 06:24:44,000 the angle brackets and I'm just going to 7949 06:24:40,520 --> 06:24:46,478 say that this will be a HTML form 7950 06:24:44,000 --> 06:24:50,080 element or 7951 06:24:46,478 --> 06:24:53,280 no then let's go with the class and 7952 06:24:50,080 --> 06:24:56,840 we're looking for the form and I believe 7953 06:24:53,280 --> 06:24:59,520 I forgot to mention you see there's no 7954 06:24:56,840 --> 06:25:01,440 way for typescript to know that this 7955 06:24:59,520 --> 06:25:04,360 definitely is a form that's why we need 7956 06:25:01,440 --> 06:25:07,558 to provide over here this form element 7957 06:25:04,360 --> 06:25:09,840 so it cannot magically go to the HTML 7958 06:25:07,558 --> 06:25:11,600 and figure out that since we're using 7959 06:25:09,840 --> 06:25:13,680 the form class then for sure we're 7960 06:25:11,600 --> 06:25:17,280 selecting the element which is actually 7961 06:25:13,680 --> 06:25:19,200 a form that's why we get back this 7962 06:25:17,280 --> 06:25:23,040 generic element and that's why we need 7963 06:25:19,200 --> 06:25:25,040 to use the generic to provide what is 7964 06:25:23,040 --> 06:25:28,280 the element we're actually selecting 7965 06:25:25,040 --> 06:25:30,520 hopefully that is clear const form input 7966 06:25:28,280 --> 06:25:32,558 is going to be the next one we're going 7967 06:25:30,520 --> 06:25:36,360 to go with document query 7968 06:25:32,558 --> 06:25:40,240 selector then will provide the 7969 06:25:36,360 --> 06:25:43,360 HTML input element so this is the 7970 06:25:40,240 --> 06:25:47,320 element I'm looking for input element 7971 06:25:43,360 --> 06:25:48,760 okay good and then the class is Dot and 7972 06:25:47,320 --> 06:25:51,558 then 7973 06:25:48,760 --> 06:25:55,520 form input 7974 06:25:51,558 --> 06:25:57,320 now I think I can make this one so 7975 06:25:55,520 --> 06:25:58,920 everything is in one line I'm pretty 7976 06:25:57,320 --> 06:26:02,280 sure we can see everything that's 7977 06:25:58,920 --> 06:26:04,878 happening I mean you know what no let me 7978 06:26:02,280 --> 06:26:10,120 make it like this and then 7979 06:26:04,878 --> 06:26:12,760 Also let's go with const task list 7980 06:26:10,120 --> 06:26:15,320 element and this one will be equal to 7981 06:26:12,760 --> 06:26:18,558 document then query 7982 06:26:15,320 --> 06:26:22,240 selector let me add the angle brackets 7983 06:26:18,558 --> 06:26:26,000 as far as the class we're looking for 7984 06:26:22,240 --> 06:26:30,840 list and over here we want 7985 06:26:26,000 --> 06:26:33,200 HTML ul and list element and my 7986 06:26:30,840 --> 06:26:35,520 apologies I'm just going to be kind of 7987 06:26:33,200 --> 06:26:37,440 randomly moving the this stuff over here 7988 06:26:35,520 --> 06:26:41,200 don't worry we shouldn't have issues 7989 06:26:37,440 --> 06:26:45,478 with the later code it's just these 7990 06:26:41,200 --> 06:26:48,920 select lines then let's create the type 7991 06:26:45,478 --> 06:26:50,478 and effectively we will have a task with 7992 06:26:48,920 --> 06:26:53,638 description which is going to be a 7993 06:26:50,478 --> 06:26:56,520 string and is completed which is going 7994 06:26:53,638 --> 06:26:58,040 to be a bullan so let's set up this one 7995 06:26:56,520 --> 06:27:03,360 type 7996 06:26:58,040 --> 06:27:06,040 task then let's go with the description 7997 06:27:03,360 --> 06:27:07,958 set it equal to to a string and yes as a 7998 06:27:06,040 --> 06:27:09,400 sign not you can set this one up as 7999 06:27:07,958 --> 06:27:12,520 interface as 8000 06:27:09,400 --> 06:27:14,920 well it's just my preference to go with 8001 06:27:12,520 --> 06:27:16,718 typ aliases and we're going to go with 8002 06:27:14,920 --> 06:27:20,478 Boolean and 8003 06:27:16,718 --> 06:27:23,558 then I'm going to set up an array of 8004 06:27:20,478 --> 06:27:26,718 tasks so this is where we'll store the 8005 06:27:23,558 --> 06:27:28,760 tasks and as a sign note eventually 8006 06:27:26,718 --> 06:27:32,478 we'll grab these tasks from the local 8007 06:27:28,760 --> 06:27:34,638 storage as well so think of this as the 8008 06:27:32,478 --> 06:27:36,558 state for our applications so we're just 8009 06:27:34,638 --> 06:27:39,760 going to go with tasks it's actually 8010 06:27:36,558 --> 06:27:42,760 going to be equal to an array of 8011 06:27:39,760 --> 06:27:46,040 tasks and for now it's just going to be 8012 06:27:42,760 --> 06:27:48,718 empty array okay and now let's start 8013 06:27:46,040 --> 06:27:51,440 setting up the functionality so we want 8014 06:27:48,718 --> 06:27:55,040 to add event listener on a form we're 8015 06:27:51,440 --> 06:27:57,840 going to be listening for submit events 8016 06:27:55,040 --> 06:27:59,360 we do want to prevent the default so we 8017 06:27:57,840 --> 06:28:02,520 want to handle everything with 8018 06:27:59,360 --> 06:28:05,440 JavaScript and after that inside of the 8019 06:28:02,520 --> 06:28:09,240 Callback function we want to grab the 8020 06:28:05,440 --> 06:28:11,120 form input value if some value is 8021 06:28:09,240 --> 06:28:14,160 provided will actually wipe everything 8022 06:28:11,120 --> 06:28:16,200 clean and return and of course before 8023 06:28:14,160 --> 06:28:18,718 that we'll have to do quite a bit of 8024 06:28:16,200 --> 06:28:23,440 logic and if there is no value then 8025 06:28:18,718 --> 06:28:25,160 we'll just set up the alert so let's set 8026 06:28:23,440 --> 06:28:28,878 everything up we're looking for task 8027 06:28:25,160 --> 06:28:30,878 form again we can rely on typescript or 8028 06:28:28,878 --> 06:28:33,718 we can manually add this optional 8029 06:28:30,878 --> 06:28:36,000 chaining operator ourselves then we're 8030 06:28:33,718 --> 06:28:38,400 looking for ad event listener like I 8031 06:28:36,000 --> 06:28:42,280 said we're looking for submit and again 8032 06:28:38,400 --> 06:28:46,760 notice how helpful typescript is so 8033 06:28:42,280 --> 06:28:50,680 right away provides all the possible 8034 06:28:46,760 --> 06:28:55,520 events that we can use over here for now 8035 06:28:50,680 --> 06:28:57,400 we will provide call back directly but 8036 06:28:55,520 --> 06:28:59,958 in the following video I'll show you the 8037 06:28:57,400 --> 06:29:03,080 Goa we need to be aware of remember we 8038 06:28:59,958 --> 06:29:05,400 have access to the event now if we hover 8039 06:29:03,080 --> 06:29:07,920 over we can see the that actually it has 8040 06:29:05,400 --> 06:29:10,080 a type of submit event something 8041 06:29:07,920 --> 06:29:12,478 important we need to keep in mind and 8042 06:29:10,080 --> 06:29:15,200 then let's go with event we're looking 8043 06:29:12,478 --> 06:29:17,160 for prevent default and we just invoke 8044 06:29:15,200 --> 06:29:20,200 it so now we'll handle everything with 8045 06:29:17,160 --> 06:29:22,600 JavaScript after that let's grab the 8046 06:29:20,200 --> 06:29:24,718 form input and more specifically we're 8047 06:29:22,600 --> 06:29:28,200 looking for the value property which 8048 06:29:24,718 --> 06:29:31,680 again is on the form input I'm going to 8049 06:29:28,200 --> 06:29:34,878 give it a name of task description let's 8050 06:29:31,680 --> 06:29:38,760 set it equal to form input 8051 06:29:34,878 --> 06:29:42,240 same deal we don't have this non null 8052 06:29:38,760 --> 06:29:44,920 assertion operator so again we can just 8053 06:29:42,240 --> 06:29:47,280 rely I guess on typescript or in this 8054 06:29:44,920 --> 06:29:49,638 case it simply complains so we'll just 8055 06:29:47,280 --> 06:29:51,840 need to add this optional chaining so 8056 06:29:49,638 --> 06:29:54,360 now we're grabbing the form input value 8057 06:29:51,840 --> 06:29:57,600 if we have something in there if the 8058 06:29:54,360 --> 06:30:02,920 user has provided something then we'll 8059 06:29:57,600 --> 06:30:04,520 do a list of things if not then go with 8060 06:30:02,920 --> 06:30:07,958 alert and 8061 06:30:04,520 --> 06:30:10,320 we'll just say please enter a task 8062 06:30:07,958 --> 06:30:13,400 description again it's not going to be 8063 06:30:10,320 --> 06:30:15,600 the most complex application ever let me 8064 06:30:13,400 --> 06:30:18,600 refresh and notice if I don't provide 8065 06:30:15,600 --> 06:30:21,638 anything essentially I just get this 8066 06:30:18,600 --> 06:30:23,200 alert box now if we are successful again 8067 06:30:21,638 --> 06:30:24,400 there's going to be a list of things 8068 06:30:23,200 --> 06:30:29,400 that we will 8069 06:30:24,400 --> 06:30:31,320 do but once we're done adding a task to 8070 06:30:29,400 --> 06:30:34,878 the list once we're done rendering the 8071 06:30:31,320 --> 06:30:38,240 tasks and updating the local 8072 06:30:34,878 --> 06:30:40,760 storage at the very end regardless we 8073 06:30:38,240 --> 06:30:43,160 want to set the form input value back to 8074 06:30:40,760 --> 06:30:47,160 an empty string and we want to return 8075 06:30:43,160 --> 06:30:50,040 since I only want to hit this alert if 8076 06:30:47,160 --> 06:30:51,840 no value has been provided now also for 8077 06:30:50,040 --> 06:30:55,360 time being you know what let's just log 8078 06:30:51,840 --> 06:30:57,600 here let's just say task description so 8079 06:30:55,360 --> 06:31:01,000 if I provide some random value check it 8080 06:30:57,600 --> 06:31:03,718 out I have it here in the console if not 8081 06:31:01,000 --> 06:31:05,478 again we have this alert box and what 8082 06:31:03,718 --> 06:31:08,878 this in place now we can move on to the 8083 06:31:05,478 --> 06:31:13,760 next task okay and before we set up the 8084 06:31:08,878 --> 06:31:17,680 logic to add new task to our list let me 8085 06:31:13,760 --> 06:31:21,000 showcase the event object gotcha and as 8086 06:31:17,680 --> 06:31:24,680 I know this is something you'll also see 8087 06:31:21,000 --> 06:31:26,958 in react section of the course so pretty 8088 06:31:24,680 --> 06:31:29,958 much the main idea is going to be 8089 06:31:26,958 --> 06:31:33,360 exactly the same and it is following you 8090 06:31:29,958 --> 06:31:35,558 see if we provide over here call back 8091 06:31:33,360 --> 06:31:37,760 function directly everything's awesome 8092 06:31:35,558 --> 06:31:40,520 typescript knows that yeah it's actually 8093 06:31:37,760 --> 06:31:44,638 a submit event but it's not going to be 8094 06:31:40,520 --> 06:31:47,798 the case if we set up the reference so 8095 06:31:44,638 --> 06:31:49,558 can go here with function create task as 8096 06:31:47,798 --> 06:31:53,440 a say not I don't think I'm going to 8097 06:31:49,558 --> 06:31:55,638 keep it so I'll just set it up and 8098 06:31:53,440 --> 06:31:57,520 eventually remove it but this is totally 8099 06:31:55,638 --> 06:32:01,160 up to you of course you can also keep 8100 06:31:57,520 --> 06:32:05,320 this code if we're going to go over here 8101 06:32:01,160 --> 06:32:09,200 and basically take all of this 8102 06:32:05,320 --> 06:32:11,958 logic like so everything should work 8103 06:32:09,200 --> 06:32:15,080 correct we have access to the event and 8104 06:32:11,958 --> 06:32:17,798 now we can just replace it well let's 8105 06:32:15,080 --> 06:32:21,280 see let's go with create task and check 8106 06:32:17,798 --> 06:32:22,718 it out so typescript essentially saying 8107 06:32:21,280 --> 06:32:26,240 that event right 8108 06:32:22,718 --> 06:32:30,160 now has the type of any so this is the 8109 06:32:26,240 --> 06:32:32,558 case where if we're setting up 8110 06:32:30,160 --> 06:32:35,798 functions and providing them as 8111 06:32:32,558 --> 06:32:39,080 references here when we're listening for 8112 06:32:35,798 --> 06:32:42,200 events then yes we'll explicitly need to 8113 06:32:39,080 --> 06:32:44,680 provide what is the type so in this case 8114 06:32:42,200 --> 06:32:46,638 we're looking for submit event and once 8115 06:32:44,680 --> 06:32:50,200 we do that again our logic is going to 8116 06:32:46,638 --> 06:32:51,920 work I can provide some value over here 8117 06:32:50,200 --> 06:32:55,200 and everything is awesome and the same 8118 06:32:51,920 --> 06:32:58,120 is going to work with the empty value 8119 06:32:55,200 --> 06:33:00,160 and yes the same you'll have to do in 8120 06:32:58,120 --> 06:33:03,120 react now in there the code is going to 8121 06:33:00,160 --> 06:33:05,638 be more complex but the idea is the same 8122 06:33:03,120 --> 06:33:08,120 if you're listening for some kind of 8123 06:33:05,638 --> 06:33:10,760 event and you set up the logic directly 8124 06:33:08,120 --> 06:33:13,478 in a callback function yes event will 8125 06:33:10,760 --> 06:33:16,760 have that correct type straight out of 8126 06:33:13,478 --> 06:33:18,478 the box but if you're setting up a 8127 06:33:16,760 --> 06:33:22,120 function which you're then using as a 8128 06:33:18,478 --> 06:33:25,200 reference it's not going to be the case 8129 06:33:22,120 --> 06:33:28,680 something important to remember okay and 8130 06:33:25,200 --> 06:33:31,600 up next let's see how we can add task to 8131 06:33:28,680 --> 06:33:34,360 our list now in this video we're not 8132 06:33:31,600 --> 06:33:37,680 going to set up the logic to render any 8133 06:33:34,360 --> 06:33:39,958 tasks on the screen that is coming up so 8134 06:33:37,680 --> 06:33:44,040 most likely we'll do right after we are 8135 06:33:39,958 --> 06:33:47,398 able to add task to our list and in here 8136 06:33:44,040 --> 06:33:49,920 I want to First create function I'm 8137 06:33:47,398 --> 06:33:52,600 going to call this add task and as far 8138 06:33:49,920 --> 06:33:55,718 as the parameter it's going to be a task 8139 06:33:52,600 --> 06:33:57,760 but this is where we set up the type so 8140 06:33:55,718 --> 06:34:00,558 we definitely say that we need to 8141 06:33:57,760 --> 06:34:03,240 provide the task and we're not going to 8142 06:34:00,558 --> 06:34:06,080 return anything from this function and 8143 06:34:03,240 --> 06:34:09,920 as far as logic we want to go with task 8144 06:34:06,080 --> 06:34:13,478 I'm sorry tasks and then push and we 8145 06:34:09,920 --> 06:34:15,360 want to add our task so whatever we 8146 06:34:13,478 --> 06:34:19,120 provide over here then we want to 8147 06:34:15,360 --> 06:34:21,478 navigate back to our if block and you 8148 06:34:19,120 --> 06:34:24,840 know what let's add three comments we'll 8149 06:34:21,478 --> 06:34:28,398 say add task to list then second one 8150 06:34:24,840 --> 06:34:32,280 will be render tasks and then we also 8151 06:34:28,398 --> 06:34:35,878 want to update the local storage so all 8152 06:34:32,280 --> 06:34:39,160 of that is coming up for now inside of 8153 06:34:35,878 --> 06:34:42,638 the if block I want to construct a task 8154 06:34:39,160 --> 06:34:46,878 so if I have a text I want to go cons 8155 06:34:42,638 --> 06:34:50,200 task is equal again to my type my task 8156 06:34:46,878 --> 06:34:52,920 type and I want to set it equal to 8157 06:34:50,200 --> 06:34:55,558 description this will be equal to task 8158 06:34:52,920 --> 06:34:58,638 description so this will be my string 8159 06:34:55,558 --> 06:35:01,520 and then as far as the completed one we 8160 06:34:58,638 --> 06:35:03,718 will hard code so go with is completed 8161 06:35:01,520 --> 06:35:07,240 and we'll always always set it equal to 8162 06:35:03,718 --> 06:35:11,320 false so once we create task now we can 8163 06:35:07,240 --> 06:35:14,360 invoke add task and pass in a task and 8164 06:35:11,320 --> 06:35:16,840 since I have the correct type everything 8165 06:35:14,360 --> 06:35:20,878 is good script is not complaining and we 8166 06:35:16,840 --> 06:35:23,798 can actually log the tasks here so let's 8167 06:35:20,878 --> 06:35:27,080 go here with tasks let's add something 8168 06:35:23,798 --> 06:35:30,320 so let's say first task and check it out 8169 06:35:27,080 --> 06:35:32,360 now I have my array with my first task 8170 06:35:30,320 --> 06:35:35,200 if I go with second task it's not going 8171 06:35:32,360 --> 06:35:37,840 to be surprised if we have two items in 8172 06:35:35,200 --> 06:35:41,280 the array and with this in place now we 8173 06:35:37,840 --> 06:35:45,638 can move on to the next step okay and up 8174 06:35:41,280 --> 06:35:48,638 next let's render the task on the screen 8175 06:35:45,638 --> 06:35:52,160 now we will call actually render task in 8176 06:35:48,638 --> 06:35:55,120 two places so for now we'll only call it 8177 06:35:52,160 --> 06:35:57,478 here in a task description block so if 8178 06:35:55,120 --> 06:36:02,000 we're successful we'll just render it on 8179 06:35:57,478 --> 06:36:04,478 a screen but also once we're able to 8180 06:36:02,000 --> 06:36:07,360 grab the tasks from the Lo local 8181 06:36:04,478 --> 06:36:09,360 storage will iterate over and render all 8182 06:36:07,360 --> 06:36:12,638 the tasks on the screen so think of it 8183 06:36:09,360 --> 06:36:15,958 this way not only we want to render the 8184 06:36:12,638 --> 06:36:18,718 task once we add new one to the list but 8185 06:36:15,958 --> 06:36:21,680 also when the user first navigates to 8186 06:36:18,718 --> 06:36:23,680 the page or we refresh we want to grab 8187 06:36:21,680 --> 06:36:25,878 the tasks from the local storage where 8188 06:36:23,680 --> 06:36:30,398 they are nicely saved and we want to 8189 06:36:25,878 --> 06:36:32,600 render them on the screen as far as the 8190 06:36:30,398 --> 06:36:35,000 function first we just set up our 8191 06:36:32,600 --> 06:36:37,760 keyword I'm going to call this render 8192 06:36:35,000 --> 06:36:41,840 task and again it's looking for a 8193 06:36:37,760 --> 06:36:45,398 parameter but parameter has a specific 8194 06:36:41,840 --> 06:36:48,120 type and it's equal to task then inside 8195 06:36:45,398 --> 06:36:51,120 of the function first I want to create 8196 06:36:48,120 --> 06:36:53,240 the element and in order to do that I 8197 06:36:51,120 --> 06:36:55,558 need to come up with the name in my case 8198 06:36:53,240 --> 06:36:58,040 I'm going to call this task element then 8199 06:36:55,558 --> 06:36:59,280 we go with our document it has a method 8200 06:36:58,040 --> 06:37:01,200 of 8201 06:36:59,280 --> 06:37:03,840 create 8202 06:37:01,200 --> 06:37:07,120 element and we just need to provide what 8203 06:37:03,840 --> 06:37:09,520 element we want to create so either we 8204 06:37:07,120 --> 06:37:12,600 scroll down or we just type Li and now 8205 06:37:09,520 --> 06:37:14,160 we successfully create the list element 8206 06:37:12,600 --> 06:37:16,478 then we also want to add the text 8207 06:37:14,160 --> 06:37:19,160 content basically it's going to be the 8208 06:37:16,478 --> 06:37:21,040 value for our task description the 8209 06:37:19,160 --> 06:37:25,478 property is text content and we set it 8210 06:37:21,040 --> 06:37:26,558 equal to task so whatever it's passed in 8211 06:37:25,478 --> 06:37:28,878 and 8212 06:37:26,558 --> 06:37:31,878 description and then at the very end 8213 06:37:28,878 --> 06:37:34,240 remember we have our list somewhere here 8214 06:37:31,878 --> 06:37:37,478 that's the one if 8215 06:37:34,240 --> 06:37:40,080 it exists if it's not null then we want 8216 06:37:37,478 --> 06:37:46,280 to add this element to the list so we go 8217 06:37:40,080 --> 06:37:49,120 with task list element up in child again 8218 06:37:46,280 --> 06:37:51,520 typescript Auto formats for us and just 8219 06:37:49,120 --> 06:37:54,478 add this optional chaining and then we 8220 06:37:51,520 --> 06:37:56,958 pass in our task element so now let's 8221 06:37:54,478 --> 06:37:58,320 navigate up where we have render tasks 8222 06:37:56,958 --> 06:38:01,040 and we want to go here with this 8223 06:37:58,320 --> 06:38:04,000 function pass in a task and now let's 8224 06:38:01,040 --> 06:38:08,558 try it out if we're successful 8225 06:38:04,000 --> 06:38:11,478 we should see the task on a screen so 8226 06:38:08,558 --> 06:38:14,040 let's again go with first task and check 8227 06:38:11,478 --> 06:38:16,600 it out we successfully created the 8228 06:38:14,040 --> 06:38:19,680 element on the Fly same is going to be 8229 06:38:16,600 --> 06:38:22,120 with second task and all the tasks we 8230 06:38:19,680 --> 06:38:24,878 add to the list all right and I think we 8231 06:38:22,120 --> 06:38:27,600 are at the point where we can add local 8232 06:38:24,878 --> 06:38:30,320 storage to our project because it's nice 8233 06:38:27,600 --> 06:38:34,718 that we can render all the tasks we add 8234 06:38:30,320 --> 06:38:36,878 to our array however if we na away or 8235 06:38:34,718 --> 06:38:39,120 just refresh notice how essentially we 8236 06:38:36,878 --> 06:38:41,558 lose all the tasks since they're just 8237 06:38:39,120 --> 06:38:43,478 saved in the memory now before we do 8238 06:38:41,558 --> 06:38:46,040 anything I actually want to make sure I 8239 06:38:43,478 --> 06:38:48,600 have nothing in a local storage 8240 06:38:46,040 --> 06:38:50,718 otherwise I mean I might get some weird 8241 06:38:48,600 --> 06:38:53,798 bugs so let me navigate to my local 8242 06:38:50,718 --> 06:38:56,080 storage and just clean everything again 8243 06:38:53,798 --> 06:38:59,520 Let me refresh I have nothing in there 8244 06:38:56,080 --> 06:39:02,120 so now again I can make this one 8245 06:38:59,520 --> 06:39:04,120 smaller and as I said I'm looking for 8246 06:39:02,120 --> 06:39:05,760 the console okay good 8247 06:39:04,120 --> 06:39:08,878 and now as far as the functionality we 8248 06:39:05,760 --> 06:39:11,878 want to create a function called update 8249 06:39:08,878 --> 06:39:15,558 storage and in there we will use the 8250 06:39:11,878 --> 06:39:18,000 local storage more specifically set item 8251 06:39:15,558 --> 06:39:20,798 method and we'll need to provide the 8252 06:39:18,000 --> 06:39:23,040 value and just stringify our current 8253 06:39:20,798 --> 06:39:26,240 tasks so in my case I'm going to do it 8254 06:39:23,040 --> 06:39:30,040 over here so we go with function let's 8255 06:39:26,240 --> 06:39:32,680 call this update storage and as a yes 8256 06:39:30,040 --> 06:39:33,878 we'll call it in multiple places so 8257 06:39:32,680 --> 06:39:36,878 update storage 8258 06:39:33,878 --> 06:39:39,520 as far as the parameters well we're not 8259 06:39:36,878 --> 06:39:40,878 going to pass anything in and as far as 8260 06:39:39,520 --> 06:39:43,638 the functionality we're just going to go 8261 06:39:40,878 --> 06:39:46,520 with local storage then let's go with 8262 06:39:43,638 --> 06:39:50,478 set item as far as mining it's going to 8263 06:39:46,520 --> 06:39:54,240 be tasks then comma and then we're going 8264 06:39:50,478 --> 06:39:58,040 to stringify the tasks as a sign note 8265 06:39:54,240 --> 06:40:00,680 yes pretty much every time we will add 8266 06:39:58,040 --> 06:40:04,520 the task to our list 8267 06:40:00,680 --> 06:40:07,478 or when we click on the checkbox and we 8268 06:40:04,520 --> 06:40:11,040 edit the task when we set it equal to 8269 06:40:07,478 --> 06:40:13,558 completed or we uncheck that yes 8270 06:40:11,040 --> 06:40:16,000 effectively we will overwrite all of the 8271 06:40:13,558 --> 06:40:18,920 values that we have in the local storage 8272 06:40:16,000 --> 06:40:21,320 with this new one so grab the tasks and 8273 06:40:18,920 --> 06:40:24,160 overwrite with the current 8274 06:40:21,320 --> 06:40:26,638 values now when do we want to call it 8275 06:40:24,160 --> 06:40:30,558 well for starters we want to do it over 8276 06:40:26,638 --> 06:40:33,040 here so we're going to go with update 8277 06:40:30,558 --> 06:40:36,000 storage let's invoke that and you know 8278 06:40:33,040 --> 06:40:38,520 what I will actually open up 8279 06:40:36,000 --> 06:40:42,200 a bigger window I think it's going to be 8280 06:40:38,520 --> 06:40:44,760 easier to see so that's my 8281 06:40:42,200 --> 06:40:48,160 tasks let me open 8282 06:40:44,760 --> 06:40:51,558 up application okay good and then let's 8283 06:40:48,160 --> 06:40:54,878 go with first task again Yep this is 8284 06:40:51,558 --> 06:40:58,240 what we have over here notice tasks and 8285 06:40:54,878 --> 06:41:00,440 I have a d and if I'm going to go with 8286 06:40:58,240 --> 06:41:03,440 second task now this is going to get 8287 06:41:00,440 --> 06:41:07,000 added to the array so this should cover 8288 06:41:03,440 --> 06:41:09,520 when we add the task to the array so 8289 06:41:07,000 --> 06:41:12,520 this should cover the form submission 8290 06:41:09,520 --> 06:41:14,920 now we also want to handle when we load 8291 06:41:12,520 --> 06:41:17,200 the page so at the moment we always set 8292 06:41:14,920 --> 06:41:19,718 it equal to an empty array but we want 8293 06:41:17,200 --> 06:41:22,320 to change that and order to do that we 8294 06:41:19,718 --> 06:41:25,120 will come up with a function called load 8295 06:41:22,320 --> 06:41:26,398 tasks now you can set it up with all of 8296 06:41:25,120 --> 06:41:28,600 the functions but I'm just going to do 8297 06:41:26,398 --> 06:41:31,920 it over here since in my opinion it's 8298 06:41:28,600 --> 06:41:34,440 going to be easier to see the entire 8299 06:41:31,920 --> 06:41:36,798 functionality we we are looking for a 8300 06:41:34,440 --> 06:41:40,558 return and from this function we want to 8301 06:41:36,798 --> 06:41:42,840 return an array of tasks so an array and 8302 06:41:40,558 --> 06:41:43,680 type is going to be the task now inside 8303 06:41:42,840 --> 06:41:47,360 of the 8304 06:41:43,680 --> 06:41:49,398 function first let's grab the tasks from 8305 06:41:47,360 --> 06:41:52,878 the local storage so we're going to set 8306 06:41:49,398 --> 06:41:56,440 it equal to stored tasks that is equal 8307 06:41:52,878 --> 06:42:00,200 to local or I'm sorry not load local 8308 06:41:56,440 --> 06:42:03,040 storage it has a method of get item and 8309 06:42:00,200 --> 06:42:06,920 we're looking for the tasks now here's 8310 06:42:03,040 --> 06:42:09,478 the kicker just like previously this can 8311 06:42:06,920 --> 06:42:11,840 be a null and we definitely need to 8312 06:42:09,478 --> 06:42:14,440 handle that so in here when we set up 8313 06:42:11,840 --> 06:42:18,920 the return we're not just going to go 8314 06:42:14,440 --> 06:42:22,240 with store tasks actually we will set up 8315 06:42:18,920 --> 06:42:25,360 optional training and we'll say hey if 8316 06:42:22,240 --> 06:42:27,520 there's a value okay awesome parse it so 8317 06:42:25,360 --> 06:42:30,958 we're going to go with store tasks 8318 06:42:27,520 --> 06:42:32,760 because keep in mind we cannot store 8319 06:42:30,958 --> 06:42:35,040 essentially objects in the local storage 8320 06:42:32,760 --> 06:42:37,920 we can only Only Store strings that's 8321 06:42:35,040 --> 06:42:39,958 why we went with Json string ify and now 8322 06:42:37,920 --> 06:42:42,080 we want to parse that value but we only 8323 06:42:39,958 --> 06:42:44,680 want to parse it if we get something 8324 06:42:42,080 --> 06:42:47,920 back correct if not well then we'll just 8325 06:42:44,680 --> 06:42:50,840 return empty array and now where we have 8326 06:42:47,920 --> 06:42:53,600 the tasks we'll actually go with load 8327 06:42:50,840 --> 06:42:57,280 tasks we will invoke it and since 8328 06:42:53,600 --> 06:42:59,798 potentially now we can get some tasks 8329 06:42:57,280 --> 06:43:04,600 from the local storage we also want to 8330 06:42:59,798 --> 06:43:07,638 iterate over and render the tasks so for 8331 06:43:04,600 --> 06:43:11,600 every task we have in the local storage 8332 06:43:07,638 --> 06:43:14,958 in our array we want to call render task 8333 06:43:11,600 --> 06:43:18,958 method so I mean somewhere after the 8334 06:43:14,958 --> 06:43:22,760 tasks we want to go with the name so 8335 06:43:18,958 --> 06:43:25,240 tasks then dot we're looking for for 8336 06:43:22,760 --> 06:43:28,360 each and we simply can pass in the 8337 06:43:25,240 --> 06:43:30,558 render task now let me showcase 8338 06:43:28,360 --> 06:43:34,040 that 8339 06:43:30,558 --> 06:43:36,638 essentially it's pretty much the 8340 06:43:34,040 --> 06:43:39,160 same as we have over here so notice we 8341 06:43:36,638 --> 06:43:41,798 can either go tasks and for each and 8342 06:43:39,160 --> 06:43:45,080 then pass in the call back function or 8343 06:43:41,798 --> 06:43:47,040 you simply provide the reference to 8344 06:43:45,080 --> 06:43:48,840 render tasks and now let me navigate to 8345 06:43:47,040 --> 06:43:51,360 the browser and if everything is correct 8346 06:43:48,840 --> 06:43:54,558 whatever tasks we have in local storage 8347 06:43:51,360 --> 06:43:57,320 once we refresh we'll see them in the 8348 06:43:54,558 --> 06:44:00,160 browser as well all right and at the end 8349 06:43:57,320 --> 06:44:04,478 of the project let's also add the 8350 06:44:00,160 --> 06:44:06,600 checkbox to our list item and every time 8351 06:44:04,478 --> 06:44:09,718 we click on checkbox we'll be able to 8352 06:44:06,600 --> 06:44:12,080 control this is completed so by default 8353 06:44:09,718 --> 06:44:14,638 it's false but then once we click on a 8354 06:44:12,080 --> 06:44:17,398 checkbox we can actually toggle that and 8355 06:44:14,638 --> 06:44:20,520 I think I'll try to cover the part eight 8356 06:44:17,398 --> 06:44:22,958 and part nine in this video hopefully 8357 06:44:20,520 --> 06:44:26,840 it's not going to be too long STS we 8358 06:44:22,958 --> 06:44:29,360 want to navigate to render task and at 8359 06:44:26,840 --> 06:44:32,840 the moment we're just adding the list 8360 06:44:29,360 --> 06:44:37,160 item to our list but we also want to add 8361 06:44:32,840 --> 06:44:38,680 a checkbox to the item and then add that 8362 06:44:37,160 --> 06:44:42,160 item to the list hopefully that makes 8363 06:44:38,680 --> 06:44:46,120 sense so let's go here with checkbox we 8364 06:44:42,160 --> 06:44:48,558 have const task checkbox that will be my 8365 06:44:46,120 --> 06:44:50,478 variable we're looking for document then 8366 06:44:48,558 --> 06:44:53,638 dot then we want to create the element 8367 06:44:50,478 --> 06:44:55,398 and this is going to be the input so not 8368 06:44:53,638 --> 06:44:58,000 list item we're looking for the input 8369 06:44:55,398 --> 06:45:01,600 and then we'll set the type to be equal 8370 06:44:58,000 --> 06:45:03,920 to a text box so we're looking for task 8371 06:45:01,600 --> 06:45:07,798 checkbox type 8372 06:45:03,920 --> 06:45:11,760 and we set it equal to checkbox then 8373 06:45:07,798 --> 06:45:13,360 let's set check equal to is completed 8374 06:45:11,760 --> 06:45:16,638 Again by default it's going to be false 8375 06:45:13,360 --> 06:45:19,398 so let's go with task checkbox we have 8376 06:45:16,638 --> 06:45:21,440 the checked property which controls that 8377 06:45:19,398 --> 06:45:25,840 and we want to set it equal to task and 8378 06:45:21,440 --> 06:45:28,520 then is completed and now before we add 8379 06:45:25,840 --> 06:45:30,600 task element to our list we also want to 8380 06:45:28,520 --> 06:45:34,200 go with task 8381 06:45:30,600 --> 06:45:40,280 element so task element then 8382 06:45:34,200 --> 06:45:43,040 upend child and we want to pass in task 8383 06:45:40,280 --> 06:45:44,840 checkbox and check it out now we have 8384 06:45:43,040 --> 06:45:47,200 the checkbox at the moment it's not 8385 06:45:44,840 --> 06:45:50,280 doing anything since we don't have any 8386 06:45:47,200 --> 06:45:53,638 functionality but we'll change that 8387 06:45:50,280 --> 06:45:56,958 right after we create the checkbox I 8388 06:45:53,638 --> 06:45:58,320 also want to add the event listener and 8389 06:45:56,958 --> 06:46:01,878 the event we're going to be listening 8390 06:45:58,320 --> 06:46:04,120 for is going to be change so before we 8391 06:46:01,878 --> 06:46:05,558 add it to a task element element let's 8392 06:46:04,120 --> 06:46:07,520 call 8393 06:46:05,558 --> 06:46:12,280 toggle 8394 06:46:07,520 --> 06:46:15,360 checkbox we're looking for our task 8395 06:46:12,280 --> 06:46:18,040 checkbox that's the element then add 8396 06:46:15,360 --> 06:46:20,600 event listener we're going to be 8397 06:46:18,040 --> 06:46:23,558 listening for change event we're not 8398 06:46:20,600 --> 06:46:26,080 going to access the event and as far as 8399 06:46:23,558 --> 06:46:29,120 the functionality we want to go with 8400 06:46:26,080 --> 06:46:30,638 task and then is completed set it to the 8401 06:46:29,120 --> 06:46:32,398 opposite since we're going to be 8402 06:46:30,638 --> 06:46:35,718 toggling and the way we do that is 8403 06:46:32,398 --> 06:46:40,160 following so we go with exclamation 8404 06:46:35,718 --> 06:46:42,878 point and task is completed so we set it 8405 06:46:40,160 --> 06:46:46,600 equal to the opposite and then we call 8406 06:46:42,878 --> 06:46:48,080 update storage so now once I click and 8407 06:46:46,600 --> 06:46:50,798 you know what let me showcase that on 8408 06:46:48,080 --> 06:46:53,000 big screen again let me start from the 8409 06:46:50,798 --> 06:46:56,000 scratch just so you don't think that I'm 8410 06:46:53,000 --> 06:46:58,398 cheating over here and if we take a look 8411 06:46:56,000 --> 06:47:00,558 at the tasks okay this is what we have 8412 06:46:58,398 --> 06:47:03,558 is completed is equal to false but then 8413 06:47:00,558 --> 06:47:06,558 once I click notice it's set equal to 8414 06:47:03,558 --> 06:47:09,558 true so when I refresh I get back the 8415 06:47:06,558 --> 06:47:12,718 same result this concludes the tasks 8416 06:47:09,558 --> 06:47:15,080 project hopefully everyone enjoyed it 8417 06:47:12,718 --> 06:47:19,478 and up next we're going to take a look 8418 06:47:15,080 --> 06:47:21,718 how we can use typescript in react all 8419 06:47:19,478 --> 06:47:24,320 right and once we're familiar with the 8420 06:47:21,718 --> 06:47:27,040 typescript fundamentals let's see how we 8421 06:47:24,320 --> 06:47:29,958 can use typescript with react before we 8422 06:47:27,040 --> 06:47:33,680 begin let me just mention that I assume 8423 06:47:29,958 --> 06:47:36,878 you are already comfortable with react 8424 06:47:33,680 --> 06:47:40,600 so things like components State most 8425 06:47:36,878 --> 06:47:44,280 common Hooks and some of the popular 8426 06:47:40,600 --> 06:47:47,680 libraries if you are confused about some 8427 06:47:44,280 --> 06:47:50,958 react topics please pause the video and 8428 06:47:47,680 --> 06:47:52,958 utilize some external resources since 8429 06:47:50,958 --> 06:47:55,600 our main focus is going to be on 8430 06:47:52,958 --> 06:47:58,520 implementing react with typescript 8431 06:47:55,600 --> 06:48:01,718 otherwise this part of the course will 8432 06:47:58,520 --> 06:48:03,558 easily turn into a react tutorial in 8433 06:48:01,718 --> 06:48:06,920 order to follow along with this part of 8434 06:48:03,558 --> 06:48:09,600 the course you will need a star project 8435 06:48:06,920 --> 06:48:14,160 which is the second folder in the main 8436 06:48:09,600 --> 06:48:18,440 course repo so if you have not already 8437 06:48:14,160 --> 06:48:21,958 done so get a hold of the repo either by 8438 06:48:18,440 --> 06:48:24,840 downloading or cloning it and once you 8439 06:48:21,958 --> 06:48:28,958 have the main repo on your machine open 8440 06:48:24,840 --> 06:48:30,320 the second folder in your text error so 8441 06:48:28,958 --> 06:48:34,478 get a hold of the 8442 06:48:30,320 --> 06:48:37,840 repo open up your text eror and then 8443 06:48:34,478 --> 06:48:42,160 just access the second 8444 06:48:37,840 --> 06:48:46,000 folder now it is a note app so same deal 8445 06:48:42,160 --> 06:48:48,958 we want to start by running npm install 8446 06:48:46,000 --> 06:48:51,360 first and also or you know what I'm not 8447 06:48:48,958 --> 06:48:53,798 going to set up such command because 8448 06:48:51,360 --> 06:48:55,360 here and there students complain that 8449 06:48:53,798 --> 06:48:58,558 such command doesn't work on their 8450 06:48:55,360 --> 06:49:01,520 operating system so first let's just run 8451 06:48:58,558 --> 06:49:05,080 npm install and then we want to go with 8452 06:49:01,520 --> 06:49:06,638 npm run run Dev and if everything is 8453 06:49:05,080 --> 06:49:09,320 correct again you'll have the 8454 06:49:06,638 --> 06:49:12,558 application on Local Host 8455 06:49:09,320 --> 06:49:15,280 5173 and this is what you should see on 8456 06:49:12,558 --> 06:49:17,878 the screen and if that's the case we can 8457 06:49:15,280 --> 06:49:19,520 move on to the next step all right and 8458 06:49:17,878 --> 06:49:20,920 let's start by taking a look at the 8459 06:49:19,520 --> 06:49:24,840 files and 8460 06:49:20,920 --> 06:49:27,040 folders as well as the assets I provided 8461 06:49:24,840 --> 06:49:29,080 so we have node modules over here this 8462 06:49:27,040 --> 06:49:31,160 is where we have all of our dependencies 8463 06:49:29,080 --> 06:49:34,840 we have the public one this is where we 8464 06:49:31,160 --> 06:49:37,160 can set up some public assets a source 8465 06:49:34,840 --> 06:49:40,120 folder again this is where we'll do all 8466 06:49:37,160 --> 06:49:43,000 of our work but in this case we're going 8467 06:49:40,120 --> 06:49:45,080 to be working with react so we'll create 8468 06:49:43,000 --> 06:49:49,080 components and all that cool stuff we 8469 06:49:45,080 --> 06:49:53,000 have the eslint file also a g ignore 8470 06:49:49,080 --> 06:49:54,638 again this just provides info what files 8471 06:49:53,000 --> 06:49:58,200 and folders are going to be ignored by 8472 06:49:54,638 --> 06:50:00,760 The Source control index HTML this is 8473 06:49:58,200 --> 06:50:03,280 where our root component is going to get 8474 06:50:00,760 --> 06:50:06,200 injected so for example if you want to 8475 06:50:03,280 --> 06:50:08,760 change the title feel free to do so a 8476 06:50:06,200 --> 06:50:12,798 package Json so in here you can take a 8477 06:50:08,760 --> 06:50:15,280 look what extra libraries I installed 8478 06:50:12,798 --> 06:50:18,200 and also we can see that we have a Dev 8479 06:50:15,280 --> 06:50:21,520 one so that spins up the dev server and 8480 06:50:18,200 --> 06:50:24,320 also same deal we have over here a build 8481 06:50:21,520 --> 06:50:27,320 command and before vit builds the 8482 06:50:24,320 --> 06:50:30,638 project what do we do we compile our 8483 06:50:27,320 --> 06:50:33,440 code then we have the good old 8484 06:50:30,638 --> 06:50:36,320 typescript configuration file and also a 8485 06:50:33,440 --> 06:50:40,320 few more configuration files for V and 8486 06:50:36,320 --> 06:50:43,200 also the node one now you'll also find 8487 06:50:40,320 --> 06:50:46,360 the readme over here and just like in 8488 06:50:43,200 --> 06:50:50,760 the first part of the course we will use 8489 06:50:46,360 --> 06:50:52,638 this readme to set up the challenges to 8490 06:50:50,760 --> 06:50:55,200 take a look at the examples I also 8491 06:50:52,638 --> 06:50:57,398 provided some links to useful resources 8492 06:50:55,200 --> 06:50:59,080 and all that cool stuff and first thing 8493 06:50:57,398 --> 06:51:03,718 that I just want to mention essentially 8494 06:50:59,080 --> 06:51:06,600 this project is a straight up beat react 8495 06:51:03,718 --> 06:51:08,600 TS boilerplate application the 8496 06:51:06,600 --> 06:51:11,878 difference I just added some assets to 8497 06:51:08,600 --> 06:51:14,878 it and removed some boiler plate files 8498 06:51:11,878 --> 06:51:17,878 that's it so if you're going to run npm 8499 06:51:14,878 --> 06:51:20,000 create V latest react typescript so this 8500 06:51:17,878 --> 06:51:22,520 is the name of course you can change it 8501 06:51:20,000 --> 06:51:26,120 then the double dashes template and 8502 06:51:22,520 --> 06:51:29,440 provide react DS effectively this is the 8503 06:51:26,120 --> 06:51:31,200 project you're going to get back and 8504 06:51:29,440 --> 06:51:33,080 like I mentioned before most of our work 8505 06:51:31,200 --> 06:51:35,398 we're going to do in a source 8506 06:51:33,080 --> 06:51:37,798 so let's just navigate over there for 8507 06:51:35,398 --> 06:51:40,320 starters just like 8508 06:51:37,798 --> 06:51:44,718 traditional react application we have 8509 06:51:40,320 --> 06:51:47,958 main DSX so this is where we inject our 8510 06:51:44,718 --> 06:51:51,440 app basically our root component into 8511 06:51:47,958 --> 06:51:54,240 that div with the ID of rot now also 8512 06:51:51,440 --> 06:51:55,558 you'll notice few Imports but we'll 8513 06:51:54,240 --> 06:51:58,478 discuss it 8514 06:51:55,558 --> 06:52:01,398 later I mean these are just Imports for 8515 06:51:58,478 --> 06:52:04,360 some extra libraries we're going to use 8516 06:52:01,398 --> 06:52:06,440 then we have the app DSX so this is 8517 06:52:04,360 --> 06:52:09,760 where all of our components are going to 8518 06:52:06,440 --> 06:52:12,160 meet you can also call this the root 8519 06:52:09,760 --> 06:52:15,000 component and we have a final and a 8520 06:52:12,160 --> 06:52:17,878 starter and the way I set it up notice 8521 06:52:15,000 --> 06:52:20,000 these are all the topics we're going to 8522 06:52:17,878 --> 06:52:23,080 discuss and this is going to be our 8523 06:52:20,000 --> 06:52:25,600 starting point and in here you'll find 8524 06:52:23,080 --> 06:52:27,558 the complete code so 8525 06:52:25,600 --> 06:52:30,200 essentially pretty much every time we'll 8526 06:52:27,558 --> 06:52:33,200 start working on a new topic we'll just 8527 06:52:30,200 --> 06:52:36,000 import the specific folder because if 8528 06:52:33,200 --> 06:52:39,040 you notice all of them have index DSX so 8529 06:52:36,000 --> 06:52:41,718 this will be the default import that's 8530 06:52:39,040 --> 06:52:43,920 just a note thing and then as we're 8531 06:52:41,718 --> 06:52:47,120 moving from topic to topic we'll just 8532 06:52:43,920 --> 06:52:50,120 change the path over here now for all of 8533 06:52:47,120 --> 06:52:52,798 them you'll just find this 8534 06:52:50,120 --> 06:52:55,638 standard component where I have few 8535 06:52:52,798 --> 06:52:57,920 heading twos and basically I'm just 8536 06:52:55,638 --> 06:53:01,280 displaying some text on the screen so 8537 06:52:57,920 --> 06:53:03,120 all of them pretty much will be the same 8538 06:53:01,280 --> 06:53:04,600 but of course once we're done the topic 8539 06:53:03,120 --> 06:53:07,680 there's going to be more files more 8540 06:53:04,600 --> 06:53:09,120 logic and rest of the good stuff and 8541 06:53:07,680 --> 06:53:12,000 just to Showcase how it's going to look 8542 06:53:09,120 --> 06:53:14,760 like let me navigate to app TSX we want 8543 06:53:12,000 --> 06:53:16,718 to go with import then let's come up 8544 06:53:14,760 --> 06:53:19,280 with the name in my case I'm going to go 8545 06:53:16,718 --> 06:53:22,718 pretty generic I'm going to say 8546 06:53:19,280 --> 06:53:27,040 component from and now we want to go to 8547 06:53:22,718 --> 06:53:29,600 a star forward slash and then again we 8548 06:53:27,040 --> 06:53:32,120 have all of these topics and as you can 8549 06:53:29,600 --> 06:53:33,558 see they are numbered so we'll start 8550 06:53:32,120 --> 06:53:35,398 with 8551 06:53:33,558 --> 06:53:39,320 return topic and then all the way to 8552 06:53:35,398 --> 06:53:41,080 tasks so let's go with 01 return notice 8553 06:53:39,320 --> 06:53:44,760 again we don't need to provide the file 8554 06:53:41,080 --> 06:53:49,040 because all of the folders have already 8555 06:53:44,760 --> 06:53:50,958 a index DSX and then we want to replace 8556 06:53:49,040 --> 06:53:53,000 it over here we want to go with 8557 06:53:50,958 --> 06:53:56,558 component and then render it on the 8558 06:53:53,000 --> 06:53:59,200 screen again at the moment it's not 8559 06:53:56,558 --> 06:54:02,000 going to change that much because we 8560 06:53:59,200 --> 06:54:04,080 haven't set up any logic yet and 8561 06:54:02,000 --> 06:54:07,000 basically as I'm moving from topic to 8562 06:54:04,080 --> 06:54:10,160 topic I'm just going to change over here 8563 06:54:07,000 --> 06:54:12,840 the path and then if you want to test 8564 06:54:10,160 --> 06:54:15,120 something of course you can import the 8565 06:54:12,840 --> 06:54:17,638 component from one of the final ones as 8566 06:54:15,120 --> 06:54:21,440 well now also something I want to 8567 06:54:17,638 --> 06:54:23,920 mention the idea with the react and 8568 06:54:21,440 --> 06:54:26,920 typescript is following if we want to 8569 06:54:23,920 --> 06:54:30,558 set up components basically react the 8570 06:54:26,920 --> 06:54:32,840 stuff we need to go with DSX so that's 8571 06:54:30,558 --> 06:54:34,798 the extension now if you just want to 8572 06:54:32,840 --> 06:54:37,440 set up some logic some good old 8573 06:54:34,798 --> 06:54:39,798 JavaScript logic just like we did in the 8574 06:54:37,440 --> 06:54:42,398 first part of the course we just go with 8575 06:54:39,798 --> 06:54:46,320 Ds so whether you want to set up some 8576 06:54:42,398 --> 06:54:49,240 hooks file or utils file or types file 8577 06:54:46,320 --> 06:54:51,878 that's the case you'll go with Ds but 8578 06:54:49,240 --> 06:54:53,638 the moment you set up the component you 8579 06:54:51,878 --> 06:54:57,798 want to go with 8580 06:54:53,638 --> 06:55:00,840 TSX hopefully I clearly explained how 8581 06:54:57,798 --> 06:55:02,958 our setup is going to work and if you're 8582 06:55:00,840 --> 06:55:05,558 looking at the other files over here in 8583 06:55:02,958 --> 06:55:08,440 the source one we'll discuss them 8584 06:55:05,558 --> 06:55:12,558 essentially as we progress with the 8585 06:55:08,440 --> 06:55:16,360 course content for example the hooks the 8586 06:55:12,558 --> 06:55:17,798 store so all of that is coming up and I 8587 06:55:16,360 --> 06:55:21,240 guess lastly let me just mention that 8588 06:55:17,798 --> 06:55:25,360 yes I did provide some index CSS just so 8589 06:55:21,240 --> 06:55:28,360 our examples look a bit more presentable 8590 06:55:25,360 --> 06:55:30,080 but again this is totally optional and 8591 06:55:28,360 --> 06:55:32,798 with this in place we can move on to our 8592 06:55:30,080 --> 06:55:36,478 next topic which is going to be 8593 06:55:32,798 --> 06:55:38,600 the component returns in react all right 8594 06:55:36,478 --> 06:55:42,958 so we have the correct import again 8595 06:55:38,600 --> 06:55:45,600 we're looking for Star 01 return so 8596 06:55:42,958 --> 06:55:50,120 that's where we want to navigate and our 8597 06:55:45,600 --> 06:55:54,160 first topic is going to be the component 8598 06:55:50,120 --> 06:55:57,440 return now let's establish a few things 8599 06:55:54,160 --> 06:56:01,680 for starters what are react components 8600 06:55:57,440 --> 06:56:04,760 they are functions correct and what is 8601 06:56:01,680 --> 06:56:07,440 the one thing that we know about 8602 06:56:04,760 --> 06:56:10,520 function returns and 8603 06:56:07,440 --> 06:56:14,680 typescript well it's the fact that 8604 06:56:10,520 --> 06:56:19,600 typescript infers the return type so if 8605 06:56:14,680 --> 06:56:22,280 I hover over notice how typescript sees 8606 06:56:19,600 --> 06:56:25,080 that essentially this is a component and 8607 06:56:22,280 --> 06:56:28,520 the return is jsx 8608 06:56:25,080 --> 06:56:31,760 element so what does that mean well if 8609 06:56:28,520 --> 06:56:34,558 for example I'm going to forget to set 8610 06:56:31,760 --> 06:56:37,398 up the turn actually typescript is going 8611 06:56:34,558 --> 06:56:40,680 to yell at me so notice once I save I 8612 06:56:37,398 --> 06:56:43,080 have red squiggly line in app DSX and if 8613 06:56:40,680 --> 06:56:47,040 we hover over we can see that component 8614 06:56:43,080 --> 06:56:48,520 cannot be used as jsx component because 8615 06:56:47,040 --> 06:56:51,600 it returns 8616 06:56:48,520 --> 06:56:55,520 void and few things I want to mention 8617 06:56:51,600 --> 06:56:57,360 first it's super helpful because when 8618 06:56:55,520 --> 06:56:59,920 you're working on a bigger project I 8619 06:56:57,360 --> 06:57:02,920 mean having no return sometimes can be 8620 06:56:59,920 --> 06:57:05,160 tricky to spot as far as the error go 8621 06:57:02,920 --> 06:57:08,440 and second one this is something that I 8622 06:57:05,160 --> 06:57:11,080 will probably repeat 10,000 times during 8623 06:57:08,440 --> 06:57:12,878 the second and most likely also the 8624 06:57:11,080 --> 06:57:15,320 third part of the 8625 06:57:12,878 --> 06:57:18,638 course code error is going to be your 8626 06:57:15,320 --> 06:57:21,558 best friend the moment you hover over 8627 06:57:18,638 --> 06:57:26,360 the squiggly line or in 8628 06:57:21,558 --> 06:57:29,520 general the events and state values and 8629 06:57:26,360 --> 06:57:33,240 all that it will immediately pretty much 8630 06:57:29,520 --> 06:57:36,398 give you all the info you need so in our 8631 06:57:33,240 --> 06:57:38,320 case let's say if we go back over here 8632 06:57:36,398 --> 06:57:42,160 and set up the proper return we can 8633 06:57:38,320 --> 06:57:44,920 clearly see that typescript infers the 8634 06:57:42,160 --> 06:57:46,760 jsx element which leads me to another 8635 06:57:44,920 --> 06:57:50,718 Point can 8636 06:57:46,760 --> 06:57:53,718 we explicitly write jsx element in here 8637 06:57:50,718 --> 06:57:56,160 as far as the component return yes it's 8638 06:57:53,718 --> 06:57:57,878 absolutely an option we can go over here 8639 06:57:56,160 --> 06:58:00,120 even though it's already inferred we can 8640 06:57:57,878 --> 06:58:03,080 say hey you know what I'm going to go 8641 06:58:00,120 --> 06:58:05,520 over here with jsx element we save it 8642 06:58:03,080 --> 06:58:08,680 and since in this case I am returning a 8643 06:58:05,520 --> 06:58:11,760 jsx element there's no issue pretty much 8644 06:58:08,680 --> 06:58:15,920 it's the same deal but keep in mind what 8645 06:58:11,760 --> 06:58:19,120 if in your component you decide that 8646 06:58:15,920 --> 06:58:22,478 you're going to return a string well now 8647 06:58:19,120 --> 06:58:25,360 it's not going to fly because now I 8648 06:58:22,478 --> 06:58:28,878 already have here an explicit return 8649 06:58:25,360 --> 06:58:32,558 where I say hey I'm going to return jsx 8650 06:58:28,878 --> 06:58:34,440 element and what about null What if I I 8651 06:58:32,558 --> 06:58:36,760 have some kind of logic and I'm going to 8652 06:58:34,440 --> 06:58:38,840 go with null again same deal we cannot 8653 06:58:36,760 --> 06:58:42,680 do that because we have explicitly over 8654 06:58:38,840 --> 06:58:45,798 here this jsx element now if you don't 8655 06:58:42,680 --> 06:58:49,360 have that typescript is smart typescript 8656 06:58:45,798 --> 06:58:53,160 can infer that hey in this case it can 8657 06:58:49,360 --> 06:58:56,000 be either hello either jsx element or no 8658 06:58:53,160 --> 06:58:59,520 so again this really comes down to your 8659 06:58:56,000 --> 06:59:03,440 preference one option you can simply let 8660 06:58:59,520 --> 06:59:05,520 the typescript to infer a type and at 8661 06:59:03,440 --> 06:59:08,200 least at the very basic scenario it's 8662 06:59:05,520 --> 06:59:10,920 going to help you to avoid this case 8663 06:59:08,200 --> 06:59:13,240 where you don't return anything second 8664 06:59:10,920 --> 06:59:16,200 option you can start hardcoding over 8665 06:59:13,240 --> 06:59:18,520 here explicitly what are you expecting 8666 06:59:16,200 --> 06:59:22,360 back from this component just keep in 8667 06:59:18,520 --> 06:59:24,000 mind as your logic changes you'll have 8668 06:59:22,360 --> 06:59:26,958 to adjust because if you're just going 8669 06:59:24,000 --> 06:59:29,520 to write all the time jsx element well 8670 06:59:26,958 --> 06:59:32,120 typescript is going to be confused so in 8671 06:59:29,520 --> 06:59:35,398 this case let me just hardcode it but 8672 06:59:32,120 --> 06:59:37,680 for most part I just let typescript to 8673 06:59:35,398 --> 06:59:40,240 infer a type so I'm going to go over 8674 06:59:37,680 --> 06:59:43,120 here with jsx element that's going to be 8675 06:59:40,240 --> 06:59:47,040 one option I'll say Hey listen it can 8676 06:59:43,120 --> 06:59:49,638 also be null and also well let's just 8677 06:59:47,040 --> 06:59:52,520 set up a string type let me save it and 8678 06:59:49,638 --> 06:59:56,558 now notice Ty script doesn't complain 8679 06:59:52,520 --> 07:00:00,398 everything's fine because I have the 8680 06:59:56,558 --> 07:00:04,320 explicit return and the options that I 8681 07:00:00,398 --> 07:00:08,000 have inside of the component match to 8682 07:00:04,320 --> 07:00:09,600 whatever I have over here in the list so 8683 07:00:08,000 --> 07:00:12,798 that's how we can set up component 8684 07:00:09,600 --> 07:00:16,958 returns in typescript all right and up 8685 07:00:12,798 --> 07:00:20,200 next on our list we have I believe props 8686 07:00:16,958 --> 07:00:24,280 so first let's change the path so we're 8687 07:00:20,200 --> 07:00:25,200 looking for 0 to props let's take a look 8688 07:00:24,280 --> 07:00:28,638 at 8689 07:00:25,200 --> 07:00:31,280 the browser yep we have react typescript 8690 07:00:28,638 --> 07:00:34,440 and props so looks like everything is 8691 07:00:31,280 --> 07:00:36,478 correct and essentially in here in the 8692 07:00:34,440 --> 07:00:37,840 component let's just provide some values 8693 07:00:36,478 --> 07:00:41,878 so we're going to go with something 8694 07:00:37,840 --> 07:00:44,280 generic for example name and the ID 8695 07:00:41,878 --> 07:00:48,600 which is going to be actually a number 8696 07:00:44,280 --> 07:00:51,320 so 1 two 3 and immediately once we add 8697 07:00:48,600 --> 07:00:54,798 those values typescript will complain 8698 07:00:51,320 --> 07:00:57,718 because again it's like setting up a 8699 07:00:54,798 --> 07:00:59,718 function providing the arguments but we 8700 07:00:57,718 --> 07:01:02,280 haven't set up the parameters so 8701 07:00:59,718 --> 07:01:05,760 typescript doesn't know what to expect 8702 07:01:02,280 --> 07:01:09,760 so let's navigate right now to our props 8703 07:01:05,760 --> 07:01:12,760 index DX and let's take a look at the 8704 07:01:09,760 --> 07:01:14,718 multiple options we have for props and 8705 07:01:12,760 --> 07:01:20,240 essentially the first one is the inline 8706 07:01:14,718 --> 07:01:24,440 one remember in react props are gathered 8707 07:01:20,240 --> 07:01:26,798 in this object which we access either by 8708 07:01:24,440 --> 07:01:29,040 just the structuring over here where we 8709 07:01:26,798 --> 07:01:32,478 go with the curly braces or we go with 8710 07:01:29,040 --> 07:01:35,280 props now I'll show you the other option 8711 07:01:32,478 --> 07:01:38,320 a little bit later for now let's just 8712 07:01:35,280 --> 07:01:39,478 grab the object the props object and 8713 07:01:38,320 --> 07:01:42,558 let's 8714 07:01:39,478 --> 07:01:44,280 explicitly take a name and the ID so 8715 07:01:42,558 --> 07:01:46,878 those are the two values we're looking 8716 07:01:44,280 --> 07:01:49,600 for but before we can do that since 8717 07:01:46,878 --> 07:01:51,920 we're working in a typescript yep just 8718 07:01:49,600 --> 07:01:55,360 like regular function we'll need to set 8719 07:01:51,920 --> 07:01:57,718 up the type and the first option we have 8720 07:01:55,360 --> 07:02:00,520 is basically set up everything in line 8721 07:01:57,718 --> 07:02:02,638 now I can tell you right away that it's 8722 07:02:00,520 --> 07:02:05,520 most likely not going to be your 8723 07:02:02,638 --> 07:02:09,320 approach especially for bigger projects 8724 07:02:05,520 --> 07:02:11,718 where you have components with bunch of 8725 07:02:09,320 --> 07:02:14,718 props because it's just going to be very 8726 07:02:11,718 --> 07:02:16,760 messy so yes in here it will start nice 8727 07:02:14,718 --> 07:02:19,600 and easy but in a second I'll show you 8728 07:02:16,760 --> 07:02:22,798 also another approach that we can take 8729 07:02:19,600 --> 07:02:25,680 so in here we just say Hey listen name 8730 07:02:22,798 --> 07:02:29,160 is going to be a string and the ID will 8731 07:02:25,680 --> 07:02:31,840 be a number and the moment we do that 8732 07:02:29,160 --> 07:02:34,080 typescript should be happy it just 8733 07:02:31,840 --> 07:02:37,840 complaining that we haven't essentially 8734 07:02:34,080 --> 07:02:41,280 used it yet now notice how everything's 8735 07:02:37,840 --> 07:02:43,760 fine in the component for some reason 8736 07:02:41,280 --> 07:02:45,360 it's complaining over here so let's try 8737 07:02:43,760 --> 07:02:47,798 to use these values and let's see 8738 07:02:45,360 --> 07:02:49,520 whether that makes typescript happy so 8739 07:02:47,798 --> 07:02:52,600 I'm just going to change it around I'll 8740 07:02:49,520 --> 07:02:57,478 say adding one now that one will be 8741 07:02:52,600 --> 07:03:00,798 equal to name and we're looking for our 8742 07:02:57,478 --> 07:03:02,680 name value and then the second one will 8743 07:03:00,798 --> 07:03:06,160 be Hing one and and we'll just provide 8744 07:03:02,680 --> 07:03:09,040 the ID so let's go with ID and then 8745 07:03:06,160 --> 07:03:11,240 let's access that let's save this and if 8746 07:03:09,040 --> 07:03:14,000 everything is correct we should see name 8747 07:03:11,240 --> 07:03:16,398 Peter and whatever ID 8748 07:03:14,000 --> 07:03:18,680 provided again we just go back to the 8749 07:03:16,398 --> 07:03:21,080 fundamentals section where we learn that 8750 07:03:18,680 --> 07:03:24,120 every time we have a function we need to 8751 07:03:21,080 --> 07:03:27,440 provide info to typescript what is this 8752 07:03:24,120 --> 07:03:30,200 function going to receive as far as the 8753 07:03:27,440 --> 07:03:32,558 argument and since props are essentially 8754 07:03:30,200 --> 07:03:33,840 arguments that we pass in when we 8755 07:03:32,558 --> 07:03:36,558 construct the 8756 07:03:33,840 --> 07:03:38,840 component we need to provide the type 8757 07:03:36,558 --> 07:03:41,080 annotations and what's really cool let's 8758 07:03:38,840 --> 07:03:45,440 say that we're using this component in 8759 07:03:41,080 --> 07:03:49,320 multiple places in our project if for 8760 07:03:45,440 --> 07:03:52,080 some reason I'm going to forget provide 8761 07:03:49,320 --> 07:03:55,000 one of the props notice how immediately 8762 07:03:52,080 --> 07:03:59,558 typescript is going to complain so I 8763 07:03:55,000 --> 07:04:02,080 won't have to realize this error at run 8764 07:03:59,558 --> 07:04:05,120 time as I'm building the project let's 8765 07:04:02,080 --> 07:04:07,160 say that I'm setting up this component 8766 07:04:05,120 --> 07:04:09,878 somewhere and I just forget that I also 8767 07:04:07,160 --> 07:04:12,520 need to provide the name typescript will 8768 07:04:09,878 --> 07:04:16,080 immediately let me know that something's 8769 07:04:12,520 --> 07:04:17,958 wrong and actually the same works if I 8770 07:04:16,080 --> 07:04:20,600 decide that the name is actually going 8771 07:04:17,958 --> 07:04:24,200 to be title I mean it's quite common to 8772 07:04:20,600 --> 07:04:27,398 have a component and you think that yeah 8773 07:04:24,200 --> 07:04:29,160 the name for the prop name is awesome 8774 07:04:27,398 --> 07:04:32,120 but then you realize that hey you know 8775 07:04:29,160 --> 07:04:35,398 what actually prop name should be titled 8776 07:04:32,120 --> 07:04:38,160 let's say I do that notice how 8777 07:04:35,398 --> 07:04:42,200 immediately in all the places where I'm 8778 07:04:38,160 --> 07:04:45,600 passing in the wrong prop it's going to 8779 07:04:42,200 --> 07:04:47,878 light up so again imagine scenario where 8780 07:04:45,600 --> 07:04:50,398 you have this component in multiple 8781 07:04:47,878 --> 07:04:52,478 places in your project immediately you 8782 07:04:50,398 --> 07:04:55,200 know that something is wrong again 8783 07:04:52,478 --> 07:04:57,040 something super super cool where you 8784 07:04:55,200 --> 07:04:58,680 don't need to run around like headless 8785 07:04:57,040 --> 07:05:02,718 chicken and then look for those 8786 07:04:58,680 --> 07:05:06,200 instances you'll immediately know where 8787 07:05:02,718 --> 07:05:09,200 you provided the wrong prop so let me 8788 07:05:06,200 --> 07:05:11,798 set it back to name and ID and up next 8789 07:05:09,200 --> 07:05:15,398 we're going to discuss alternatives to 8790 07:05:11,798 --> 07:05:18,840 inline type annotations okay so our 8791 07:05:15,398 --> 07:05:23,160 initial setup works but what if we have 8792 07:05:18,840 --> 07:05:26,240 a long list of props or let's say we 8793 07:05:23,160 --> 07:05:29,200 want to reuse these props well in that 8794 07:05:26,240 --> 07:05:31,840 case we can either set up the type or 8795 07:05:29,200 --> 07:05:33,478 the interface again this is the totally 8796 07:05:31,840 --> 07:05:35,680 up to you I'm going to stick with type 8797 07:05:33,478 --> 07:05:38,520 just because it's my preference I'm 8798 07:05:35,680 --> 07:05:41,080 going to call this component 8799 07:05:38,520 --> 07:05:43,638 props and in here we just want to 8800 07:05:41,080 --> 07:05:46,040 provide pretty much the same values so 8801 07:05:43,638 --> 07:05:50,160 actually you know what I can just cut it 8802 07:05:46,040 --> 07:05:50,920 out remove these curries set it equal to 8803 07:05:50,160 --> 07:05:54,478 and 8804 07:05:50,920 --> 07:05:56,080 then where I have my D structuring I 8805 07:05:54,478 --> 07:05:58,840 just want to set it equal to my 8806 07:05:56,080 --> 07:06:01,600 component props save it and since I 8807 07:05:58,840 --> 07:06:04,120 don't see any red squiggly lines I'm 8808 07:06:01,600 --> 07:06:05,600 good to go now also please keep in mind 8809 07:06:04,120 --> 07:06:08,760 that 8810 07:06:05,600 --> 07:06:12,040 alternatively you can just reference the 8811 07:06:08,760 --> 07:06:15,360 props so in here I can say yeah props is 8812 07:06:12,040 --> 07:06:18,398 going to be equal to component props 8813 07:06:15,360 --> 07:06:20,520 instead of the structuring name and ID 8814 07:06:18,398 --> 07:06:23,080 and then every time I want to access one 8815 07:06:20,520 --> 07:06:26,240 of the values I can go here with props 8816 07:06:23,080 --> 07:06:28,680 ID and props name again this really 8817 07:06:26,240 --> 07:06:31,638 comes down to your preference Some 8818 07:06:28,680 --> 07:06:34,760 people prefer the props option and some 8819 07:06:31,638 --> 07:06:36,920 people prefer this option as long as you 8820 07:06:34,760 --> 07:06:39,200 don't have the red squiggly lines as 8821 07:06:36,920 --> 07:06:41,680 long as typescript is not complaining it 8822 07:06:39,200 --> 07:06:45,920 does really come down to your preference 8823 07:06:41,680 --> 07:06:47,920 and the same goes for the type or the 8824 07:06:45,920 --> 07:06:50,360 interface so if you want to use the 8825 07:06:47,920 --> 07:06:54,080 interface over here you can definitely 8826 07:06:50,360 --> 07:06:56,440 do so if you prefer type it's definitely 8827 07:06:54,080 --> 07:06:58,760 an option as well and while we're still 8828 07:06:56,440 --> 07:07:01,520 on a topic of props let's also discuss 8829 07:06:58,760 --> 07:07:03,958 the special children prop which allows 8830 07:07:01,520 --> 07:07:08,558 elements and components to be passed 8831 07:07:03,958 --> 07:07:10,920 into other components so if I'm going to 8832 07:07:08,558 --> 07:07:15,920 go to app 8833 07:07:10,920 --> 07:07:18,760 PSX and set up my closing tag as well 8834 07:07:15,920 --> 07:07:21,200 and then let's say add a heading to with 8835 07:07:18,760 --> 07:07:24,600 a hello world typescript is going to 8836 07:07:21,200 --> 07:07:26,878 complain because I haven't specified 8837 07:07:24,600 --> 07:07:29,718 that I'm going to provide the children 8838 07:07:26,878 --> 07:07:32,160 prop now the name is exactly the same so 8839 07:07:29,718 --> 07:07:35,878 that doesn't change we just go over here 8840 07:07:32,160 --> 07:07:37,878 with children and yes we do want to 8841 07:07:35,878 --> 07:07:41,520 render it somewhere in our return as 8842 07:07:37,878 --> 07:07:44,718 well but now M dollar question what is 8843 07:07:41,520 --> 07:07:48,160 the type and essentially we have few 8844 07:07:44,718 --> 07:07:52,120 options we can hardcode it ourselves and 8845 07:07:48,160 --> 07:07:54,120 the type we're looking for is react node 8846 07:07:52,120 --> 07:07:57,040 and we can import it or we can just go 8847 07:07:54,120 --> 07:08:00,000 to react Dot and then react node so for 8848 07:07:57,040 --> 07:08:02,000 example this is one option we can take 8849 07:08:00,000 --> 07:08:06,638 where we go with react 8850 07:08:02,000 --> 07:08:09,478 Dot and then react node so that's the 8851 07:08:06,638 --> 07:08:12,520 type we have access to and the moment we 8852 07:08:09,478 --> 07:08:14,360 do that we fix the first error but 8853 07:08:12,520 --> 07:08:17,600 notice right now typescript is 8854 07:08:14,360 --> 07:08:21,440 complaining because since I set up my 8855 07:08:17,600 --> 07:08:23,920 children prop in the props component 8856 07:08:21,440 --> 07:08:27,760 it's also expecting the same thing over 8857 07:08:23,920 --> 07:08:30,080 here and one easy way how to fix that is 8858 07:08:27,760 --> 07:08:31,920 to just make this one optional just like 8859 07:08:30,080 --> 07:08:34,280 we covered through during the 8860 07:08:31,920 --> 07:08:37,920 fundamentals part so I can just go here 8861 07:08:34,280 --> 07:08:40,320 say that yeah children might be provided 8862 07:08:37,920 --> 07:08:43,280 or not and if it's provided then it's 8863 07:08:40,320 --> 07:08:46,520 going to be a react node now also a 8864 07:08:43,280 --> 07:08:48,920 quick side note just like with vanilla 8865 07:08:46,520 --> 07:08:50,478 JavaScript actually during development 8866 07:08:48,920 --> 07:08:52,558 everything is going to work even though 8867 07:08:50,478 --> 07:08:55,080 we have the error but please keep in 8868 07:08:52,558 --> 07:08:57,840 mind you won't be able to build the 8869 07:08:55,080 --> 07:09:00,080 project unless you change some 8870 07:08:57,840 --> 07:09:03,638 configurations and 8871 07:09:00,080 --> 07:09:05,760 allow the project to be built with the 8872 07:09:03,638 --> 07:09:09,638 typescript errors hopefully that is 8873 07:09:05,760 --> 07:09:11,958 clear so let me go back to this optional 8874 07:09:09,638 --> 07:09:14,718 property and let me show you another way 8875 07:09:11,958 --> 07:09:17,398 how we can handle the children prop 8876 07:09:14,718 --> 07:09:20,760 alternatively we can also import a 8877 07:09:17,398 --> 07:09:24,200 special type from react which is called 8878 07:09:20,760 --> 07:09:27,240 props with children so yes we have 8879 07:09:24,200 --> 07:09:29,638 access to a declaration file where there 8880 07:09:27,240 --> 07:09:32,680 is a type called props with children 8881 07:09:29,638 --> 07:09:36,240 which actually is going to handle 8882 07:09:32,680 --> 07:09:38,040 the children prop for us now in order to 8883 07:09:36,240 --> 07:09:41,080 import it we're going to go over here 8884 07:09:38,040 --> 07:09:44,360 with import now I will add this type 8885 07:09:41,080 --> 07:09:46,080 remember technically it's not a must 8886 07:09:44,360 --> 07:09:48,520 then we're looking for props with 8887 07:09:46,080 --> 07:09:51,718 children and this is going to be coming 8888 07:09:48,520 --> 07:09:53,680 from react and if we hover over we can 8889 07:09:51,718 --> 07:09:55,638 nicely see the type 8890 07:09:53,680 --> 07:10:00,320 definition and this is where I would 8891 07:09:55,638 --> 07:10:02,160 encourage you to stop the video and go 8892 07:10:00,320 --> 07:10:06,240 over this logic 8893 07:10:02,160 --> 07:10:07,600 and try to see whether you're able to 8894 07:10:06,240 --> 07:10:10,798 understand everything that's happening 8895 07:10:07,600 --> 07:10:13,600 under the hood since we have covered all 8896 07:10:10,798 --> 07:10:15,878 of these things during the fundamentals 8897 07:10:13,600 --> 07:10:18,160 part and again reading such type 8898 07:10:15,878 --> 07:10:21,798 definitions is going to be a good 8899 07:10:18,160 --> 07:10:24,718 practice for us not only to jog our 8900 07:10:21,798 --> 07:10:26,920 memory as far as typescript but also to 8901 07:10:24,718 --> 07:10:29,958 understand what is the specific type or 8902 07:10:26,920 --> 07:10:32,558 function or I don't know property 8903 07:10:29,958 --> 07:10:34,240 actually doing and if we take a look at 8904 07:10:32,558 --> 07:10:36,958 the type definition we can see that 8905 07:10:34,240 --> 07:10:40,000 props with children is a generic which 8906 07:10:36,958 --> 07:10:42,600 again kind of makes sense 8907 07:10:40,000 --> 07:10:45,360 because it's going to be used in a lot 8908 07:10:42,600 --> 07:10:48,680 of situations so there's going to be the 8909 07:10:45,360 --> 07:10:51,320 case where the component props has one 8910 07:10:48,680 --> 07:10:54,280 shape and then in the second component 8911 07:10:51,320 --> 07:10:57,958 the shape might be totally different so 8912 07:10:54,280 --> 07:11:00,200 again a very good use case for generic 8913 07:10:57,958 --> 07:11:03,398 Now by default it's actually going to be 8914 07:11:00,200 --> 07:11:06,520 set equal to to unknown so this will be 8915 07:11:03,398 --> 07:11:09,878 the default value and then notice how 8916 07:11:06,520 --> 07:11:13,000 they use this intersection type so 8917 07:11:09,878 --> 07:11:16,798 essentially whatever is passed over here 8918 07:11:13,000 --> 07:11:20,680 is just combined with what with children 8919 07:11:16,798 --> 07:11:23,280 property which is set equal to optional 8920 07:11:20,680 --> 07:11:25,718 so it might be passed might not and the 8921 07:11:23,280 --> 07:11:28,760 type here is going to be equal to react 8922 07:11:25,718 --> 07:11:31,920 node or it's going to be equal to 8923 07:11:28,760 --> 07:11:33,440 undefined so let's try it out 8924 07:11:31,920 --> 07:11:36,200 I think I'm going to leave this one for 8925 07:11:33,440 --> 07:11:41,920 your reference so let me just copy and 8926 07:11:36,200 --> 07:11:44,440 paste let me comment out first one over 8927 07:11:41,920 --> 07:11:49,878 here and then instead 8928 07:11:44,440 --> 07:11:52,440 of setting up the children property 8929 07:11:49,878 --> 07:11:55,840 ourselves we'll just go with props with 8930 07:11:52,440 --> 07:11:58,200 children and then we'll provide this 8931 07:11:55,840 --> 07:12:00,240 value over here so effectively it's 8932 07:11:58,200 --> 07:12:02,040 going to look like this we're going to 8933 07:12:00,240 --> 07:12:05,520 go with props with children remember 8934 07:12:02,040 --> 07:12:09,440 it's a generic and it's looking for that 8935 07:12:05,520 --> 07:12:13,360 type and the moment we do that as far as 8936 07:12:09,440 --> 07:12:16,398 the result notice we get back whatever 8937 07:12:13,360 --> 07:12:19,958 type we pass in and it's just combined 8938 07:12:16,398 --> 07:12:24,040 with this children property and with 8939 07:12:19,958 --> 07:12:26,440 this in place now I can go to the app 8940 07:12:24,040 --> 07:12:29,000 remove the heading 2 or provide the 8941 07:12:26,440 --> 07:12:30,558 heading 2 to the second instance and 8942 07:12:29,000 --> 07:12:33,280 everything is going to work and 8943 07:12:30,558 --> 07:12:36,000 typescript is not going to complain so 8944 07:12:33,280 --> 07:12:39,760 again when we work with children prop we 8945 07:12:36,000 --> 07:12:43,080 have few options either we can 8946 07:12:39,760 --> 07:12:47,240 explicitly provide the value of react 8947 07:12:43,080 --> 07:12:48,878 node ourselves or we can import the 8948 07:12:47,240 --> 07:12:51,280 props with children which essentially 8949 07:12:48,878 --> 07:12:53,360 does the same thing behind the scenes 8950 07:12:51,280 --> 07:12:56,638 all right and once we're clear on 8951 07:12:53,360 --> 07:12:59,440 component returns props up next let's 8952 07:12:56,638 --> 07:13:02,200 talk about state so first we want to 8953 07:12:59,440 --> 07:13:05,240 change the import and and this is 8954 07:13:02,200 --> 07:13:07,760 actually going to trigger some 8955 07:13:05,240 --> 07:13:09,958 typescript errors so I think I'm just 8956 07:13:07,760 --> 07:13:13,920 going to go with good old component for 8957 07:13:09,958 --> 07:13:16,160 now so let me render the component yep 8958 07:13:13,920 --> 07:13:17,878 typescript still complains because we 8959 07:13:16,160 --> 07:13:21,000 haven't changed the import so we're 8960 07:13:17,878 --> 07:13:24,040 looking for a 03 State and now we render 8961 07:13:21,000 --> 07:13:28,398 the component so now let's navigate to 8962 07:13:24,040 --> 07:13:30,718 this particular component and let's just 8963 07:13:28,398 --> 07:13:33,600 I guess set up heading two with some 8964 07:13:30,718 --> 07:13:36,040 kind of value as well as the button 8965 07:13:33,600 --> 07:13:38,600 because inside of the onclick we'll try 8966 07:13:36,040 --> 07:13:40,600 to change the state values so I think I 8967 07:13:38,600 --> 07:13:43,558 want to do that first and then we'll 8968 07:13:40,600 --> 07:13:46,920 worry about the actual logic so in here 8969 07:13:43,558 --> 07:13:49,718 inside of the div I want to go with 8970 07:13:46,920 --> 07:13:52,280 heading two I'll add one of the classes 8971 07:13:49,718 --> 07:13:55,240 so margin bottom is going to be set to 8972 07:13:52,280 --> 07:13:56,320 one and I guess I'm just going to leave 8973 07:13:55,240 --> 07:14:00,520 this one over 8974 07:13:56,320 --> 07:14:02,240 here then let me go with a button let me 8975 07:14:00,520 --> 07:14:05,280 remove this heading 8976 07:14:02,240 --> 07:14:08,398 to and let me add some classes just so 8977 07:14:05,280 --> 07:14:12,958 we have some basic styling so 8978 07:14:08,398 --> 07:14:15,920 BTN Center over here and then let's just 8979 07:14:12,958 --> 07:14:18,000 right click me and eventually we will 8980 07:14:15,920 --> 07:14:20,080 set up the on click let's not worry 8981 07:14:18,000 --> 07:14:24,440 about it right now this is what we 8982 07:14:20,080 --> 07:14:26,320 should see on the screen now I don't 8983 07:14:24,440 --> 07:14:31,520 know whether you remember but actually 8984 07:14:26,320 --> 07:14:33,478 we already covered sort of the Ed state 8985 07:14:31,520 --> 07:14:36,240 during the 8986 07:14:33,478 --> 07:14:38,878 fundamentals we talked about it when we 8987 07:14:36,240 --> 07:14:41,398 discussed the generics but let's go over 8988 07:14:38,878 --> 07:14:45,760 it one more time so yes we can nicely 8989 07:14:41,398 --> 07:14:48,558 use use state in react to control the 8990 07:14:45,760 --> 07:14:50,120 local state but if we take a look at use 8991 07:14:48,558 --> 07:14:52,600 State it's actually going to be a 8992 07:14:50,120 --> 07:14:55,600 generic so let's start I guess with the 8993 07:14:52,600 --> 07:14:58,478 import so looking for use State it's 8994 07:14:55,600 --> 07:15:01,638 coming from the react and let me just 8995 07:14:58,478 --> 07:15:04,478 hover over it notice this is going to be 8996 07:15:01,638 --> 07:15:09,160 the type definition it's a function it's 8997 07:15:04,478 --> 07:15:12,160 a generic and essentially we can either 8998 07:15:09,160 --> 07:15:15,040 pass in the initial state or we can pass 8999 07:15:12,160 --> 07:15:18,718 in the function and as far as the return 9000 07:15:15,040 --> 07:15:21,798 notice it Returns the value we provide 9001 07:15:18,718 --> 07:15:24,320 in the initial State and the function 9002 07:15:21,798 --> 07:15:27,040 that controls it so where I'm going with 9003 07:15:24,320 --> 07:15:29,160 this if we have just primitive values in 9004 07:15:27,040 --> 07:15:32,040 the use State you'll see that typescript 9005 07:15:29,160 --> 07:15:34,680 is very smart and it's going to infer 9006 07:15:32,040 --> 07:15:38,040 the type so for example if I'm going to 9007 07:15:34,680 --> 07:15:41,798 go over here with text and set text and 9008 07:15:38,040 --> 07:15:44,558 set it equal to my use State I don't 9009 07:15:41,798 --> 07:15:46,718 need to set up over here the type and 9010 07:15:44,558 --> 07:15:49,040 say that is going to be a string because 9011 07:15:46,718 --> 07:15:53,520 typ script is going to know that so if 9012 07:15:49,040 --> 07:15:56,200 I'm going to go here with shake and bake 9013 07:15:53,520 --> 07:15:58,520 and if I'll have our over text step 9014 07:15:56,200 --> 07:16:01,000 script knows that it's a string so 9015 07:15:58,520 --> 07:16:04,040 essentially whenever I will try to 9016 07:16:01,000 --> 07:16:06,080 update this value it's going to expect 9017 07:16:04,040 --> 07:16:09,160 the string and we can actually see that 9018 07:16:06,080 --> 07:16:11,000 over here if we go with on click and 9019 07:16:09,160 --> 07:16:14,200 let's not worry about how we can handle 9020 07:16:11,000 --> 07:16:16,920 events in react we'll cover that a 9021 07:16:14,200 --> 07:16:19,840 little bit later for now let's just pass 9022 07:16:16,920 --> 07:16:24,280 in our Arrow function just so we can 9023 07:16:19,840 --> 07:16:27,718 invoke set text and if I'm going to 9024 07:16:24,280 --> 07:16:29,600 provide a wrong type it's not going to 9025 07:16:27,718 --> 07:16:32,638 work so notice again as we're hovering 9026 07:16:29,600 --> 07:16:35,040 over what is the set State action 9027 07:16:32,638 --> 07:16:37,878 function looking for it's looking for 9028 07:16:35,040 --> 07:16:39,798 type string so it's going to be okay if 9029 07:16:37,878 --> 07:16:42,600 I go here with typescript yeah there are 9030 07:16:39,798 --> 07:16:44,718 no complaints but if I'm going to 9031 07:16:42,600 --> 07:16:47,878 provide the number immediately 9032 07:16:44,718 --> 07:16:50,000 typescript complaints again the logic 9033 07:16:47,878 --> 07:16:52,120 technically is going to work during 9034 07:16:50,000 --> 07:16:54,478 development I can navigate over here 9035 07:16:52,120 --> 07:16:57,840 press and technically I shouldn't have 9036 07:16:54,478 --> 07:17:01,398 any errors in a console nope everything 9037 07:16:57,840 --> 07:17:03,638 is fine but typescript is not happy with 9038 07:17:01,398 --> 07:17:05,718 me and the same is going to work if 9039 07:17:03,638 --> 07:17:08,280 let's say I'm going to set up the number 9040 07:17:05,718 --> 07:17:11,160 so let me navigate over here and instead 9041 07:17:08,280 --> 07:17:14,000 of text we're going to go with number 9042 07:17:11,160 --> 07:17:17,680 and set 9043 07:17:14,000 --> 07:17:19,920 number okay good and then we'll provide 9044 07:17:17,680 --> 07:17:22,040 the Primitive value over 9045 07:17:19,920 --> 07:17:24,200 here and you know what let me just 9046 07:17:22,040 --> 07:17:26,240 comment this one out for your reference 9047 07:17:24,200 --> 07:17:29,040 if we're going to go with set number 9048 07:17:26,240 --> 07:17:31,440 again we can see that set State action 9049 07:17:29,040 --> 07:17:35,320 function which essentially is a type for 9050 07:17:31,440 --> 07:17:38,680 this one for our set number function in 9051 07:17:35,320 --> 07:17:41,878 this case is expecting a number because 9052 07:17:38,680 --> 07:17:43,360 typescript is going to infer the type if 9053 07:17:41,878 --> 07:17:45,558 I'm going to provide it if I'm going to 9054 07:17:43,360 --> 07:17:46,840 say 23 yeah everything's awesome if I'm 9055 07:17:45,558 --> 07:17:49,680 going to go with some kind of string 9056 07:17:46,840 --> 07:17:52,280 value it's not going to work now it 9057 07:17:49,680 --> 07:17:55,120 doesn't stop me from explicitly going 9058 07:17:52,280 --> 07:17:57,638 over here and saying number I mean we 9059 07:17:55,120 --> 07:17:59,878 can still definitely do so but again the 9060 07:17:57,638 --> 07:18:02,958 whole point is that typescript is going 9061 07:17:59,878 --> 07:18:05,000 to do a very good job of inferring such 9062 07:18:02,958 --> 07:18:07,520 type now it's going to be a different 9063 07:18:05,000 --> 07:18:10,878 scenario if let's say we have empty 9064 07:18:07,520 --> 07:18:14,798 array as our initial value so let's say 9065 07:18:10,878 --> 07:18:17,120 we have over here a list then comma and 9066 07:18:14,798 --> 07:18:20,798 set list and we set it equal to use 9067 07:18:17,120 --> 07:18:24,160 State an empty array notice how 9068 07:18:20,798 --> 07:18:28,320 typescript essentially sets list to an 9069 07:18:24,160 --> 07:18:30,840 array but the type is equal to a never 9070 07:18:28,320 --> 07:18:33,840 and at this point we have few options 9071 07:18:30,840 --> 07:18:36,360 either we provide some values over here 9072 07:18:33,840 --> 07:18:38,760 so typescript knows what the type is 9073 07:18:36,360 --> 07:18:40,958 going to be so for example I can go with 9074 07:18:38,760 --> 07:18:43,478 hello but keep in mind there are 9075 07:18:40,958 --> 07:18:46,718 instances where you want the default 9076 07:18:43,478 --> 07:18:48,920 value to be empty array so I'm not going 9077 07:18:46,718 --> 07:18:52,000 to provide some random hello just to 9078 07:18:48,920 --> 07:18:55,760 make typescript happy so the solution in 9079 07:18:52,000 --> 07:18:57,520 this case is to use the generic approach 9080 07:18:55,760 --> 07:19:00,478 where we know that US state is in a 9081 07:18:57,520 --> 07:19:02,280 generic and we'll just say all right 9082 07:19:00,478 --> 07:19:06,000 yeah yeah it's an array but it's going 9083 07:19:02,280 --> 07:19:09,680 to be an array of strings and at that 9084 07:19:06,000 --> 07:19:12,398 point if I'm going to go to my on click 9085 07:19:09,680 --> 07:19:14,280 and go with my set list again my 9086 07:19:12,398 --> 07:19:16,878 function everything is going to be 9087 07:19:14,280 --> 07:19:19,120 awesome if I'm going to provide the 9088 07:19:16,878 --> 07:19:22,958 strings over here I'm going to go with 9089 07:19:19,120 --> 07:19:27,920 hello and maybe I'll also add over here 9090 07:19:22,958 --> 07:19:31,240 world so this is going to fly because I 9091 07:19:27,920 --> 07:19:32,958 have strings in my array however if 9092 07:19:31,240 --> 07:19:36,760 we're going to change this around and 9093 07:19:32,958 --> 07:19:39,200 we'll go with one and three notice 9094 07:19:36,760 --> 07:19:41,920 typescript is not happy because it's 9095 07:19:39,200 --> 07:19:44,798 expecting a type string so this is how 9096 07:19:41,920 --> 07:19:46,840 we can work with use State and 9097 07:19:44,798 --> 07:19:49,600 typescript by default typescript is 9098 07:19:46,840 --> 07:19:51,080 going to infer the type but it is a 9099 07:19:49,600 --> 07:19:54,000 generic and there's going to be 9100 07:19:51,080 --> 07:19:57,200 instances where we'll need to 9101 07:19:54,000 --> 07:20:00,798 explicitly provide the type that we 9102 07:19:57,200 --> 07:20:03,558 expect for the value and automatically 9103 07:20:00,798 --> 07:20:06,440 is going to be set for the set function 9104 07:20:03,558 --> 07:20:10,760 as well okay and up next let's cover an 9105 07:20:06,440 --> 07:20:13,558 example where we have a list of objects 9106 07:20:10,760 --> 07:20:17,520 now in my case I'm going to create nav 9107 07:20:13,558 --> 07:20:19,638 links but again the actual data is 9108 07:20:17,520 --> 07:20:23,040 totally up to you so I'm going to go 9109 07:20:19,638 --> 07:20:25,638 with naab links that will be my array 9110 07:20:23,040 --> 07:20:29,120 and I'm just going to provide some dummy 9111 07:20:25,638 --> 07:20:31,760 data and yes I will in the beginning 9112 07:20:29,120 --> 07:20:34,958 rely on the fact that typescript is 9113 07:20:31,760 --> 07:20:38,840 going to be able to infert a type so in 9114 07:20:34,958 --> 07:20:40,958 here I'm going to go with id1 then URL 9115 07:20:38,840 --> 07:20:43,040 I'll just type some URL so essentially 9116 07:20:40,958 --> 07:20:47,240 it's going to be some kind of string and 9117 07:20:43,040 --> 07:20:51,320 also text will be some kind of text so 9118 07:20:47,240 --> 07:20:55,080 some text okay let's save it then we 9119 07:20:51,320 --> 07:20:57,718 want to copy and paste so one and two 9120 07:20:55,080 --> 07:21:00,718 let's just change these values over here 9121 07:20:57,718 --> 07:21:02,760 two and three and I don't think think 9122 07:21:00,718 --> 07:21:05,760 I'm going to change any of the actual 9123 07:21:02,760 --> 07:21:08,760 values I don't think it's relevant now I 9124 07:21:05,760 --> 07:21:12,958 do want to set up the type type will be 9125 07:21:08,760 --> 07:21:16,280 equal to a link I'll say that ID is a 9126 07:21:12,958 --> 07:21:20,840 number okay awesome then URL will be a 9127 07:21:16,280 --> 07:21:23,680 string and then text will be a string as 9128 07:21:20,840 --> 07:21:25,798 well let me save it I'm not going to set 9129 07:21:23,680 --> 07:21:27,760 it up over here this is something we're 9130 07:21:25,798 --> 07:21:30,798 going to do later and now let's see how 9131 07:21:27,760 --> 07:21:35,080 we can work with that in our use state 9132 07:21:30,798 --> 07:21:37,200 so in here I'll call this links and set 9133 07:21:35,080 --> 07:21:38,958 links that will be my function name 9134 07:21:37,200 --> 07:21:43,000 we're going to go with use state for now 9135 07:21:38,958 --> 07:21:45,600 let's not set the type explicitly and 9136 07:21:43,000 --> 07:21:48,718 now let's just provide the nav links and 9137 07:21:45,600 --> 07:21:52,000 again typescript is super smart now it 9138 07:21:48,718 --> 07:21:55,120 knows that essentially links has this 9139 07:21:52,000 --> 07:21:58,440 shape and essentially we can go over 9140 07:21:55,120 --> 07:22:01,478 here where we have our own click we can 9141 07:21:58,440 --> 07:22:04,240 invoke set links let's say that I just 9142 07:22:01,478 --> 07:22:06,558 want to add it to an existing array so 9143 07:22:04,240 --> 07:22:08,760 I'm going to spread out all the current 9144 07:22:06,558 --> 07:22:11,840 links and I'm going to try to add a new 9145 07:22:08,760 --> 07:22:13,840 one if the values I provide don't match 9146 07:22:11,840 --> 07:22:16,958 the signature you'll see that typescript 9147 07:22:13,840 --> 07:22:20,160 will complain so if I'm going to go with 9148 07:22:16,958 --> 07:22:23,878 id4 then URL is going to be equal to 9149 07:22:20,160 --> 07:22:26,478 hello and then also a comma and I'm 9150 07:22:23,878 --> 07:22:30,878 going to go with I don't know what was 9151 07:22:26,478 --> 07:22:33,120 the last one I believe it was text right 9152 07:22:30,878 --> 07:22:35,240 if I'm going to say hello yeah 9153 07:22:33,120 --> 07:22:39,680 everything's awesome because this 9154 07:22:35,240 --> 07:22:43,318 matches the signature however if I'm 9155 07:22:39,680 --> 07:22:46,200 going to Omit text notice tapescript 9156 07:22:43,318 --> 07:22:48,920 immediately is going to complain because 9157 07:22:46,200 --> 07:22:51,878 the text property is missing so all of 9158 07:22:48,920 --> 07:22:55,240 this is nice the problem 9159 07:22:51,878 --> 07:22:58,798 becomes this is inferred and remember 9160 07:22:55,240 --> 07:23:01,600 when typescript infers something it 9161 07:22:58,798 --> 07:23:04,718 infers based on what on the original 9162 07:23:01,600 --> 07:23:07,440 value so for example if I just go and 9163 07:23:04,718 --> 07:23:09,440 comment this one out now notice 9164 07:23:07,440 --> 07:23:11,878 typescript thinks that yeah links 9165 07:23:09,440 --> 07:23:14,798 technically can be an array of objects 9166 07:23:11,878 --> 07:23:17,240 with this shape or with this shape where 9167 07:23:14,798 --> 07:23:19,680 text is actually missing so now if I'm 9168 07:23:17,240 --> 07:23:22,798 going to go here I'm actually going to 9169 07:23:19,680 --> 07:23:26,120 be successful so this is a good use case 9170 07:23:22,798 --> 07:23:28,600 where we want to go with our angle 9171 07:23:26,120 --> 07:23:31,360 brackets and we want to say 9172 07:23:28,600 --> 07:23:33,280 yeah we have these links over here but 9173 07:23:31,360 --> 07:23:36,200 actually the type is going to be 9174 07:23:33,280 --> 07:23:39,000 following it's going to be an array of 9175 07:23:36,200 --> 07:23:41,240 Link type so now typescript is going to 9176 07:23:39,000 --> 07:23:43,680 complain it's going to say Hey listen 9177 07:23:41,240 --> 07:23:45,878 you know whatever values you provided 9178 07:23:43,680 --> 07:23:48,478 they don't match so we need to fix it 9179 07:23:45,878 --> 07:23:51,280 over here and now the same deal is going 9180 07:23:48,478 --> 07:23:55,120 to be in our on click as well we'll have 9181 07:23:51,280 --> 07:23:57,920 to provide I believe it was our text one 9182 07:23:55,120 --> 07:24:01,200 now lastly I just want to mention that 9183 07:23:57,920 --> 07:24:04,440 technically we can avoid all of this 9184 07:24:01,200 --> 07:24:06,878 debacle pretty much if we just go with 9185 07:24:04,440 --> 07:24:09,318 our type annotation over here and we say 9186 07:24:06,878 --> 07:24:12,240 with the link then of course I'm not 9187 07:24:09,318 --> 07:24:13,558 going to be able to remove the property 9188 07:24:12,240 --> 07:24:16,280 but keep in mind there's going to be 9189 07:24:13,558 --> 07:24:18,878 some instances where you're not going to 9190 07:24:16,280 --> 07:24:22,160 be setting that data so again that's why 9191 07:24:18,878 --> 07:24:24,680 it's super useful to use the angle 9192 07:24:22,160 --> 07:24:27,798 brackets and to explicitly let 9193 07:24:24,680 --> 07:24:30,600 typescript know that the shape You're 9194 07:24:27,798 --> 07:24:33,080 Expecting is going to be following and 9195 07:24:30,600 --> 07:24:37,520 that way essentially later when you need 9196 07:24:33,080 --> 07:24:40,478 to update it's going to be very hard to 9197 07:24:37,520 --> 07:24:45,000 provide the wrong data and up next let's 9198 07:24:40,478 --> 07:24:47,080 see how we can handle events in react 9199 07:24:45,000 --> 07:24:49,478 for starters we want to change the 9200 07:24:47,080 --> 07:24:54,080 import so we're looking for 9201 07:24:49,478 --> 07:24:56,120 starter and the number four events one 9202 07:24:54,080 --> 07:24:58,798 the component is not expecting anything 9203 07:24:56,120 --> 07:25:01,680 because we just have the bare bone setup 9204 07:24:58,798 --> 07:25:04,280 with the div and all that and initially 9205 07:25:01,680 --> 07:25:06,600 I just want to set up two controlled 9206 07:25:04,280 --> 07:25:10,440 inputs one is going to be for text and 9207 07:25:06,600 --> 07:25:12,280 one will be for email and of course in 9208 07:25:10,440 --> 07:25:15,080 the process you'll see why we have two 9209 07:25:12,280 --> 07:25:18,200 of them so let's do that let's go with 9210 07:25:15,080 --> 07:25:21,600 use state that is coming from react 9211 07:25:18,200 --> 07:25:24,200 awesome then let's go with our text and 9212 07:25:21,600 --> 07:25:27,160 email and yes I will rely on the fact 9213 07:25:24,200 --> 07:25:29,398 that typescript can nicely infer the 9214 07:25:27,160 --> 07:25:30,878 Primitive types so I'm just going to go 9215 07:25:29,398 --> 07:25:33,638 with empty value 9216 07:25:30,878 --> 07:25:38,040 that will be my string and the same is 9217 07:25:33,638 --> 07:25:41,920 going to be for email let's provide the 9218 07:25:38,040 --> 07:25:45,840 name here and let's go with email let's 9219 07:25:41,920 --> 07:25:49,478 save this and now we want to set up two 9220 07:25:45,840 --> 07:25:51,240 inputs inside of the form again just for 9221 07:25:49,478 --> 07:25:54,718 better styling and all that I'll wrap 9222 07:25:51,240 --> 07:25:58,958 this into a section then we'll provide a 9223 07:25:54,718 --> 07:26:01,478 heading to I'll call this events 9224 07:25:58,958 --> 07:26:04,600 example and we want to go with our form 9225 07:26:01,478 --> 07:26:07,920 for now no action don't care about that 9226 07:26:04,600 --> 07:26:11,360 but we do want to add a class of form 9227 07:26:07,920 --> 07:26:14,000 and inside of it let's go with input 9228 07:26:11,360 --> 07:26:16,718 type will be equal to text let's just 9229 07:26:14,000 --> 07:26:19,638 add few classes here we're looking for 9230 07:26:16,718 --> 07:26:21,840 form input and margin bottom is going to 9231 07:26:19,638 --> 07:26:25,240 be set to 9232 07:26:21,840 --> 07:26:28,000 one since I'm creating over here a 9233 07:26:25,240 --> 07:26:32,840 controlled input I'm going to go with 9234 07:26:28,000 --> 07:26:35,398 value and on change so let me go with 9235 07:26:32,840 --> 07:26:38,478 value so that will be equal to my state 9236 07:26:35,398 --> 07:26:40,878 value so I'll set it equal to my text 9237 07:26:38,478 --> 07:26:43,040 and then also we have unchange which 9238 07:26:40,878 --> 07:26:45,520 essentially controls that value so we're 9239 07:26:43,040 --> 07:26:49,080 going to go over here with unchange in 9240 07:26:45,520 --> 07:26:52,000 here I'm going to pass in the arrow 9241 07:26:49,080 --> 07:26:56,398 function and remember we have access to 9242 07:26:52,000 --> 07:27:00,280 the event and now let's go with our set 9243 07:26:56,398 --> 07:27:03,440 text and let's pass in event. target. 9244 07:27:00,280 --> 07:27:06,080 value so again this is typical setup for 9245 07:27:03,440 --> 07:27:07,280 controlled inputs in react there's 9246 07:27:06,080 --> 07:27:10,440 nothing really 9247 07:27:07,280 --> 07:27:12,760 unique now we want to copy and paste and 9248 07:27:10,440 --> 07:27:14,440 we want to set up one for email the 9249 07:27:12,760 --> 07:27:16,558 difference we're not going to use the 9250 07:27:14,440 --> 07:27:19,680 arrow function we'll actually set up the 9251 07:27:16,558 --> 07:27:21,798 function reference so type yep still 9252 07:27:19,680 --> 07:27:25,120 equal to or you know what no it's 9253 07:27:21,798 --> 07:27:27,680 actually going to be email then the 9254 07:27:25,120 --> 07:27:30,638 class name for input yeah that stays the 9255 07:27:27,680 --> 07:27:33,638 same value will be email so this is 9256 07:27:30,638 --> 07:27:36,080 coming from the state and 9257 07:27:33,638 --> 07:27:38,520 also on change like I said there will be 9258 07:27:36,080 --> 07:27:40,878 a reference so you know what for now I 9259 07:27:38,520 --> 07:27:43,280 guess let's just remove it even though 9260 07:27:40,878 --> 07:27:45,920 most likely it's going to complain then 9261 07:27:43,280 --> 07:27:48,920 let's go with our button and Just for 9262 07:27:45,920 --> 07:27:50,958 kicks let's go with type and let's set 9263 07:27:48,920 --> 07:27:54,440 it equal to submit then let's add some 9264 07:27:50,958 --> 07:27:57,080 classes BTN and BTN block and let's 9265 07:27:54,440 --> 07:27:59,478 write over here 9266 07:27:57,080 --> 07:28:02,958 submit and actually this is something 9267 07:27:59,478 --> 07:28:05,680 that we covered previously when we 9268 07:28:02,958 --> 07:28:09,680 worked on the tasks application during 9269 07:28:05,680 --> 07:28:12,040 fundamentals you see in here when we 9270 07:28:09,680 --> 07:28:15,638 provide the arrow 9271 07:28:12,040 --> 07:28:17,680 function T script will nicely provide 9272 07:28:15,638 --> 07:28:21,398 the value for the 9273 07:28:17,680 --> 07:28:25,398 event as you can see it's react. change 9274 07:28:21,398 --> 07:28:28,520 event so that's the type we get from our 9275 07:28:25,398 --> 07:28:31,440 declaration files and it's a generic and 9276 07:28:28,520 --> 07:28:35,878 essentially we provide over here 9277 07:28:31,440 --> 07:28:38,558 the element the HTML input element and 9278 07:28:35,878 --> 07:28:40,478 this gives us a good clue what we need 9279 07:28:38,558 --> 07:28:43,920 to provide over here in the function 9280 07:28:40,478 --> 07:28:46,718 reference so if I'm going to navigate up 9281 07:28:43,920 --> 07:28:50,318 and I'm going to go with const and then 9282 07:28:46,718 --> 07:28:53,638 handle change function and if I'm going 9283 07:28:50,318 --> 07:28:55,318 to try to access the event typescript is 9284 07:28:53,638 --> 07:28:58,600 going to complain and it's going to say 9285 07:28:55,318 --> 07:29:01,240 Hey listen this is type any so for now 9286 07:28:58,600 --> 07:29:04,120 let's not worry about it let's just go 9287 07:29:01,240 --> 07:29:05,798 with event. target. Val just so you can 9288 07:29:04,120 --> 07:29:07,440 see that we can access that and let's 9289 07:29:05,798 --> 07:29:10,680 also go with our 9290 07:29:07,440 --> 07:29:15,958 set email and we want to provide the 9291 07:29:10,680 --> 07:29:19,920 same value so event. target. 9292 07:29:15,958 --> 07:29:21,638 value now let me navigate to my browser 9293 07:29:19,920 --> 07:29:24,200 so at the moment it's just complaining 9294 07:29:21,638 --> 07:29:26,558 because we haven't set the onchange this 9295 07:29:24,200 --> 07:29:29,080 is our form notice I can provide the 9296 07:29:26,558 --> 07:29:32,478 value but I cannot do the same for the 9297 07:29:29,080 --> 07:29:34,958 email and in order to provide the 9298 07:29:32,478 --> 07:29:37,360 correct type again we simply want to 9299 07:29:34,958 --> 07:29:39,398 reference what we have over here so 9300 07:29:37,360 --> 07:29:43,040 notice this change 9301 07:29:39,398 --> 07:29:46,478 event we want to go with event then 9302 07:29:43,040 --> 07:29:49,280 react Dot and then we're looking for the 9303 07:29:46,478 --> 07:29:51,958 change event and if we take a look at 9304 07:29:49,280 --> 07:29:54,718 the docs if we take a look at the type 9305 07:29:51,958 --> 07:29:56,840 definition we can see that change event 9306 07:29:54,718 --> 07:30:00,000 type is a 9307 07:29:56,840 --> 07:30:04,200 generic it's expecting a type which is 9308 07:30:00,000 --> 07:30:07,520 going to be the HTML element and the 9309 07:30:04,200 --> 07:30:10,440 default value is going to be element now 9310 07:30:07,520 --> 07:30:12,878 where we get back is Target and that 9311 07:30:10,440 --> 07:30:17,478 will be the intersection type between 9312 07:30:12,878 --> 07:30:20,718 the event Target and the type we provide 9313 07:30:17,478 --> 07:30:23,638 so now let's navigate back to our index 9314 07:30:20,718 --> 07:30:27,520 DSX and set up the angle brackets and 9315 07:30:23,638 --> 07:30:30,958 provide the correct element so we're 9316 07:30:27,520 --> 07:30:34,160 going to set this change event on H DML 9317 07:30:30,958 --> 07:30:36,520 and input element so now we have access 9318 07:30:34,160 --> 07:30:39,240 to the Target as well as the value so 9319 07:30:36,520 --> 07:30:43,878 this is what we need to do when we set 9320 07:30:39,240 --> 07:30:46,798 up the function as a reference again if 9321 07:30:43,878 --> 07:30:49,360 we just provide the inline 9322 07:30:46,798 --> 07:30:51,558 one everything is going to be set 9323 07:30:49,360 --> 07:30:54,160 already for us so now let me just 9324 07:30:51,558 --> 07:30:57,240 navigate over here and we're going to go 9325 07:30:54,160 --> 07:30:59,920 with on change and we'll set it equal to 9326 07:30:57,240 --> 07:31:02,680 anal change and with this in place that 9327 07:30:59,920 --> 07:31:05,280 typescript doesn't complain and also we 9328 07:31:02,680 --> 07:31:07,120 have the correct values in the browser 9329 07:31:05,280 --> 07:31:09,040 so let me go over here notice I'm able 9330 07:31:07,120 --> 07:31:12,120 to provide a value and the same is going 9331 07:31:09,040 --> 07:31:15,000 to be with my email okay and while we're 9332 07:31:12,120 --> 07:31:18,318 still on the topic of events let's cover 9333 07:31:15,000 --> 07:31:21,478 submit event and few things we need to 9334 07:31:18,318 --> 07:31:26,798 be aware of when we work with form data 9335 07:31:21,478 --> 07:31:30,398 API so let's say we want to handle the 9336 07:31:26,798 --> 07:31:33,520 form submissions we know that in react 9337 07:31:30,398 --> 07:31:37,120 there is onsubmit and same deal we can 9338 07:31:33,520 --> 07:31:39,718 either pass in the inline function or we 9339 07:31:37,120 --> 07:31:44,240 can set up the reference and just like 9340 07:31:39,718 --> 07:31:46,638 with on change event essentially if we 9341 07:31:44,240 --> 07:31:49,878 pass here the inline typescript will 9342 07:31:46,638 --> 07:31:52,040 right away infer the correct type if we 9343 07:31:49,878 --> 07:31:54,638 set up the function 9344 07:31:52,040 --> 07:31:57,000 reference then we'll need to do it 9345 07:31:54,638 --> 07:32:00,398 basically manually for 9346 07:31:57,000 --> 07:32:03,718 ourselves and you'll see that the form 9347 07:32:00,398 --> 07:32:06,080 event is very similar to change event 9348 07:32:03,718 --> 07:32:08,360 it's also going to be a generic and 9349 07:32:06,080 --> 07:32:13,080 we'll just need to provide the correct 9350 07:32:08,360 --> 07:32:15,558 HTML element and I think I'll right away 9351 07:32:13,080 --> 07:32:17,638 set up the function reference but you 9352 07:32:15,558 --> 07:32:20,240 know what before we do that let me just 9353 07:32:17,638 --> 07:32:22,638 mention that in between the videos I 9354 07:32:20,240 --> 07:32:26,240 added two 9355 07:32:22,638 --> 07:32:28,958 attributes one on each input and it was 9356 07:32:26,240 --> 07:32:32,318 the name attribute so in here I just set 9357 07:32:28,958 --> 07:32:34,520 the value for text and in here for email 9358 07:32:32,318 --> 07:32:37,360 simply because we will use the form data 9359 07:32:34,520 --> 07:32:40,600 in this video as well and in order to 9360 07:32:37,360 --> 07:32:42,718 access those values we need to add those 9361 07:32:40,600 --> 07:32:44,200 name attributes now I'm fully aware that 9362 07:32:42,718 --> 07:32:47,240 these are controlled inputs so 9363 07:32:44,200 --> 07:32:49,478 technically when we submit the form we 9364 07:32:47,240 --> 07:32:51,600 can access both of these values directly 9365 07:32:49,478 --> 07:32:53,600 but I do want to showcase some things 9366 07:32:51,600 --> 07:32:55,280 about form data and therefore I added 9367 07:32:53,600 --> 07:32:58,680 these attributes so if you want to 9368 07:32:55,280 --> 07:33:00,478 follow along I suggest you do the same 9369 07:32:58,680 --> 07:33:03,920 so like I said I'll right away set up 9370 07:33:00,478 --> 07:33:06,478 the reference let me go here with on 9371 07:33:03,920 --> 07:33:09,478 submit and I'm going to set it equal to 9372 07:33:06,478 --> 07:33:13,440 handle submit a function I don't have 9373 07:33:09,478 --> 07:33:14,760 yet so let me go with handle submit and 9374 07:33:13,440 --> 07:33:17,240 what is 9375 07:33:14,760 --> 07:33:19,478 typically the first thing we want to do 9376 07:33:17,240 --> 07:33:22,318 that's correct we want to prevent the 9377 07:33:19,478 --> 07:33:24,798 default so for now again I just pass in 9378 07:33:22,318 --> 07:33:27,718 the event and since typescript has no 9379 07:33:24,798 --> 07:33:29,360 idea what e I'm talking about it's going 9380 07:33:27,718 --> 07:33:32,200 to complain and it's going to say that 9381 07:33:29,360 --> 07:33:34,240 the parameter any so first let me just 9382 07:33:32,200 --> 07:33:36,920 set up the logic 9383 07:33:34,240 --> 07:33:40,920 prevent default and if we want to get 9384 07:33:36,920 --> 07:33:44,398 the type again we can always use the 9385 07:33:40,920 --> 07:33:47,440 editor as our best friend so notice 9386 07:33:44,398 --> 07:33:50,558 essentially we have over here the HTML 9387 07:33:47,440 --> 07:33:53,958 form element and then we're looking for 9388 07:33:50,558 --> 07:33:58,240 form event or you can pass in the inline 9389 07:33:53,958 --> 07:34:00,360 and you'll get the exact event type so 9390 07:33:58,240 --> 07:34:03,000 in here where we have the event we want 9391 07:34:00,360 --> 07:34:05,638 to go again with react or we just import 9392 07:34:03,000 --> 07:34:09,160 this type then we're looking for 9393 07:34:05,638 --> 07:34:13,440 form it's a generic and in here we 9394 07:34:09,160 --> 07:34:16,680 simply pass in HTML form and element and 9395 07:34:13,440 --> 07:34:19,558 now we have the correct type so now we 9396 07:34:16,680 --> 07:34:23,840 can use all the properties and methods 9397 07:34:19,558 --> 07:34:26,440 and typescript is going to be happy and 9398 07:34:23,840 --> 07:34:32,718 not going to complain now as far as form 9399 07:34:26,440 --> 07:34:36,200 data it's a nice interface to handle AE 9400 07:34:32,718 --> 07:34:38,558 if you have bunch of form inputs and you 9401 07:34:36,200 --> 07:34:41,398 don't want to deal with controlled input 9402 07:34:38,558 --> 07:34:43,840 so you don't want to set up the state 9403 07:34:41,398 --> 07:34:47,600 variables you don't want to set up the 9404 07:34:43,840 --> 07:34:50,080 on change as well as the value then it's 9405 07:34:47,600 --> 07:34:53,558 pretty straightforward just set up the 9406 07:34:50,080 --> 07:34:55,638 form add those name attributes and 9407 07:34:53,558 --> 07:34:59,120 you're good to go once you submit the 9408 07:34:55,638 --> 07:35:01,600 form you'll have access to it by 9409 07:34:59,120 --> 07:35:05,958 creating a a new instance of form data 9410 07:35:01,600 --> 07:35:08,478 so in here we want to go const form data 9411 07:35:05,958 --> 07:35:12,080 we want to set it equal to new 9412 07:35:08,478 --> 07:35:13,638 form data so this is right away 9413 07:35:12,080 --> 07:35:15,878 available to us we don't need to do 9414 07:35:13,638 --> 07:35:17,958 anything and then if you don't work with 9415 07:35:15,878 --> 07:35:20,160 typescript essentially we have two 9416 07:35:17,958 --> 07:35:23,440 options we can either go with 9417 07:35:20,160 --> 07:35:24,280 event. current Target and notice how 9418 07:35:23,440 --> 07:35:27,760 with 9419 07:35:24,280 --> 07:35:30,440 typescript we can do the same typescript 9420 07:35:27,760 --> 07:35:33,558 is not going to complain however 9421 07:35:30,440 --> 07:35:36,280 if we're going to go with event. Target 9422 07:35:33,558 --> 07:35:38,200 which again we can do without typescript 9423 07:35:36,280 --> 07:35:40,318 actually it's not going to work if we 9424 07:35:38,200 --> 07:35:42,680 use typescript so let me just comment 9425 07:35:40,318 --> 07:35:45,798 this one out for your reference and 9426 07:35:42,680 --> 07:35:47,760 actually I'll use this one later on 9427 07:35:45,798 --> 07:35:49,440 anyway but if we go here with 9428 07:35:47,760 --> 07:35:52,160 event.target 9429 07:35:49,440 --> 07:35:55,200 it's not going to fly so typescript is 9430 07:35:52,160 --> 07:35:57,958 going to complain now way around that is 9431 07:35:55,200 --> 07:36:02,080 to basically go with HTML and then 9432 07:35:57,958 --> 07:36:04,600 provide form element so we just add this 9433 07:36:02,080 --> 07:36:07,398 type assertion where 9434 07:36:04,600 --> 07:36:09,160 yeah we're looking for event. Target but 9435 07:36:07,398 --> 07:36:12,360 we say that Hey listen this is going to 9436 07:36:09,160 --> 07:36:15,680 be actually a forum element now if we go 9437 07:36:12,360 --> 07:36:19,000 with current Target typescript is smart 9438 07:36:15,680 --> 07:36:22,520 and it can see that we already have this 9439 07:36:19,000 --> 07:36:24,080 element over here so let me like I said 9440 07:36:22,520 --> 07:36:26,240 come in this one out and actually use 9441 07:36:24,080 --> 07:36:28,520 the current Target and then one more 9442 07:36:26,240 --> 07:36:31,280 gotch about form 9443 07:36:28,520 --> 07:36:34,920 data potentially the values could be 9444 07:36:31,280 --> 07:36:36,520 null as well and we need to handle that 9445 07:36:34,920 --> 07:36:38,200 so how does that look like well for 9446 07:36:36,520 --> 07:36:40,240 stars let me just showcase how we can 9447 07:36:38,200 --> 07:36:43,878 nicely access all of the values so if we 9448 07:36:40,240 --> 07:36:46,638 go here with object dot then from 9449 07:36:43,878 --> 07:36:49,958 entries so now we just need to provide 9450 07:36:46,638 --> 07:36:53,240 deform data and we'll get access to all 9451 07:36:49,958 --> 07:36:56,080 of the values so if I go with data then 9452 07:36:53,240 --> 07:37:00,600 let me refresh and provide some values 9453 07:36:56,080 --> 07:37:03,280 so some name and 9454 07:37:00,600 --> 07:37:05,520 email at 9455 07:37:03,280 --> 07:37:07,600 email.com let's see where we get back 9456 07:37:05,520 --> 07:37:11,638 once we click on submit notice we'll 9457 07:37:07,600 --> 07:37:15,638 have this object and even if we have 15 9458 07:37:11,638 --> 07:37:18,520 inputs we'll have a object with 15 9459 07:37:15,638 --> 07:37:22,280 properties with all of the values over 9460 07:37:18,520 --> 07:37:25,318 here and as far as the gacha well let me 9461 07:37:22,280 --> 07:37:27,398 first create a type just so we can 9462 07:37:25,318 --> 07:37:31,040 clearly see that so let's say we have 9463 07:37:27,398 --> 07:37:33,440 over here type person is equal equal to 9464 07:37:31,040 --> 07:37:36,798 and then name and that will be equal to 9465 07:37:33,440 --> 07:37:40,600 string so pretty straightforward type 9466 07:37:36,798 --> 07:37:45,440 and then let's go here with const 9467 07:37:40,600 --> 07:37:47,638 person let's set it equal to our person 9468 07:37:45,440 --> 07:37:50,160 type and we're going to go here with 9469 07:37:47,638 --> 07:37:52,160 name and now let's get the value now 9470 07:37:50,160 --> 07:37:54,680 it's going to be the same for the object 9471 07:37:52,160 --> 07:37:56,440 over here for the text property inside 9472 07:37:54,680 --> 07:38:00,040 of this object but first I'll just 9473 07:37:56,440 --> 07:38:02,680 showcase what happens if we use the form 9474 07:38:00,040 --> 07:38:05,160 data. getet method which is available to 9475 07:38:02,680 --> 07:38:07,558 us so let's say I only want to get the 9476 07:38:05,160 --> 07:38:13,440 text value I can always go here with 9477 07:38:07,558 --> 07:38:15,478 text equals to form data then dot get 9478 07:38:13,440 --> 07:38:17,360 then we need to provide the name and the 9479 07:38:15,478 --> 07:38:19,478 names need to match so let's say in this 9480 07:38:17,360 --> 07:38:21,840 case I want to access the 9481 07:38:19,478 --> 07:38:24,120 text notice the value that I'm getting 9482 07:38:21,840 --> 07:38:26,878 back either it's going to be form data 9483 07:38:24,120 --> 07:38:29,120 entry value type or it's going to be 9484 07:38:26,878 --> 07:38:31,558 null now why do we care about that well 9485 07:38:29,120 --> 07:38:33,718 if I'm I'm going to set my name equal to 9486 07:38:31,558 --> 07:38:36,558 a text typescript is not going to be 9487 07:38:33,718 --> 07:38:39,120 happy about that it says Hey listen 9488 07:38:36,558 --> 07:38:40,360 potentially it can also be a null and 9489 07:38:39,120 --> 07:38:44,280 you cannot 9490 07:38:40,360 --> 07:38:47,240 assign this value to a string so the way 9491 07:38:44,280 --> 07:38:49,318 around that is to again use the type 9492 07:38:47,240 --> 07:38:51,958 assertion where essentially we go with 9493 07:38:49,318 --> 07:38:53,558 as and then we set it equal to string 9494 07:38:51,958 --> 07:38:56,280 and here again I'm telling typescript 9495 07:38:53,558 --> 07:38:59,440 that I know more about the project where 9496 07:38:56,280 --> 07:39:02,558 yeah maybe this can be known but in this 9497 07:38:59,440 --> 07:39:05,718 case I know for sure that value is going 9498 07:39:02,558 --> 07:39:08,240 to be correct because I know that these 9499 07:39:05,718 --> 07:39:09,638 two values match and now notice how 9500 07:39:08,240 --> 07:39:13,680 everything works and the same is going 9501 07:39:09,638 --> 07:39:17,360 to be over here let's say if we go with 9502 07:39:13,680 --> 07:39:19,760 data. text remember that was the 9503 07:39:17,360 --> 07:39:22,760 property name again it's not going to 9504 07:39:19,760 --> 07:39:25,718 fly so in that case we again need to go 9505 07:39:22,760 --> 07:39:28,318 with as and then string and now 9506 07:39:25,718 --> 07:39:33,120 typescript doesn't complain so just to 9507 07:39:28,318 --> 07:39:36,558 recap whenever we set up events in react 9508 07:39:33,120 --> 07:39:39,558 if we provide the inline 9509 07:39:36,558 --> 07:39:43,360 function everything is inferred as far 9510 07:39:39,558 --> 07:39:45,600 as the event type so there's really not 9511 07:39:43,360 --> 07:39:49,440 that much difference between typescript 9512 07:39:45,600 --> 07:39:51,120 or not typescript however if we set up 9513 07:39:49,440 --> 07:39:53,680 functions as the 9514 07:39:51,120 --> 07:39:56,520 references then we'll need to provide 9515 07:39:53,680 --> 07:39:59,478 the correct type for the 9516 07:39:56,520 --> 07:40:02,240 event and there are two things things 9517 07:39:59,478 --> 07:40:06,120 that we're looking for we first need to 9518 07:40:02,240 --> 07:40:07,520 grab the correct event type and also 9519 07:40:06,120 --> 07:40:11,600 we'll need to 9520 07:40:07,520 --> 07:40:13,920 provide the correct HTML element since 9521 07:40:11,600 --> 07:40:16,440 most of these events are actually 9522 07:40:13,920 --> 07:40:19,558 generic now if we want to get the quick 9523 07:40:16,440 --> 07:40:22,398 suggestion we simply can set up the prop 9524 07:40:19,558 --> 07:40:24,520 on the element and then provide the 9525 07:40:22,398 --> 07:40:27,080 inline function and the moment we do 9526 07:40:24,520 --> 07:40:30,478 that we'll get a nice suggestion with 9527 07:40:27,080 --> 07:40:33,760 the correct event type and the correct 9528 07:40:30,478 --> 07:40:36,360 HTML element okay and up next let's work 9529 07:40:33,760 --> 07:40:37,718 on a small Challenge and I think it's 9530 07:40:36,360 --> 07:40:40,360 going to be 9531 07:40:37,718 --> 07:40:44,280 easier for me to 9532 07:40:40,360 --> 07:40:47,160 showcase the requirements so 9533 07:40:44,280 --> 07:40:49,920 first let's set up the import but in 9534 07:40:47,160 --> 07:40:52,440 this case we're looking for the final 9535 07:40:49,920 --> 07:40:55,120 folder so all this time we have been 9536 07:40:52,440 --> 07:40:58,440 importing from the Star this is the case 9537 07:40:55,120 --> 07:41:00,958 where we will import from the final one 9538 07:40:58,440 --> 07:41:02,558 so let's go with Port let's come up with 9539 07:41:00,958 --> 07:41:05,240 a name in my case it's going to be 9540 07:41:02,558 --> 07:41:07,760 component from then we're going to go in 9541 07:41:05,240 --> 07:41:10,638 the final one more specifically 9542 07:41:07,760 --> 07:41:12,920 Challenge and then we'll set up two 9543 07:41:10,638 --> 07:41:15,160 instances so first we're going to go 9544 07:41:12,920 --> 07:41:17,478 with component we'll need to provide the 9545 07:41:15,160 --> 07:41:20,160 type as far as the values notice we have 9546 07:41:17,478 --> 07:41:22,558 two options we can either go with 9547 07:41:20,160 --> 07:41:24,318 Advanced or basic so let's start with 9548 07:41:22,558 --> 07:41:27,200 basic 9549 07:41:24,318 --> 07:41:29,558 then if we have basic we'll also need to 9550 07:41:27,200 --> 07:41:32,240 provide the name in my case I'm going to 9551 07:41:29,558 --> 07:41:34,398 go with Susan let's close this component 9552 07:41:32,240 --> 07:41:36,840 and let's set up another instance where 9553 07:41:34,398 --> 07:41:40,600 we're going to go with component then 9554 07:41:36,840 --> 07:41:43,040 type now if we go with Advanced now 9555 07:41:40,600 --> 07:41:45,520 we'll need to provide more values not 9556 07:41:43,040 --> 07:41:47,760 only we'll need to provide the name 9557 07:41:45,520 --> 07:41:50,360 which is going to be Anna also we'll 9558 07:41:47,760 --> 07:41:55,440 need to provide the email and let's go 9559 07:41:50,360 --> 07:41:57,878 with Anna gmail.com and if we take a 9560 07:41:55,440 --> 07:42:00,318 look at the browser you'll see something 9561 07:41:57,878 --> 07:42:04,040 like this so so this is going to be the 9562 07:42:00,318 --> 07:42:06,558 look for the basic one and this is going 9563 07:42:04,040 --> 07:42:09,398 to be the look for I believe the name 9564 07:42:06,558 --> 07:42:12,558 was Advanced so here's the challenge 9565 07:42:09,398 --> 07:42:16,478 create a component that has following 9566 07:42:12,558 --> 07:42:18,680 features if we provide type basic we 9567 07:42:16,478 --> 07:42:22,840 only look for 9568 07:42:18,680 --> 07:42:26,600 name however if we provide type Advanced 9569 07:42:22,840 --> 07:42:29,160 we also need to provide email so for 9570 07:42:26,600 --> 07:42:30,920 example if I'll try to set up ADV 9571 07:42:29,160 --> 07:42:34,120 Advanced without the 9572 07:42:30,920 --> 07:42:38,478 email typescript is going to complain 9573 07:42:34,120 --> 07:42:41,878 and also based on a type notice how we 9574 07:42:38,478 --> 07:42:44,360 have different looks so for the basic 9575 07:42:41,878 --> 07:42:46,798 one we have this one and then for the 9576 07:42:44,360 --> 07:42:50,360 advanced one we have a different look 9577 07:42:46,798 --> 07:42:54,798 now a tiny hint as far as the classes in 9578 07:42:50,360 --> 07:42:58,240 here in CSS you're looking for alert and 9579 07:42:54,798 --> 07:42:59,878 alert danger and success so this CSS 9580 07:42:58,240 --> 07:43:02,080 part is totally 9581 07:42:59,878 --> 07:43:04,878 irrelevant technically you can skip it 9582 07:43:02,080 --> 07:43:07,318 for example you can just add a red text 9583 07:43:04,878 --> 07:43:10,318 or something along those lines for one 9584 07:43:07,318 --> 07:43:13,318 of the classes the general idea is to 9585 07:43:10,318 --> 07:43:15,718 create a component where based on that 9586 07:43:13,318 --> 07:43:19,600 type we are 9587 07:43:15,718 --> 07:43:22,120 first providing different sets of props 9588 07:43:19,600 --> 07:43:25,958 and if we don't then we get the error 9589 07:43:22,120 --> 07:43:28,040 and also we get a different look so try 9590 07:43:25,958 --> 07:43:30,160 to work on this Challenge and in the 9591 07:43:28,040 --> 07:43:32,638 upcoming videos I'll show you my 9592 07:43:30,160 --> 07:43:35,600 solution okay and first let me show you 9593 07:43:32,638 --> 07:43:38,000 a typical react solution prior to 9594 07:43:35,600 --> 07:43:40,958 typescript we'll also take a look at the 9595 07:43:38,000 --> 07:43:44,240 potential issues essentially we won't be 9596 07:43:40,958 --> 07:43:46,240 able to complete the challenge and in 9597 07:43:44,240 --> 07:43:50,000 the following video I'll show you a 9598 07:43:46,240 --> 07:43:52,360 proper typescript solution so first let 9599 07:43:50,000 --> 07:43:55,638 me comment some things 9600 07:43:52,360 --> 07:43:58,000 out first one the import and then these 9601 07:43:55,638 --> 07:44:01,080 two so I still want these for your 9602 07:43:58,000 --> 07:44:03,600 reference because they will help me to 9603 07:44:01,080 --> 07:44:06,958 showcase the potential issues and now 9604 07:44:03,600 --> 07:44:10,318 let me grab the import component from 9605 07:44:06,958 --> 07:44:12,600 and now we're looking for the starter 9606 07:44:10,318 --> 07:44:14,718 and same folder so we're looking for the 9607 07:44:12,600 --> 07:44:17,160 challenge and we want to render that 9608 07:44:14,718 --> 07:44:18,398 component somewhere so let's render the 9609 07:44:17,160 --> 07:44:21,360 component let's take a look at the 9610 07:44:18,398 --> 07:44:22,478 browser so now we have react and 9611 07:44:21,360 --> 07:44:25,840 typescript 9612 07:44:22,478 --> 07:44:28,000 Challenge and initially our approach 9613 07:44:25,840 --> 07:44:31,160 could look something like this where we 9614 07:44:28,000 --> 07:44:33,760 know that we need this type prop we also 9615 07:44:31,160 --> 07:44:36,360 know that we need name and email and we 9616 07:44:33,760 --> 07:44:40,040 know that based on that type we will 9617 07:44:36,360 --> 07:44:41,958 display one set of classes and if it's 9618 07:44:40,040 --> 07:44:44,558 the opposite then we'll display a 9619 07:44:41,958 --> 07:44:47,280 different set so let's navigate first to 9620 07:44:44,558 --> 07:44:49,638 starter and then index I guess we can 9621 07:44:47,280 --> 07:44:52,080 right away set up the type over here or 9622 07:44:49,638 --> 07:44:56,680 interface that is totally up to you I'm 9623 07:44:52,080 --> 07:44:58,600 going to go with type profile card props 9624 07:44:56,680 --> 07:44:59,718 three properties so we start here with 9625 07:44:58,600 --> 07:45:01,718 type 9626 07:44:59,718 --> 07:45:04,160 just because I enjoy it so much I'm just 9627 07:45:01,718 --> 07:45:06,718 going to go with the literal type so I'm 9628 07:45:04,160 --> 07:45:11,040 going to say yeah the possible values 9629 07:45:06,718 --> 07:45:14,440 here are basic or they're going to be 9630 07:45:11,040 --> 07:45:18,240 Advanced then there's going to be a name 9631 07:45:14,440 --> 07:45:20,840 which is a string and also we'll set up 9632 07:45:18,240 --> 07:45:24,398 the email which also will be a string 9633 07:45:20,840 --> 07:45:26,440 cool cool cool and awesome start then in 9634 07:45:24,398 --> 07:45:29,440 the component I'll say yeah there's a 9635 07:45:26,440 --> 07:45:32,280 prop subject and actually it's going to 9636 07:45:29,440 --> 07:45:34,878 have this type so profile card props 9637 07:45:32,280 --> 07:45:38,398 okay cool then let's gather all of these 9638 07:45:34,878 --> 07:45:42,520 things so we're looking for type name 9639 07:45:38,398 --> 07:45:43,958 and email and that is equal to our props 9640 07:45:42,520 --> 07:45:46,200 typescript is complaining because we're 9641 07:45:43,958 --> 07:45:48,558 not using these elements and you know 9642 07:45:46,200 --> 07:45:51,558 what before we set up the return let me 9643 07:45:48,558 --> 07:45:54,318 just set up the logic for the class so 9644 07:45:51,558 --> 07:45:57,120 essentially if the type is basic I want 9645 07:45:54,318 --> 07:45:59,520 to use the success one and if not I'm 9646 07:45:57,120 --> 07:46:02,440 going to use the danger again just to go 9647 07:45:59,520 --> 07:46:04,920 back notice in the index we have this 9648 07:46:02,440 --> 07:46:08,280 class for alert so this is just going to 9649 07:46:04,920 --> 07:46:11,638 add the padding and or radius and all 9650 07:46:08,280 --> 07:46:14,878 that and then based on this class so 9651 07:46:11,638 --> 07:46:17,398 either alert danger or alert success 9652 07:46:14,878 --> 07:46:19,760 we'll just have the background color and 9653 07:46:17,398 --> 07:46:23,558 color and yes of course we can set it up 9654 07:46:19,760 --> 07:46:26,080 over here in the return but I'll do it 9655 07:46:23,558 --> 07:46:29,120 above since in my opinion it's going to 9656 07:46:26,080 --> 07:46:32,398 be easier to see what we're doing so 9657 07:46:29,120 --> 07:46:35,558 alert type is going to be equal to type 9658 07:46:32,398 --> 07:46:37,638 then either basic or Advanced that is 9659 07:46:35,558 --> 07:46:40,478 totally up to you and I'm going to go 9660 07:46:37,638 --> 07:46:43,478 with success since that's the class name 9661 07:46:40,478 --> 07:46:46,120 and yes they do need to match and then 9662 07:46:43,478 --> 07:46:48,958 based on this one I'll set up my class 9663 07:46:46,120 --> 07:46:52,600 name so I'm going to go with const class 9664 07:46:48,958 --> 07:46:56,360 name and we'll set it equal to alert so 9665 07:46:52,600 --> 07:47:00,360 that's my main class and then alert iph 9666 07:46:56,360 --> 07:47:03,040 and now let's access the alert type so 9667 07:47:00,360 --> 07:47:06,000 hopefully it's clear that if it's going 9668 07:47:03,040 --> 07:47:09,398 to be equal to Advanced essentially not 9669 07:47:06,000 --> 07:47:13,520 basic then the value is going to be 9670 07:47:09,398 --> 07:47:16,240 danger if going to be equal to basic 9671 07:47:13,520 --> 07:47:20,478 then essentially the value will be 9672 07:47:16,240 --> 07:47:22,520 success and as a result our card is 9673 07:47:20,478 --> 07:47:24,440 going to look different so in here we 9674 07:47:22,520 --> 07:47:27,558 want to go with 9675 07:47:24,440 --> 07:47:31,000 article and then let's add a class name 9676 07:47:27,558 --> 07:47:35,080 so let's access our variable over here 9677 07:47:31,000 --> 07:47:38,120 or constant I guess more properly and 9678 07:47:35,080 --> 07:47:40,840 then let's just access both of the 9679 07:47:38,120 --> 07:47:43,840 props so the first heading two is going 9680 07:47:40,840 --> 07:47:47,718 to be user so we're going to go user is 9681 07:47:43,840 --> 07:47:49,200 equal to our name prop now when it comes 9682 07:47:47,718 --> 07:47:52,160 to email it's going to be a little bit 9683 07:47:49,200 --> 07:47:55,680 different simply because we only want to 9684 07:47:52,160 --> 07:47:59,760 display the email if the type is equal 9685 07:47:55,680 --> 07:48:02,398 to Advanced correct so in my case I will 9686 07:47:59,760 --> 07:48:05,440 only display the email if it's 9687 07:48:02,398 --> 07:48:08,680 provided now we can set up turn operator 9688 07:48:05,440 --> 07:48:12,120 or we can use the good old hand operator 9689 07:48:08,680 --> 07:48:13,798 and I think I'll stick with that one 9690 07:48:12,120 --> 07:48:15,878 because in my opinion in this case it 9691 07:48:13,798 --> 07:48:18,318 doesn't really make a difference so 9692 07:48:15,878 --> 07:48:20,680 let's look for heading two then let's 9693 07:48:18,318 --> 07:48:25,280 set it equal to email and now we just 9694 07:48:20,680 --> 07:48:26,760 want to access the email prop okay good 9695 07:48:25,280 --> 07:48:29,318 we have everything in place now let's 9696 07:48:26,760 --> 07:48:32,718 navigate back and let's provide these 9697 07:48:29,318 --> 07:48:35,920 values and you'll notice something 9698 07:48:32,718 --> 07:48:40,240 interesting we will only be able 9699 07:48:35,920 --> 07:48:43,360 to display the advanced component let me 9700 07:48:40,240 --> 07:48:45,440 showcase why so let me go here with type 9701 07:48:43,360 --> 07:48:49,600 we're looking for 9702 07:48:45,440 --> 07:48:51,240 advanced then let's go with name again 9703 07:48:49,600 --> 07:48:53,318 doesn't really matter what we provide so 9704 07:48:51,240 --> 07:48:56,680 I'm going to go with Susan and let me 9705 07:48:53,318 --> 07:49:00,600 also provide the email over here let me 9706 07:48:56,680 --> 07:49:04,040 go with Susan at 9707 07:49:00,600 --> 07:49:07,000 susan. let me save it everything looks 9708 07:49:04,040 --> 07:49:11,920 correct but notice if I'm going to go 9709 07:49:07,000 --> 07:49:15,080 here with the basic and just omit the 9710 07:49:11,920 --> 07:49:18,280 email not going to be successful why 9711 07:49:15,080 --> 07:49:21,000 well let's take a look at our type 9712 07:49:18,280 --> 07:49:24,120 typescript is expecting this email one 9713 07:49:21,000 --> 07:49:26,478 so if I'll copy and paste over here and 9714 07:49:24,120 --> 07:49:29,440 if I'll say yeah the type is going to be 9715 07:49:26,478 --> 07:49:30,478 basic and actually I don't want to 9716 07:49:29,440 --> 07:49:33,318 provide the 9717 07:49:30,478 --> 07:49:35,478 email typescript is going to complain 9718 07:49:33,318 --> 07:49:37,958 it's going to say Hey listen I still 9719 07:49:35,478 --> 07:49:40,318 need this email and technically you can 9720 07:49:37,958 --> 07:49:43,040 make an argument okay well I could 9721 07:49:40,318 --> 07:49:47,360 provide email over here and just provide 9722 07:49:43,040 --> 07:49:49,680 empty string and even though it kind of 9723 07:49:47,360 --> 07:49:50,600 works you have to agree that this is 9724 07:49:49,680 --> 07:49:52,280 somewhat 9725 07:49:50,600 --> 07:49:57,080 clunky 9726 07:49:52,280 --> 07:49:59,760 my goal is to have a component where if 9727 07:49:57,080 --> 07:50:02,360 I provide this type Advanced I am 9728 07:49:59,760 --> 07:50:05,360 looking for email and if it's actually 9729 07:50:02,360 --> 07:50:07,558 type basic I'm not even looking for this 9730 07:50:05,360 --> 07:50:11,120 prop that's my 9731 07:50:07,558 --> 07:50:13,318 goal now you could make an argument that 9732 07:50:11,120 --> 07:50:15,920 we can go back and make this one 9733 07:50:13,318 --> 07:50:20,200 optional over here and yeah technically 9734 07:50:15,920 --> 07:50:23,080 it works for the basic one right now but 9735 07:50:20,200 --> 07:50:26,080 what about the advanced one so now this 9736 07:50:23,080 --> 07:50:28,878 technically is optional as well so if I 9737 07:50:26,080 --> 07:50:31,558 remove it typescript doesn't complain 9738 07:50:28,878 --> 07:50:36,840 but if I navigate back I'm not showing 9739 07:50:31,558 --> 07:50:39,080 the email in my Advanced user card as 9740 07:50:36,840 --> 07:50:41,600 you can see in both of the cases we have 9741 07:50:39,080 --> 07:50:45,600 issues we don't have the 9742 07:50:41,600 --> 07:50:47,920 setup that we're looking for and 9743 07:50:45,600 --> 07:50:51,558 therefore in the following video we will 9744 07:50:47,920 --> 07:50:52,958 cover a proper typescript solution and 9745 07:50:51,558 --> 07:50:55,080 since I know that I'm going to get this 9746 07:50:52,958 --> 07:50:59,520 question a lot let me just answer it 9747 07:50:55,080 --> 07:51:02,718 here no adding following logic to our 9748 07:50:59,520 --> 07:51:05,558 component is not going to solve the 9749 07:51:02,718 --> 07:51:07,920 issue so yes 9750 07:51:05,558 --> 07:51:10,360 definitely not only we can check for 9751 07:51:07,920 --> 07:51:13,398 email we can also check for type and set 9752 07:51:10,360 --> 07:51:16,318 up the tary operator where if the type 9753 07:51:13,398 --> 07:51:18,200 is equal to advaned then we display the 9754 07:51:16,318 --> 07:51:23,680 heading to with an email and if not we 9755 07:51:18,200 --> 07:51:26,160 go with null but notice our component so 9756 07:51:23,680 --> 07:51:29,440 for example if I have the advanced one 9757 07:51:26,160 --> 07:51:32,478 yes it nicely requires the email and 9758 07:51:29,440 --> 07:51:34,878 once I provide everything is 9759 07:51:32,478 --> 07:51:38,638 awesome but it's going to be the same 9760 07:51:34,878 --> 07:51:41,760 thing with our basic one so since the 9761 07:51:38,638 --> 07:51:45,040 prop is present I need to provide it and 9762 07:51:41,760 --> 07:51:48,040 yes I can still provide the empty string 9763 07:51:45,040 --> 07:51:50,040 but remember that's not my solution 9764 07:51:48,040 --> 07:51:52,558 that's not what I'm looking for I don't 9765 07:51:50,040 --> 07:51:55,558 want to randomly provide these empty 9766 07:51:52,558 --> 07:51:59,840 strings and if we change this one around 9767 07:51:55,558 --> 07:52:04,360 to optional property same deal 9768 07:51:59,840 --> 07:52:07,558 now if I go to app DSX and remove both 9769 07:52:04,360 --> 07:52:10,120 of these values over here notice 9770 07:52:07,558 --> 07:52:13,718 essentially the first card is correct 9771 07:52:10,120 --> 07:52:16,080 but the second one has empty email value 9772 07:52:13,718 --> 07:52:18,680 so yes even though this one looks like a 9773 07:52:16,080 --> 07:52:19,840 better solution essentially it has the 9774 07:52:18,680 --> 07:52:22,558 same 9775 07:52:19,840 --> 07:52:25,398 issues as the one that we used before 9776 07:52:22,558 --> 07:52:27,200 where we checked for the email all right 9777 07:52:25,398 --> 07:52:29,398 and once we have covered the issues with 9778 07:52:27,200 --> 07:52:32,120 our initial setup 9779 07:52:29,398 --> 07:52:35,600 now let's complete the challenge 9780 07:52:32,120 --> 07:52:39,200 by constructing a proper typescript 9781 07:52:35,600 --> 07:52:41,240 solution for starters I do want to 9782 07:52:39,200 --> 07:52:44,280 remove these ones so I want to start 9783 07:52:41,240 --> 07:52:48,878 from the scratch and essentially in here 9784 07:52:44,280 --> 07:52:50,520 we want to create two types so our 9785 07:52:48,878 --> 07:52:52,600 component actually is going to have two 9786 07:52:50,520 --> 07:52:54,680 types so the first one is going to be 9787 07:52:52,600 --> 07:52:57,638 basic one and second one will be 9788 07:52:54,680 --> 07:53:01,000 Advanced so we're not going to use this 9789 07:52:57,638 --> 07:53:03,958 literal type over here we're actually 9790 07:53:01,000 --> 07:53:05,920 going to hardcode it equal to a basic 9791 07:53:03,958 --> 07:53:08,760 and since there's going to be two types 9792 07:53:05,920 --> 07:53:12,920 I'll also name this one basic profile 9793 07:53:08,760 --> 07:53:15,000 card props now let me copy this one and 9794 07:53:12,920 --> 07:53:17,638 in here we want to remove the 9795 07:53:15,000 --> 07:53:21,160 email and it's not going to be optional 9796 07:53:17,638 --> 07:53:25,878 anymore so if the type is Advanced if 9797 07:53:21,160 --> 07:53:28,280 we'll have this type then we'll look for 9798 07:53:25,878 --> 07:53:30,440 all of these properties so type is going 9799 07:53:28,280 --> 07:53:33,240 to be equal to Advanced name String and 9800 07:53:30,440 --> 07:53:37,440 email string now for the basic one we'll 9801 07:53:33,240 --> 07:53:40,160 only have type basic and name type is 9802 07:53:37,440 --> 07:53:42,360 going to be a string now where we have 9803 07:53:40,160 --> 07:53:46,440 the component I'll actually create 9804 07:53:42,360 --> 07:53:50,520 another type I'll say profile card props 9805 07:53:46,440 --> 07:53:53,600 and this will be a union type with basic 9806 07:53:50,520 --> 07:53:54,840 profile card props and the advanced 9807 07:53:53,600 --> 07:53:58,000 profile card 9808 07:53:54,840 --> 07:54:02,120 props and in here we're actually going 9809 07:53:58,000 --> 07:54:06,000 to set up to return based on a type so 9810 07:54:02,120 --> 07:54:08,958 for starters what two properties we can 9811 07:54:06,000 --> 07:54:11,160 always access well it's the type as well 9812 07:54:08,958 --> 07:54:14,040 as the name correct because keep in mind 9813 07:54:11,160 --> 07:54:18,398 we only have access to email if the type 9814 07:54:14,040 --> 07:54:21,478 is equal to Advanced profile card props 9815 07:54:18,398 --> 07:54:23,318 so therefore we're going to look for two 9816 07:54:21,478 --> 07:54:27,040 properties that are always going to be 9817 07:54:23,318 --> 07:54:29,760 there so name and type and now 9818 07:54:27,040 --> 07:54:31,798 essentially we want to set up our return 9819 07:54:29,760 --> 07:54:35,760 where we're going to go with well if 9820 07:54:31,798 --> 07:54:40,080 type is equal to basic one of the 9821 07:54:35,760 --> 07:54:43,520 potential values then we'll have one set 9822 07:54:40,080 --> 07:54:47,000 of elements if not well then we'll 9823 07:54:43,520 --> 07:54:49,200 return prey much the other card so let's 9824 07:54:47,000 --> 07:54:51,718 go here with the return we're looking 9825 07:54:49,200 --> 07:54:56,520 for the article so in this case we don't 9826 07:54:51,718 --> 07:54:58,558 need to set up that logic to add classes 9827 07:54:56,520 --> 07:55:03,280 based on type we simply we want to go 9828 07:54:58,558 --> 07:55:03,280 with class name alert alert 9829 07:55:03,878 --> 07:55:09,040 success and then inside of the article 9830 07:55:06,718 --> 07:55:11,798 we're just going to go with heading two 9831 07:55:09,040 --> 07:55:15,240 user and we'll access the 9832 07:55:11,798 --> 07:55:18,280 name so let's set it up over here and 9833 07:55:15,240 --> 07:55:21,760 then if the type is 9834 07:55:18,280 --> 07:55:25,040 Advanced basically our second option 9835 07:55:21,760 --> 07:55:26,798 you'll see that we have access to props 9836 07:55:25,040 --> 07:55:30,558 that email so let's go here with the 9837 07:55:26,798 --> 07:55:33,080 return and as a sign you can actually 9838 07:55:30,558 --> 07:55:34,958 structure it here as well so in here you 9839 07:55:33,080 --> 07:55:37,558 can go with email and actually 9840 07:55:34,958 --> 07:55:40,600 typescript is not going to complain and 9841 07:55:37,558 --> 07:55:43,318 it sees that email is actually a string 9842 07:55:40,600 --> 07:55:45,398 simply because if the type is basic okay 9843 07:55:43,318 --> 07:55:49,478 we have one return so it knows that at 9844 07:55:45,398 --> 07:55:51,680 this point the email has to be present 9845 07:55:49,478 --> 07:55:54,360 now in my case I will use the props that 9846 07:55:51,680 --> 07:55:56,798 email again doesn't really matter which 9847 07:55:54,360 --> 07:56:00,558 way you pick so we want to go over here 9848 07:55:56,798 --> 07:56:01,878 with article then let's add the classes 9849 07:56:00,558 --> 07:56:04,558 now we just want to provide the 9850 07:56:01,878 --> 07:56:07,398 different values so it's not going to be 9851 07:56:04,558 --> 07:56:09,680 success it's actually going to be a 9852 07:56:07,398 --> 07:56:11,240 danger one and then inside of the 9853 07:56:09,680 --> 07:56:15,000 article we're going to go with heading 9854 07:56:11,240 --> 07:56:20,240 two we're looking for user then we want 9855 07:56:15,000 --> 07:56:22,878 to access the name just like before and 9856 07:56:20,240 --> 07:56:27,520 we'll also add the heading two with 9857 07:56:22,878 --> 07:56:31,520 email and then we'll grab the props that 9858 07:56:27,520 --> 07:56:34,318 email so now let's navigate to app and 9859 07:56:31,520 --> 07:56:37,240 if everything is correct if we type 9860 07:56:34,318 --> 07:56:40,558 basic we only need to provide the name 9861 07:56:37,240 --> 07:56:42,280 and we'll get the correct look however 9862 07:56:40,558 --> 07:56:44,760 if we go with Advanced we'll need to 9863 07:56:42,280 --> 07:56:48,760 provide both of those values so if we go 9864 07:56:44,760 --> 07:56:52,318 with component type notice we have basic 9865 07:56:48,760 --> 07:56:55,600 or advanc so those are our two options 9866 07:56:52,318 --> 07:56:58,558 if we go with basic we only need to 9867 07:56:55,600 --> 07:57:01,920 provide the name so now we have property 9868 07:56:58,558 --> 07:57:04,280 name is missing okay good it's provided 9869 07:57:01,920 --> 07:57:07,040 and notice how we don't need to provide 9870 07:57:04,280 --> 07:57:09,478 an email here with an empty value no 9871 07:57:07,040 --> 07:57:13,798 that script is Happy everything's fine 9872 07:57:09,478 --> 07:57:15,878 we have correctly a basic user card now 9873 07:57:13,798 --> 07:57:19,080 if we're going to go here with component 9874 07:57:15,878 --> 07:57:22,440 type and we'll set up the advanced now 9875 07:57:19,080 --> 07:57:25,080 it's looking for name and email so one 9876 07:57:22,440 --> 07:57:29,398 by one let's set it up we have name 9877 07:57:25,080 --> 07:57:32,798 again let's go with our Anna and email 9878 07:57:29,398 --> 07:57:35,360 and let's just provide the value Yep 9879 07:57:32,798 --> 07:57:37,760 looks about right hopefully everyone 9880 07:57:35,360 --> 07:57:40,160 enjoyed the challenge and we can move on 9881 07:57:37,760 --> 07:57:42,000 to the next topic all right and up next 9882 07:57:40,160 --> 07:57:45,160 let's see how we can use typescript with 9883 07:57:42,000 --> 07:57:47,920 context API before we begin please keep 9884 07:57:45,160 --> 07:57:50,798 in mind that we will write bunch of the 9885 07:57:47,920 --> 07:57:54,160 boiler plate code meaning don't get 9886 07:57:50,798 --> 07:57:56,360 overwhelmed by the amount of code since 9887 07:57:54,160 --> 07:57:59,520 you'll be able to take it from Project 9888 07:57:56,360 --> 07:58:02,558 to project and second at this point in 9889 07:57:59,520 --> 07:58:06,000 time context API is not as popular 9890 07:58:02,558 --> 07:58:09,318 anymore so if you don't find these 9891 07:58:06,000 --> 07:58:12,200 examples useful don't stress about it in 9892 07:58:09,318 --> 07:58:15,360 fact if you don't use the context API at 9893 07:58:12,200 --> 07:58:18,558 all in your apps feel free to skip the 9894 07:58:15,360 --> 07:58:22,240 following videos for context API I did 9895 07:58:18,558 --> 07:58:25,080 prepare two examples the very basic one 9896 07:58:22,240 --> 07:58:28,160 with just one state value and a more 9897 07:58:25,080 --> 07:58:30,840 complex one with more bells and with 9898 07:58:28,160 --> 07:58:33,360 just so we can cover all the topics step 9899 07:58:30,840 --> 07:58:36,080 by step so let's start just like with 9900 07:58:33,360 --> 07:58:38,440 the previous examples where we want to 9901 07:58:36,080 --> 07:58:41,040 import from the star in this case we're 9902 07:58:38,440 --> 07:58:43,120 looking for the context one and in this 9903 07:58:41,040 --> 07:58:45,638 case we'll create two files so one is 9904 07:58:43,120 --> 07:58:48,398 going to be for context and essentially 9905 07:58:45,638 --> 07:58:52,160 in the index one we'll access the 9906 07:58:48,398 --> 07:58:54,160 context now if you work with context API 9907 07:58:52,160 --> 07:58:57,040 before you know that technically we can 9908 07:58:54,160 --> 07:59:00,718 wrap the app component in context we can 9909 07:58:57,040 --> 07:59:04,360 also set it up in the main 9910 07:59:00,718 --> 07:59:06,958 DSX so there's tons of options but in 9911 07:59:04,360 --> 07:59:10,240 this case I just decided to set up 9912 07:59:06,958 --> 07:59:13,840 everything in the context just so it's 9913 07:59:10,240 --> 07:59:16,520 easier add it to the final one and leave 9914 07:59:13,840 --> 07:59:20,600 it for your reference so don't be 9915 07:59:16,520 --> 07:59:24,718 surprised if in this example we'll have 9916 07:59:20,600 --> 07:59:28,040 two files so for stars we're looking for 9917 07:59:24,718 --> 07:59:31,318 component so this is coming from where 9918 07:59:28,040 --> 07:59:33,760 we're going to final and then we want to 9919 07:59:31,318 --> 07:59:36,440 look for context again let's render it 9920 07:59:33,760 --> 07:59:41,680 somewhere here on the screen the 9921 07:59:36,440 --> 07:59:45,558 component one and let's navigate to our 9922 07:59:41,680 --> 07:59:49,040 06 context one let's create a new one 9923 07:59:45,558 --> 07:59:52,440 and the name is going to be context DSX 9924 07:59:49,040 --> 07:59:54,718 in here we need two Imports and you know 9925 07:59:52,440 --> 07:59:56,878 what actually I'll set up the third one 9926 07:59:54,718 --> 07:59:59,200 as well since we'll use it in the 9927 07:59:56,878 --> 08:00:03,120 following example so we're looking for 9928 07:59:59,200 --> 08:00:05,600 two functions create context one and 9929 08:00:03,120 --> 08:00:08,760 also use context since we'll set up the 9930 08:00:05,600 --> 08:00:12,318 hook as well and then lastly in the 9931 08:00:08,760 --> 08:00:14,840 following example we'll use the use 9932 08:00:12,318 --> 08:00:17,398 state so I'll add that import here as 9933 08:00:14,840 --> 08:00:19,798 well right away and for now let's just 9934 08:00:17,398 --> 08:00:22,240 create a new context now as far as the 9935 08:00:19,798 --> 08:00:25,200 naming you'll notice that I keep using 9936 08:00:22,240 --> 08:00:28,558 this theme and it's simply because the 9937 08:00:25,200 --> 08:00:30,958 following example is going to be for the 9938 08:00:28,558 --> 08:00:34,120 theme and I prettyy much want to use the 9939 08:00:30,958 --> 08:00:37,120 same names so we'll just build on this 9940 08:00:34,120 --> 08:00:39,718 example that way we don't need to type 9941 08:00:37,120 --> 08:00:43,080 too much so theme 9942 08:00:39,718 --> 08:00:44,840 provider and let's call this context 9943 08:00:43,080 --> 08:00:47,718 let's invoke create context now this is 9944 08:00:44,840 --> 08:00:51,040 going to be generic and the difference 9945 08:00:47,718 --> 08:00:53,120 between normal react and react with 9946 08:00:51,040 --> 08:00:55,520 typescript is that we'll have to provide 9947 08:00:53,120 --> 08:00:57,280 the default value so in this case I'm 9948 08:00:55,520 --> 08:00:59,638 just going to go with undefined let's 9949 08:00:57,280 --> 08:01:01,920 imagine that we don't know what the 9950 08:00:59,638 --> 08:01:04,160 context is going to be then we want to 9951 08:01:01,920 --> 08:01:05,000 set up a component which is actually 9952 08:01:04,160 --> 08:01:08,878 going 9953 08:01:05,000 --> 08:01:11,318 to wrap all of our components and 9954 08:01:08,878 --> 08:01:12,878 provide the context so we're going to go 9955 08:01:11,318 --> 08:01:15,958 here with 9956 08:01:12,878 --> 08:01:18,680 theme provider and if you're familiar 9957 08:01:15,958 --> 08:01:21,000 with the context API you know that this 9958 08:01:18,680 --> 08:01:24,360 component normally is looking for 9959 08:01:21,000 --> 08:01:27,878 children so it's looking for the 9960 08:01:24,360 --> 08:01:29,840 component since it's a context provider 9961 08:01:27,878 --> 08:01:32,958 so in here we'll definitely need 9962 08:01:29,840 --> 08:01:36,318 children and we know already that as far 9963 08:01:32,958 --> 08:01:39,600 as the type we can either use the type 9964 08:01:36,318 --> 08:01:43,680 provided by react or we can just go with 9965 08:01:39,600 --> 08:01:46,558 react and then do and react node 9966 08:01:43,680 --> 08:01:48,680 somewhere here now as far as the logic 9967 08:01:46,558 --> 08:01:51,080 here's where we want to go with return 9968 08:01:48,680 --> 08:01:54,638 and we'll just set up one value so we're 9969 08:01:51,080 --> 08:01:58,840 looking for theme provider context it 9970 08:01:54,638 --> 08:02:00,958 has the provider component so we go here 9971 08:01:58,840 --> 08:02:04,600 with the provider and then we want to 9972 08:02:00,958 --> 08:02:07,040 wrap the children we'll go here with the 9973 08:02:04,600 --> 08:02:08,878 children and notice how typescript is 9974 08:02:07,040 --> 08:02:12,040 complaining because we do need to 9975 08:02:08,878 --> 08:02:15,160 provide the value now once we provide a 9976 08:02:12,040 --> 08:02:17,520 value you'll notice that typescript is 9977 08:02:15,160 --> 08:02:19,760 not going to be happy with create 9978 08:02:17,520 --> 08:02:22,200 context setup so for now let me just 9979 08:02:19,760 --> 08:02:25,360 showcase the basic example let's say I 9980 08:02:22,200 --> 08:02:27,120 provide over here the hello and the 9981 08:02:25,360 --> 08:02:30,080 moment we do that notice we right away 9982 08:02:27,120 --> 08:02:32,600 get the red squiggly line and the error 9983 08:02:30,080 --> 08:02:36,080 message says well you cannot assign 9984 08:02:32,600 --> 08:02:37,558 string to undefined so this was okay 9985 08:02:36,080 --> 08:02:39,160 initially when we were creating the 9986 08:02:37,558 --> 08:02:41,840 context we could say all right so it's 9987 08:02:39,160 --> 08:02:45,160 going to be undefined but now typescript 9988 08:02:41,840 --> 08:02:48,558 is complaining and in order to fix it 9989 08:02:45,160 --> 08:02:51,440 create context is a generic and in here 9990 08:02:48,558 --> 08:02:54,160 we'll use the type Union first we'll 9991 08:02:51,440 --> 08:02:57,478 provide what is the eventual value which 9992 08:02:54,160 --> 08:02:59,398 will be a string here and we'll also go 9993 08:02:57,478 --> 08:03:02,000 over here and say that it can be 9994 08:02:59,398 --> 08:03:05,398 undefined so this will be our initial 9995 08:03:02,000 --> 08:03:07,240 value and then this will be our eventual 9996 08:03:05,398 --> 08:03:10,360 value So eventually we'll have this 9997 08:03:07,240 --> 08:03:12,840 value equal to string and that's why we 9998 08:03:10,360 --> 08:03:15,318 have over here this type I'm purposely 9999 08:03:12,840 --> 08:03:17,520 keeping still this undefined because 10000 08:03:15,318 --> 08:03:20,718 it's going to be super useful once we 10001 08:03:17,520 --> 08:03:23,878 set up our custom hook so even before we 10002 08:03:20,718 --> 08:03:28,360 wrap any of our components I also want 10003 08:03:23,878 --> 08:03:30,840 to set up a hook called use theme again 10004 08:03:28,360 --> 08:03:32,600 I'm just going to keep these names since 10005 08:03:30,840 --> 08:03:34,798 we will use them in our following 10006 08:03:32,600 --> 08:03:37,160 example as well and in here we want to 10007 08:03:34,798 --> 08:03:40,318 invoke use context which is looking for 10008 08:03:37,160 --> 08:03:43,478 the theme provider context essentially 10009 08:03:40,318 --> 08:03:47,000 any context so let's set up this one 10010 08:03:43,478 --> 08:03:49,478 context is equal to use context and 10011 08:03:47,000 --> 08:03:52,120 we're going to go with theme provider 10012 08:03:49,478 --> 08:03:54,000 context and now if the context is 10013 08:03:52,120 --> 08:03:57,760 undefined I actually want to throw the 10014 08:03:54,000 --> 08:03:59,680 error now when the context is going to 10015 08:03:57,760 --> 08:04:01,958 to be undefined because at the moment we 10016 08:03:59,680 --> 08:04:04,958 can clearly see that actually we do have 10017 08:04:01,958 --> 08:04:07,520 a string well this context could be 10018 08:04:04,958 --> 08:04:11,718 undefined if let's say we're trying to 10019 08:04:07,520 --> 08:04:13,920 access this use theme but the component 10020 08:04:11,718 --> 08:04:16,760 where we're using the hook is not 10021 08:04:13,920 --> 08:04:18,520 wrapped in the theme provider context 10022 08:04:16,760 --> 08:04:21,478 and in my opinion it's a good setup 10023 08:04:18,520 --> 08:04:23,200 because that way we immediately can see 10024 08:04:21,478 --> 08:04:24,718 that there is an error and you'll see 10025 08:04:23,200 --> 08:04:27,000 what I'm talking about in a second if 10026 08:04:24,718 --> 08:04:29,680 this is confusing so I'm going to go 10027 08:04:27,000 --> 08:04:32,760 here with context and if it's actually 10028 08:04:29,680 --> 08:04:34,878 equal to Define I'm going to throw the 10029 08:04:32,760 --> 08:04:37,520 eror and if everything is correct then 10030 08:04:34,878 --> 08:04:40,718 we'll return the context so say here 10031 08:04:37,520 --> 08:04:44,040 return context but before that we'll go 10032 08:04:40,718 --> 08:04:46,398 throw new and error and let's just 10033 08:04:44,040 --> 08:04:49,000 provide some kind of error message my 10034 08:04:46,398 --> 08:04:52,240 case I'm going to go with use theme must 10035 08:04:49,000 --> 08:04:55,558 be used 10036 08:04:52,240 --> 08:04:58,600 within the theme provider let me save it 10037 08:04:55,558 --> 08:05:03,120 over here and now let's navigate to our 10038 08:04:58,600 --> 08:05:06,200 index one let's import both things the 10039 08:05:03,120 --> 08:05:08,840 hook as well as the theme provider and 10040 08:05:06,200 --> 08:05:10,798 now let's set up the parent component so 10041 08:05:08,840 --> 08:05:12,798 we're not going to export this one as 10042 08:05:10,798 --> 08:05:15,558 default actually there's going to be a 10043 08:05:12,798 --> 08:05:18,000 parent component where we'll have our 10044 08:05:15,558 --> 08:05:21,280 theme provider so first let's start here 10045 08:05:18,000 --> 08:05:23,360 with use theme okay yeah that's coming 10046 08:05:21,280 --> 08:05:26,840 from the context and I also want to grab 10047 08:05:23,360 --> 08:05:29,478 the theme provider and then let's set up 10048 08:05:26,840 --> 08:05:31,200 the parent comp component again this is 10049 08:05:29,478 --> 08:05:32,920 just to demonstrate how we can wrap 10050 08:05:31,200 --> 08:05:36,240 components please keep in mind that 10051 08:05:32,920 --> 08:05:40,478 typically you'll do that in the app or 10052 08:05:36,240 --> 08:05:43,478 in the main DSX so we go here with 10053 08:05:40,478 --> 08:05:43,478 parent 10054 08:05:43,920 --> 08:05:50,040 component that's our function basically 10055 08:05:47,160 --> 08:05:52,718 our component and as far as default one 10056 08:05:50,040 --> 08:05:55,840 I actually want to export the parent 10057 08:05:52,718 --> 08:05:58,920 component not the component and then in 10058 08:05:55,840 --> 08:06:02,160 here let's go with theme 10059 08:05:58,920 --> 08:06:04,920 provider and then inside of it we want 10060 08:06:02,160 --> 08:06:07,760 to place the component essentially this 10061 08:06:04,920 --> 08:06:10,680 one and once we nicely wrap the 10062 08:06:07,760 --> 08:06:13,958 component we can actually go over here 10063 08:06:10,680 --> 08:06:17,000 and I can go with context go with use 10064 08:06:13,958 --> 08:06:19,440 theme so that's my hook this is how I 10065 08:06:17,000 --> 08:06:23,878 can access the values and basically if 10066 08:06:19,440 --> 08:06:26,318 we log over here and provide the context 10067 08:06:23,878 --> 08:06:29,440 if everything is correct we should see 10068 08:06:26,318 --> 08:06:33,878 that value hello in the console so now 10069 08:06:29,440 --> 08:06:35,240 let's navigate to the browser and H this 10070 08:06:33,878 --> 08:06:38,360 looks 10071 08:06:35,240 --> 08:06:42,520 interesting so it looks like I'm 10072 08:06:38,360 --> 08:06:44,440 grabbing the file from the final one so 10073 08:06:42,520 --> 08:06:48,000 let me navigate back yeah we're not 10074 08:06:44,440 --> 08:06:50,478 looking for final my bad so we're 10075 08:06:48,000 --> 08:06:53,558 looking for the starter in this case and 10076 08:06:50,478 --> 08:06:56,280 once we do that notice the value is 10077 08:06:53,558 --> 08:06:59,440 hello and essentially it matches 10078 08:06:56,280 --> 08:07:02,638 whatever we pass over over here in the 10079 08:06:59,440 --> 08:07:05,638 value prop now of course normally this 10080 08:07:02,638 --> 08:07:10,080 is going to be more complex for example 10081 08:07:05,638 --> 08:07:12,760 you could have here an object and name 10082 08:07:10,080 --> 08:07:15,360 is going to be equal to something let's 10083 08:07:12,760 --> 08:07:17,840 go with hello same deal now I'll just 10084 08:07:15,360 --> 08:07:20,478 need to go back over here where we 10085 08:07:17,840 --> 08:07:22,840 create the context and just provide that 10086 08:07:20,478 --> 08:07:25,638 eventual value so again this is 10087 08:07:22,840 --> 08:07:27,080 undefined this is the default one and in 10088 08:07:25,638 --> 08:07:29,878 here I can say that yeah there's going 10089 08:07:27,080 --> 08:07:32,040 to be an object and the property is 10090 08:07:29,878 --> 08:07:35,558 actually going to be string so notice 10091 08:07:32,040 --> 08:07:38,520 now if we navigate to the console we'll 10092 08:07:35,558 --> 08:07:42,240 see this value instead and the reason 10093 08:07:38,520 --> 08:07:44,398 why this line of code is so useful is 10094 08:07:42,240 --> 08:07:49,398 because if we navigate back and for 10095 08:07:44,398 --> 08:07:51,958 example we forget to wrap everything in 10096 08:07:49,398 --> 08:07:55,760 our theme provider basically let's say 10097 08:07:51,958 --> 08:07:58,200 that we are trying to access our theme 10098 08:07:55,760 --> 08:08:00,398 context without wrapping the theme 10099 08:07:58,200 --> 08:08:03,920 provider and I'm just going to simulate 10100 08:08:00,398 --> 08:08:06,558 that by returning a component notice how 10101 08:08:03,920 --> 08:08:08,958 immediately I'm going to have the error 10102 08:08:06,558 --> 08:08:11,520 in a console and not only that it's 10103 08:08:08,958 --> 08:08:13,718 going to be super helpful where it's 10104 08:08:11,520 --> 08:08:16,240 going to say hey us te must be used 10105 08:08:13,718 --> 08:08:19,080 within the theme provider so this is 10106 08:08:16,240 --> 08:08:21,718 going to give me a super good clue what 10107 08:08:19,080 --> 08:08:23,718 is wrong with my current code and of 10108 08:08:21,718 --> 08:08:25,798 course now I just need to go back and 10109 08:08:23,718 --> 08:08:28,160 wrap everything nicely in the theme 10110 08:08:25,798 --> 08:08:30,558 provider so let me remove move this one 10111 08:08:28,160 --> 08:08:33,638 and now again we're good to go the main 10112 08:08:30,558 --> 08:08:37,280 deal over here when we use tapescript we 10113 08:08:33,638 --> 08:08:41,240 will need to provide what is the 10114 08:08:37,280 --> 08:08:43,600 eventual type for our value prop over 10115 08:08:41,240 --> 08:08:46,160 here and we can simply say that yeah 10116 08:08:43,600 --> 08:08:47,280 initially it's going to be undefined and 10117 08:08:46,160 --> 08:08:50,280 then 10118 08:08:47,280 --> 08:08:53,080 eventually whatever we set up in a value 10119 08:08:50,280 --> 08:08:55,600 we'll need to provide the type over here 10120 08:08:53,080 --> 08:08:57,600 and since the theme provider is always 10121 08:08:55,600 --> 08:09:01,000 going to be looking for children so is 10122 08:08:57,600 --> 08:09:03,958 going to be looking for components 10123 08:09:01,000 --> 08:09:07,000 therefore we'll need to provide a type 10124 08:09:03,958 --> 08:09:08,718 for a children prop as well all right 10125 08:09:07,000 --> 08:09:11,120 and now let's work on more complex 10126 08:09:08,718 --> 08:09:12,958 example please keep in mind that this 10127 08:09:11,120 --> 08:09:15,280 code is going to be available in the 10128 08:09:12,958 --> 08:09:18,958 readme just in case you ever want to 10129 08:09:15,280 --> 08:09:22,120 reference and also in the final one 10130 08:09:18,958 --> 08:09:23,558 you'll find this basic context and basic 10131 08:09:22,120 --> 08:09:26,520 index 10132 08:09:23,558 --> 08:09:29,200 TSX and essentially this is the code 10133 08:09:26,520 --> 08:09:32,760 that we set up in the previous video 10134 08:09:29,200 --> 08:09:35,160 because in my case I will modify my 10135 08:09:32,760 --> 08:09:37,798 existing code right now so for starters 10136 08:09:35,160 --> 08:09:40,360 I'm going to go here with type and theme 10137 08:09:37,798 --> 08:09:42,878 and I'm going to go with literal types 10138 08:09:40,360 --> 08:09:46,520 so we'll provide 10139 08:09:42,878 --> 08:09:48,638 light uh we'll also provide dark and 10140 08:09:46,520 --> 08:09:51,638 we're going to go with system and 10141 08:09:48,638 --> 08:09:53,718 essentially we're building here a theme 10142 08:09:51,638 --> 08:09:55,718 context provider so let's say in our 10143 08:09:53,718 --> 08:09:58,200 application we want to toggle between 10144 08:09:55,718 --> 08:10:00,360 the light and a dark theme and we want 10145 08:09:58,200 --> 08:10:03,478 to do it globally therefore we're going 10146 08:10:00,360 --> 08:10:06,080 to set up the context provider now let 10147 08:10:03,478 --> 08:10:09,600 me navigate to theme provider and 10148 08:10:06,080 --> 08:10:12,798 essentially I'll set up over here a use 10149 08:10:09,600 --> 08:10:14,680 state so I'm going to go with use State 10150 08:10:12,798 --> 08:10:15,558 and we know that it's a generic and I'll 10151 08:10:14,680 --> 08:10:18,240 say 10152 08:10:15,558 --> 08:10:20,798 yeah the value is going to be string but 10153 08:10:18,240 --> 08:10:22,520 actually it's going to be type of theme 10154 08:10:20,798 --> 08:10:24,840 and eventually we will provide the 10155 08:10:22,520 --> 08:10:28,440 default value let's not worry about it 10156 08:10:24,840 --> 08:10:32,280 right now and let's just go with 10157 08:10:28,440 --> 08:10:34,558 theme and as I not I do need to provide 10158 08:10:32,280 --> 08:10:38,878 the square brackets and we're looking 10159 08:10:34,558 --> 08:10:41,798 for theme and also a set 10160 08:10:38,878 --> 08:10:44,318 theme so we have these values over here 10161 08:10:41,798 --> 08:10:47,120 and as far as what I want to pass down 10162 08:10:44,318 --> 08:10:52,240 instead of this name hello I'll actually 10163 08:10:47,120 --> 08:10:53,798 go with theme and also a set theme so 10164 08:10:52,240 --> 08:10:56,360 now of course typescript is going to 10165 08:10:53,798 --> 08:10:58,840 complain because this doesn't match 10166 08:10:56,360 --> 08:11:02,160 whatever I have have here in the create 10167 08:10:58,840 --> 08:11:05,600 context so let's fix that let's set up a 10168 08:11:02,160 --> 08:11:08,398 type for our state which actually 10169 08:11:05,600 --> 08:11:12,398 matches whatever I have in the value so 10170 08:11:08,398 --> 08:11:16,318 we're going to go here with type theme 10171 08:11:12,398 --> 08:11:18,360 provider and let's say State props State 10172 08:11:16,318 --> 08:11:21,520 that's totally up to you I'm going to go 10173 08:11:18,360 --> 08:11:24,638 with theme and that is equal to a theme 10174 08:11:21,520 --> 08:11:27,080 over here so theme type and also a set 10175 08:11:24,638 --> 08:11:28,840 theme and essentially we'll just say 10176 08:11:27,080 --> 08:11:32,240 that that it's a function which is 10177 08:11:28,840 --> 08:11:34,798 looking for a theme and type will be 10178 08:11:32,240 --> 08:11:36,798 equal to a theme and it doesn't return 10179 08:11:34,798 --> 08:11:39,840 anything so we 10180 08:11:36,798 --> 08:11:42,478 simply provide void over here and 10181 08:11:39,840 --> 08:11:45,200 instead of hardcoding the object with 10182 08:11:42,478 --> 08:11:48,478 the name property which is a type string 10183 08:11:45,200 --> 08:11:52,040 we're going to go with theme provider 10184 08:11:48,478 --> 08:11:54,760 State now in this case it's complaining 10185 08:11:52,040 --> 08:11:57,798 because it says well theme can also be 10186 08:11:54,760 --> 08:12:00,600 undefined and we have multiple ways how 10187 08:11:57,798 --> 08:12:03,200 we can fix that either we can provide 10188 08:12:00,600 --> 08:12:05,718 the value like so so I can just say yeah 10189 08:12:03,200 --> 08:12:08,080 the default value is going to be dark 10190 08:12:05,718 --> 08:12:11,718 light or whatever 10191 08:12:08,080 --> 08:12:16,318 or in my scenario I'm actually going to 10192 08:12:11,718 --> 08:12:19,318 add one more property in the props and 10193 08:12:16,318 --> 08:12:23,360 therefore I will set up a separate type 10194 08:12:19,318 --> 08:12:27,240 so I'm going to go type theme 10195 08:12:23,360 --> 08:12:29,000 provider and let's call this props and 10196 08:12:27,240 --> 08:12:31,318 we'll look for two things first one is 10197 08:12:29,000 --> 08:12:34,760 going to be children so this one is the 10198 08:12:31,318 --> 08:12:39,040 same so react Dot and we're looking for 10199 08:12:34,760 --> 08:12:43,638 react node and also I want to set up the 10200 08:12:39,040 --> 08:12:46,718 default theme so let me add over here a 10201 08:12:43,638 --> 08:12:49,240 property default theme I will set it 10202 08:12:46,718 --> 08:12:51,718 optional and I'll actually set it equal 10203 08:12:49,240 --> 08:12:54,000 to type theme so it's going to be 10204 08:12:51,718 --> 08:12:57,520 optional but it needs to match one of 10205 08:12:54,000 --> 08:13:00,040 these values so light dark or system one 10206 08:12:57,520 --> 08:13:03,878 and where I have the theme provider I'll 10207 08:13:00,040 --> 08:13:07,600 actually remove the hardcoded one and 10208 08:13:03,878 --> 08:13:10,280 add theme provider props and in here 10209 08:13:07,600 --> 08:13:12,000 let's go with default theme and I will 10210 08:13:10,280 --> 08:13:14,600 add some default value which is actually 10211 08:13:12,000 --> 08:13:17,280 going to be a system and now at the very 10212 08:13:14,600 --> 08:13:20,120 end I'll go over here and provide 10213 08:13:17,280 --> 08:13:22,718 default theme and pretty much this 10214 08:13:20,120 --> 08:13:25,360 concludes our theme setup so as you can 10215 08:13:22,718 --> 08:13:28,280 see once we understand how we can set up 10216 08:13:25,360 --> 08:13:31,280 a basic context with just just one value 10217 08:13:28,280 --> 08:13:33,240 it's pretty easy to set up a more 10218 08:13:31,280 --> 08:13:37,360 complex one so now we just want to 10219 08:13:33,240 --> 08:13:40,040 navigate to index DSX and we want to 10220 08:13:37,360 --> 08:13:42,440 change our component around so this one 10221 08:13:40,040 --> 08:13:44,240 will stay the same and of course if you 10222 08:13:42,440 --> 08:13:46,398 want to test it out you can provide over 10223 08:13:44,240 --> 08:13:48,600 here the default value and all that 10224 08:13:46,398 --> 08:13:51,958 that's definitely an option but in my 10225 08:13:48,600 --> 08:13:55,160 case I just want to grab the context 10226 08:13:51,958 --> 08:13:58,920 with a theme and I also want to set up a 10227 08:13:55,160 --> 08:14:01,478 button which I'll use to change the 10228 08:13:58,920 --> 08:14:03,120 theme so in here right after this react 10229 08:14:01,478 --> 08:14:05,958 and typescript so I guess I'm going to 10230 08:14:03,120 --> 08:14:08,280 leave that I want to set up a button and 10231 08:14:05,958 --> 08:14:10,958 remember now in our context we have 10232 08:14:08,280 --> 08:14:13,520 multiple things not only we have theme 10233 08:14:10,958 --> 08:14:16,360 system but we also have this set theme 10234 08:14:13,520 --> 08:14:18,760 function so I want to try to invoke the 10235 08:14:16,360 --> 08:14:21,958 set theme on a button click just so I 10236 08:14:18,760 --> 08:14:25,878 can see whether my functionality works 10237 08:14:21,958 --> 08:14:28,398 so let's add over here a value I guess 10238 08:14:25,878 --> 08:14:31,318 I'm going to go with toggle theme and 10239 08:14:28,398 --> 08:14:33,760 then also let's add the onclick so we're 10240 08:14:31,318 --> 08:14:37,280 going to go over here with onclick let's 10241 08:14:33,760 --> 08:14:39,958 provide the arrow function and I'm just 10242 08:14:37,280 --> 08:14:43,318 going to check what is the value if the 10243 08:14:39,958 --> 08:14:47,040 value is equal to dark then I'll set it 10244 08:14:43,318 --> 08:14:49,120 back to system and if it's actually a 10245 08:14:47,040 --> 08:14:52,160 system one then I'm going to go with 10246 08:14:49,120 --> 08:14:55,280 theme dark so let's go here with context 10247 08:14:52,160 --> 08:14:57,478 Dot and we're looking for theme then we 10248 08:14:55,280 --> 08:14:59,878 want to check the value notice how we 10249 08:14:57,478 --> 08:15:01,840 right away get our nice suggestions I'm 10250 08:14:59,878 --> 08:15:05,398 going to say if it's equal to dark then 10251 08:15:01,840 --> 08:15:09,000 I want to set it to system context then 10252 08:15:05,398 --> 08:15:12,958 set theme and we provide the system one 10253 08:15:09,000 --> 08:15:14,878 then we return like so now if it's 10254 08:15:12,958 --> 08:15:18,680 already a system one then we just want 10255 08:15:14,878 --> 08:15:22,120 to go with context set theme and we 10256 08:15:18,680 --> 08:15:23,760 provide over here the value of dark and 10257 08:15:22,120 --> 08:15:27,040 you know what I also want to add some 10258 08:15:23,760 --> 08:15:31,080 classes here so let's go with BTN and BT 10259 08:15:27,040 --> 08:15:34,080 and Center let's save it now I have my 10260 08:15:31,080 --> 08:15:36,798 button and pretty much every time I'll 10261 08:15:34,080 --> 08:15:40,718 click on it I'll change this value now 10262 08:15:36,798 --> 08:15:44,160 we haven't set up any CSS so nothing is 10263 08:15:40,718 --> 08:15:47,160 going to change as far as the background 10264 08:15:44,160 --> 08:15:49,878 but notice as I'm clicking I'm actually 10265 08:15:47,160 --> 08:15:52,718 changing the value in a state so that's 10266 08:15:49,878 --> 08:15:55,040 how we can set up context API with 10267 08:15:52,718 --> 08:15:57,318 typescript okay and up next let's see 10268 08:15:55,040 --> 08:16:00,840 how we can use typescript with use 10269 08:15:57,318 --> 08:16:03,760 reducer hook and just like context yes 10270 08:16:00,840 --> 08:16:06,920 it's not as popular anymore since we 10271 08:16:03,760 --> 08:16:09,680 have options like Redux toolkit and Zoo 10272 08:16:06,920 --> 08:16:11,760 stand but you still might encounter it 10273 08:16:09,680 --> 08:16:13,878 from time to time and therefore I 10274 08:16:11,760 --> 08:16:17,718 decided to add it to the course just 10275 08:16:13,878 --> 08:16:20,000 like before we want to start in app DSX 10276 08:16:17,718 --> 08:16:23,680 hopefully this time I'm importing from 10277 08:16:20,000 --> 08:16:27,000 the correct folder so let me go with 10278 08:16:23,680 --> 08:16:29,920 star and now I want to go to Red 10279 08:16:27,000 --> 08:16:31,398 producers okay let's save it let's see 10280 08:16:29,920 --> 08:16:34,478 okay at the moment I have react and 10281 08:16:31,398 --> 08:16:37,520 typescript reducers looks about right 10282 08:16:34,478 --> 08:16:40,520 and this is the case where I do 10283 08:16:37,520 --> 08:16:43,798 suggest navigating to a readme and 10284 08:16:40,520 --> 08:16:45,958 basically we want to get some star code 10285 08:16:43,798 --> 08:16:48,680 because I really don't think that it's 10286 08:16:45,958 --> 08:16:51,760 going to be beneficial to type this 10287 08:16:48,680 --> 08:16:53,840 together so essentially go to 07 10288 08:16:51,760 --> 08:16:55,478 reducers and you're looking for this 10289 08:16:53,840 --> 08:16:58,080 function so this is what you want to set 10290 08:16:55,478 --> 08:17:00,280 up in your comp component not the app 10291 08:16:58,080 --> 08:17:03,718 DSX make sure you navigate to the 10292 08:17:00,280 --> 08:17:07,160 starter 07 and we're looking for the 10293 08:17:03,718 --> 08:17:09,398 index one this is what you want to paste 10294 08:17:07,160 --> 08:17:12,318 it over here and essentially this is 10295 08:17:09,398 --> 08:17:14,638 what you'll see in the browser so we'll 10296 08:17:12,318 --> 08:17:16,680 have increment decrement and reset so at 10297 08:17:14,638 --> 08:17:19,160 the moment nothing is happening we just 10298 08:17:16,680 --> 08:17:22,878 have some console logs over here and 10299 08:17:19,160 --> 08:17:26,160 also we have this set status to active 10300 08:17:22,878 --> 08:17:28,080 and inactive so basically our state is 10301 08:17:26,160 --> 08:17:29,680 going to consider consist of two things 10302 08:17:28,080 --> 08:17:32,680 so there's going to be some counter 10303 08:17:29,680 --> 08:17:35,718 value and also we'll change the string 10304 08:17:32,680 --> 08:17:38,760 value of active and inactive just to 10305 08:17:35,718 --> 08:17:41,000 Showcase how it works and our goal 10306 08:17:38,760 --> 08:17:44,878 essentially is to set up the US reducer 10307 08:17:41,000 --> 08:17:47,398 hook and also set up the reducer initial 10308 08:17:44,878 --> 08:17:50,080 State and all that cool stuff so now 10309 08:17:47,398 --> 08:17:52,958 let's navigate back to the component we 10310 08:17:50,080 --> 08:17:56,398 want to go with const and this is 10311 08:17:52,958 --> 08:17:59,478 returning two things State and also a 10312 08:17:56,398 --> 08:18:02,360 dispatch and we set it equal to use 10313 08:17:59,478 --> 08:18:04,760 reducer so we import that and it's going 10314 08:18:02,360 --> 08:18:08,120 to look for counter reducer so some kind 10315 08:18:04,760 --> 08:18:11,200 of reducer function and also initial 10316 08:18:08,120 --> 08:18:13,238 state so first let me just provide these 10317 08:18:11,200 --> 08:18:15,958 values please don't import them from the 10318 08:18:13,238 --> 08:18:19,680 final one we'll just go here with 10319 08:18:15,958 --> 08:18:21,958 counter reducer and initial state so 10320 08:18:19,680 --> 08:18:23,920 I'll set up both of these values now of 10321 08:18:21,958 --> 08:18:27,318 course we don't have them in this file 10322 08:18:23,920 --> 08:18:29,558 this is why we want to go to the folder 10323 08:18:27,318 --> 08:18:31,840 and we want to create a new one so in 10324 08:18:29,558 --> 08:18:35,520 this case I'm going to call this reducer 10325 08:18:31,840 --> 08:18:38,160 TS again if we have a component file yes 10326 08:18:35,520 --> 08:18:39,840 we go with DSX but if it's just going to 10327 08:18:38,160 --> 08:18:44,680 be a Javascript file then we just go 10328 08:18:39,840 --> 08:18:47,000 with TS so let's set up reducer yes and 10329 08:18:44,680 --> 08:18:49,478 let's start with the initial state so 10330 08:18:47,000 --> 08:18:52,200 this is something we're going to export 10331 08:18:49,478 --> 08:18:54,600 we're going to go with initial state in 10332 08:18:52,200 --> 08:18:56,558 a second we'll set up the type for now 10333 08:18:54,600 --> 08:18:59,920 let's just go with count which is equal 10334 08:18:56,558 --> 08:19:02,200 to to a zero and then status the initial 10335 08:18:59,920 --> 08:19:06,160 value will be pending but this is where 10336 08:19:02,200 --> 08:19:08,080 we'll change it to active and inactive 10337 08:19:06,160 --> 08:19:13,840 so now let's set up the type for this 10338 08:19:08,080 --> 08:19:16,200 one so export type counter State and in 10339 08:19:13,840 --> 08:19:19,360 here we have two values we have count 10340 08:19:16,200 --> 08:19:22,360 which is type number and also we have a 10341 08:19:19,360 --> 08:19:23,760 status which is equal to a string so now 10342 08:19:22,360 --> 08:19:27,160 we just want to go here where we have 10343 08:19:23,760 --> 08:19:30,280 the initial State and set up our type so 10344 08:19:27,160 --> 08:19:32,798 counter State okay awesome so we're 10345 08:19:30,280 --> 08:19:36,120 exporting that I guess we can now go 10346 08:19:32,798 --> 08:19:39,638 over here with import and we are looking 10347 08:19:36,120 --> 08:19:41,680 for our initial State and this is coming 10348 08:19:39,638 --> 08:19:43,798 from the same folder so we just look for 10349 08:19:41,680 --> 08:19:46,760 reducer so now we just need to set up 10350 08:19:43,798 --> 08:19:48,958 the counter reducer and for now it's 10351 08:19:46,760 --> 08:19:51,040 simply going to be a function and if you 10352 08:19:48,958 --> 08:19:54,478 know the reducer so it's looking for the 10353 08:19:51,040 --> 08:19:56,238 state as well as the action so what is 10354 08:19:54,478 --> 08:19:58,558 the state and what are we going to do 10355 08:19:56,238 --> 08:20:00,120 with that State and for now we're just 10356 08:19:58,558 --> 08:20:03,080 going to return that state so we're 10357 08:20:00,120 --> 08:20:06,120 going to go here with export then const 10358 08:20:03,080 --> 08:20:09,520 counter reducer so that will be the same 10359 08:20:06,120 --> 08:20:11,958 name and we'll set it equal to our 10360 08:20:09,520 --> 08:20:14,398 function our Arrow 10361 08:20:11,958 --> 08:20:16,760 function as far as the parameters this 10362 08:20:14,398 --> 08:20:18,760 is what we're looking for the state 10363 08:20:16,760 --> 08:20:20,718 which is actually equal to a counter 10364 08:20:18,760 --> 08:20:24,160 State and action and for now let's just 10365 08:20:20,718 --> 08:20:25,638 set it equal to any of course as we're 10366 08:20:24,160 --> 08:20:27,440 working on a functionality we're going 10367 08:20:25,638 --> 08:20:28,958 to fix that and and as far as what we 10368 08:20:27,440 --> 08:20:32,080 want to return from the function we're 10369 08:20:28,958 --> 08:20:34,878 going to go with counter State and then 10370 08:20:32,080 --> 08:20:37,840 inside we're going to go with a return 10371 08:20:34,878 --> 08:20:39,798 and state so pretty much for now we're 10372 08:20:37,840 --> 08:20:42,318 just going to grab the state and we'll 10373 08:20:39,798 --> 08:20:44,478 return State as it is so then back in 10374 08:20:42,318 --> 08:20:47,680 the index the 10375 08:20:44,478 --> 08:20:50,120 SX not only we have initial state but we 10376 08:20:47,680 --> 08:20:53,680 also have the counter 10377 08:20:50,120 --> 08:20:56,080 reducer so in the upcoming videos we can 10378 08:20:53,680 --> 08:20:57,958 start working on the functionality and 10379 08:20:56,080 --> 08:21:01,200 once we have setup in place I guess 10380 08:20:57,958 --> 08:21:03,080 let's start by displaying the value from 10381 08:21:01,200 --> 08:21:05,398 the state so at the moment we're 10382 08:21:03,080 --> 08:21:07,840 hardcoding the count as well as the 10383 08:21:05,398 --> 08:21:10,478 status but keep in mind that in here we 10384 08:21:07,840 --> 08:21:12,280 already have some initial values so 10385 08:21:10,478 --> 08:21:15,360 let's just access 10386 08:21:12,280 --> 08:21:17,718 those count is going to be equal to the 10387 08:21:15,360 --> 08:21:20,558 state that we're getting back and notice 10388 08:21:17,718 --> 08:21:22,798 how we have these two properties and in 10389 08:21:20,558 --> 08:21:25,160 this case I'm looking for the count and 10390 08:21:22,798 --> 08:21:27,840 the same is going to be for the active 10391 08:21:25,160 --> 08:21:31,318 one instead of Ive actually is going to 10392 08:21:27,840 --> 08:21:34,280 be equal to a state and then the status 10393 08:21:31,318 --> 08:21:36,280 let's save it notice now we have count 10394 08:21:34,280 --> 08:21:39,080 and also we have the pending one since 10395 08:21:36,280 --> 08:21:42,080 that's my default value and I think I'm 10396 08:21:39,080 --> 08:21:44,958 going to start with these ones so we 10397 08:21:42,080 --> 08:21:48,160 want to set up few types one for 10398 08:21:44,958 --> 08:21:51,080 increment decrement as well as the reset 10399 08:21:48,160 --> 08:21:53,600 one and we're going to start by setting 10400 08:21:51,080 --> 08:21:56,878 up a type and I'm going to call this 10401 08:21:53,600 --> 08:22:00,160 update count action so that will will be 10402 08:21:56,878 --> 08:22:05,478 my type over here so let's set it up so 10403 08:22:00,160 --> 08:22:07,680 type update count action and here I'm 10404 08:22:05,478 --> 08:22:09,160 going to go with the literal one so 10405 08:22:07,680 --> 08:22:14,080 either it's going to be increment 10406 08:22:09,160 --> 08:22:18,280 decrement or reset so type increment 10407 08:22:14,080 --> 08:22:21,920 then Union we also can have decrement 10408 08:22:18,280 --> 08:22:25,398 and also we can reset the value over 10409 08:22:21,920 --> 08:22:28,398 here and now I'm going to create another 10410 08:22:25,398 --> 08:22:32,680 type since there will be one for the 10411 08:22:28,398 --> 08:22:35,798 status one but technically I mean if I 10412 08:22:32,680 --> 08:22:38,120 would only have the count over here so 10413 08:22:35,798 --> 08:22:40,000 if I wouldn't have functionality to 10414 08:22:38,120 --> 08:22:42,638 change the status I can simply go to 10415 08:22:40,000 --> 08:22:45,920 update count action and set it equal to 10416 08:22:42,638 --> 08:22:47,718 my action so that will be my type but 10417 08:22:45,920 --> 08:22:50,080 since there's going to be more logic I'm 10418 08:22:47,718 --> 08:22:52,920 going to go with type and then 10419 08:22:50,080 --> 08:22:56,600 counter action and that one will be for 10420 08:22:52,920 --> 08:22:59,040 now equal to update count action so now 10421 08:22:56,600 --> 08:23:01,958 where we have the action in the counter 10422 08:22:59,040 --> 08:23:04,680 reducer we want to go with counter 10423 08:23:01,958 --> 08:23:07,840 action and now let's set up our logic 10424 08:23:04,680 --> 08:23:09,840 where inside of the counter reducer 10425 08:23:07,840 --> 08:23:12,200 instead of returning the state we're 10426 08:23:09,840 --> 08:23:15,360 going to go switch statement and we want 10427 08:23:12,200 --> 08:23:18,120 to go with action and type notice again 10428 08:23:15,360 --> 08:23:19,040 we have these potential three values and 10429 08:23:18,120 --> 08:23:22,600 then 10430 08:23:19,040 --> 08:23:25,280 based on the actual value we'll do some 10431 08:23:22,600 --> 08:23:27,638 logic over here so first let's set up 10432 08:23:25,280 --> 08:23:31,558 the default one and and let's say that 10433 08:23:27,638 --> 08:23:34,440 by default we'll just return a state we 10434 08:23:31,558 --> 08:23:38,200 say State over here and 10435 08:23:34,440 --> 08:23:40,798 then as far as the cases let's go with 10436 08:23:38,200 --> 08:23:43,040 increment first so that's going to be 10437 08:23:40,798 --> 08:23:46,558 our first case and then we want to go 10438 08:23:43,040 --> 08:23:49,478 with return and we want to pretty much 10439 08:23:46,558 --> 08:23:51,760 spread out the current state so we only 10440 08:23:49,478 --> 08:23:56,398 want to update and then count is going 10441 08:23:51,760 --> 08:23:58,200 to be equal to State DOT and count Plus 10442 08:23:56,398 --> 08:24:01,080 one and prettyy much we want to do the 10443 08:23:58,200 --> 08:24:05,318 same thing for rest of them so the 10444 08:24:01,080 --> 08:24:07,840 second one will be decrement case 10445 08:24:05,318 --> 08:24:11,878 decrement as you can see we only have 10446 08:24:07,840 --> 08:24:14,680 two options at the moment so decrement 10447 08:24:11,878 --> 08:24:17,000 and we just want to change this around 10448 08:24:14,680 --> 08:24:19,080 where instead of plus one we're going to 10449 08:24:17,000 --> 08:24:23,238 go with minus one and then lastly we 10450 08:24:19,080 --> 08:24:27,000 also have the reset case reset and if 10451 08:24:23,238 --> 08:24:31,120 that's the case just want to grab this 10452 08:24:27,000 --> 08:24:36,440 State and basically set the count equal 10453 08:24:31,120 --> 08:24:39,360 to zero now in order to invoke we need 10454 08:24:36,440 --> 08:24:41,798 to use the dispatch and we need to pass 10455 08:24:39,360 --> 08:24:44,520 in the correct type so instead of just 10456 08:24:41,798 --> 08:24:47,638 logging it over here we want to go 10457 08:24:44,520 --> 08:24:50,638 dispatch and then we need to provide 10458 08:24:47,638 --> 08:24:54,200 what is going to be the type and as far 10459 08:24:50,638 --> 08:24:56,360 as the increment one it's over here and 10460 08:24:54,200 --> 08:24:58,000 we want to repeat for decrement the same 10461 08:24:56,360 --> 08:25:02,558 same 10462 08:24:58,000 --> 08:25:04,318 dispatch type and then decrement and 10463 08:25:02,558 --> 08:25:06,760 then at the very end we also have the 10464 08:25:04,318 --> 08:25:09,798 recet one so as you can see the biggest 10465 08:25:06,760 --> 08:25:13,600 difference is that now we have a clear 10466 08:25:09,798 --> 08:25:16,238 control over the action type so usually 10467 08:25:13,600 --> 08:25:18,238 this was the biggest pain points where 10468 08:25:16,238 --> 08:25:20,040 you could provide some random value once 10469 08:25:18,238 --> 08:25:22,318 we use the typescript it's actually not 10470 08:25:20,040 --> 08:25:24,600 the case anymore we clearly have a list 10471 08:25:22,318 --> 08:25:26,760 of values that we can provide and we're 10472 08:25:24,600 --> 08:25:28,718 not going to provide anything random so 10473 08:25:26,760 --> 08:25:31,398 notice now I navigate over here to the 10474 08:25:28,718 --> 08:25:33,600 browser and I have these three working 10475 08:25:31,398 --> 08:25:36,520 buttons okay and let's also see how we 10476 08:25:33,600 --> 08:25:39,398 can handle the action where we have the 10477 08:25:36,520 --> 08:25:43,160 payload so with our counter pretty much 10478 08:25:39,398 --> 08:25:45,280 we're only handling the type correct and 10479 08:25:43,160 --> 08:25:47,760 then based on that we control the state 10480 08:25:45,280 --> 08:25:50,760 value but we can also pass in the 10481 08:25:47,760 --> 08:25:52,958 payload and for starters let's set up a 10482 08:25:50,760 --> 08:25:57,238 type for that so we're going to go with 10483 08:25:52,958 --> 08:25:59,958 set status and let's call this action as 10484 08:25:57,238 --> 08:26:02,280 well in here there's also going to be a 10485 08:25:59,958 --> 08:26:03,000 type so this property we definitely need 10486 08:26:02,280 --> 08:26:05,440 to 10487 08:26:03,000 --> 08:26:08,840 provide and as far as the value I'm 10488 08:26:05,440 --> 08:26:10,200 going to go with set status and then 10489 08:26:08,840 --> 08:26:13,318 when it comes to payload I'm just going 10490 08:26:10,200 --> 08:26:15,360 to go with few options and again I'm 10491 08:26:13,318 --> 08:26:16,520 going to use my literal type so I'm 10492 08:26:15,360 --> 08:26:19,680 going to go with 10493 08:26:16,520 --> 08:26:23,638 active and also it's going to be equal 10494 08:26:19,680 --> 08:26:27,360 to inactive then we want to navigate to 10495 08:26:23,638 --> 08:26:30,280 our reducer and let's set up the case 10496 08:26:27,360 --> 08:26:33,160 where we're going to go with set and 10497 08:26:30,280 --> 08:26:36,878 status and as I note before we do that 10498 08:26:33,160 --> 08:26:39,840 actually we need to add this set status 10499 08:26:36,878 --> 08:26:43,080 action to our counter action to our 10500 08:26:39,840 --> 08:26:45,000 counter action type because this is how 10501 08:26:43,080 --> 08:26:47,878 we're checking actually in the reducer 10502 08:26:45,000 --> 08:26:49,440 so let's go with set status action and 10503 08:26:47,878 --> 08:26:51,360 now notice how we're going to get some 10504 08:26:49,440 --> 08:26:55,478 nice suggestions so if I go over here 10505 08:26:51,360 --> 08:26:57,120 with case and then set status now we 10506 08:26:55,478 --> 08:27:00,200 just need to decide what are we going to 10507 08:26:57,120 --> 08:27:03,280 do and in my case I'm going to return 10508 08:27:00,200 --> 08:27:06,160 state so that doesn't change and as note 10509 08:27:03,280 --> 08:27:09,638 there is a error over here and now we 10510 08:27:06,160 --> 08:27:10,478 just need to update the I believe what 10511 08:27:09,638 --> 08:27:14,638 was 10512 08:27:10,478 --> 08:27:17,280 the value the status one correct so in 10513 08:27:14,638 --> 08:27:19,600 the state there is a status one and 10514 08:27:17,280 --> 08:27:23,600 we're just going to set it equal 10515 08:27:19,600 --> 08:27:25,760 to action and payload so we'll set it 10516 08:27:23,600 --> 08:27:29,200 equal to one of these strings over here 10517 08:27:25,760 --> 08:27:32,280 so now we want to navigate to index 10518 08:27:29,200 --> 08:27:35,638 DSX and where we have the console logs 10519 08:27:32,280 --> 08:27:38,798 to active and inactive same deal we just 10520 08:27:35,638 --> 08:27:42,520 want to dispatch it type going to be 10521 08:27:38,798 --> 08:27:44,840 equal to and we want to go with set 10522 08:27:42,520 --> 08:27:48,360 status and then we will need to provide 10523 08:27:44,840 --> 08:27:51,360 the payload as well so in here let's go 10524 08:27:48,360 --> 08:27:53,520 with payload and this will be equal to 10525 08:27:51,360 --> 08:27:55,878 active one I guess that's going to be 10526 08:27:53,520 --> 08:27:58,120 the first button and now we want to copy 10527 08:27:55,878 --> 08:28:01,360 and based and just change these values 10528 08:27:58,120 --> 08:28:02,798 around so instead of active we're going 10529 08:28:01,360 --> 08:28:05,958 to go with 10530 08:28:02,798 --> 08:28:08,878 inactive yep if I provide the correct 10531 08:28:05,958 --> 08:28:10,718 value everything is awesome so now let's 10532 08:28:08,878 --> 08:28:13,360 just navigate to the browser and not 10533 08:28:10,718 --> 08:28:15,600 only we can control the counter but we 10534 08:28:13,360 --> 08:28:18,478 can also change these values so we can 10535 08:28:15,600 --> 08:28:20,798 also pass the payload and at the very 10536 08:28:18,478 --> 08:28:24,120 end let's also not forget about the type 10537 08:28:20,798 --> 08:28:27,638 never so let me navigate back to the 10538 08:28:24,120 --> 08:28:30,440 reducer and we can see over here that by 10539 08:28:27,638 --> 08:28:32,398 default we return the state which 10540 08:28:30,440 --> 08:28:34,958 technically is okay but just keep in 10541 08:28:32,398 --> 08:28:38,798 mind we can add some kind of type over 10542 08:28:34,958 --> 08:28:40,798 here and this is going to fail silently 10543 08:28:38,798 --> 08:28:43,040 so for example if I'm going to add 10544 08:28:40,798 --> 08:28:45,238 another type over here I'm going to go 10545 08:28:43,040 --> 08:28:47,680 with something random if I'm going to 10546 08:28:45,238 --> 08:28:49,920 dispatch it I mean it's going to go 10547 08:28:47,680 --> 08:28:52,440 through my cases and essentially I'll 10548 08:28:49,920 --> 08:28:55,360 just get back to state so the problem 10549 08:28:52,440 --> 08:28:58,318 with this one is that it's very hard to 10550 08:28:55,360 --> 08:29:00,680 catch this error and therefore in here 10551 08:28:58,318 --> 08:29:03,238 where we have the default actually we 10552 08:29:00,680 --> 08:29:07,398 want to add some logic where I'm going 10553 08:29:03,238 --> 08:29:09,238 to go with con on handled action type 10554 08:29:07,398 --> 08:29:11,478 remember type is never and we're going 10555 08:29:09,238 --> 08:29:14,200 to set it equal to an action and now 10556 08:29:11,478 --> 08:29:17,280 notice how immediately it complains 10557 08:29:14,200 --> 08:29:20,200 because effectively there is another 10558 08:29:17,280 --> 08:29:23,600 type the moment actually it's random one 10559 08:29:20,200 --> 08:29:26,238 over here and unless we handle it so 10560 08:29:23,600 --> 08:29:29,520 unless I go over here case and then and 10561 08:29:26,238 --> 08:29:32,160 random and then decide what I want to do 10562 08:29:29,520 --> 08:29:36,238 for example return and then dot dot dot 10563 08:29:32,160 --> 08:29:38,878 State unless I do that there will be a 10564 08:29:36,238 --> 08:29:42,360 error and what's really cool that it's 10565 08:29:38,878 --> 08:29:44,680 actually at buil time so not at runtime 10566 08:29:42,360 --> 08:29:47,718 so now let me remove this example over 10567 08:29:44,680 --> 08:29:50,760 here and let's just complete by throwing 10568 08:29:47,718 --> 08:29:53,878 a error so let me go with throw new 10569 08:29:50,760 --> 08:29:55,718 error and let's provide some error 10570 08:29:53,878 --> 08:29:58,318 message and once we have this one in 10571 08:29:55,718 --> 08:30:00,760 place now we can remove it and not only 10572 08:29:58,318 --> 08:30:04,718 we'll be able to catch the error but 10573 08:30:00,760 --> 08:30:06,718 also if for some reason we add type and 10574 08:30:04,718 --> 08:30:09,478 we dispatch it there's going to be a 10575 08:30:06,718 --> 08:30:11,760 super helpful error in the console which 10576 08:30:09,478 --> 08:30:14,040 right away will lead back to the counter 10577 08:30:11,760 --> 08:30:16,600 reducer all right and up next let's see 10578 08:30:14,040 --> 08:30:18,760 how we can fetch data in react with 10579 08:30:16,600 --> 08:30:21,280 typescript and we'll actually look at 10580 08:30:18,760 --> 08:30:24,238 two options the good old use effect and 10581 08:30:21,280 --> 08:30:26,638 fetch API and my current favorite one 10582 08:30:24,238 --> 08:30:28,280 the react query with axio 10583 08:30:26,638 --> 08:30:31,000 please keep in mind that this section 10584 08:30:28,280 --> 08:30:33,840 will only make sense if you watch the 10585 08:30:31,000 --> 08:30:36,200 fetch data fundamentals part since I'm 10586 08:30:33,840 --> 08:30:38,878 not going to repeat things like Zod 10587 08:30:36,200 --> 08:30:40,318 setup and the actual need for types just 10588 08:30:38,878 --> 08:30:42,798 like in the previous examples we're 10589 08:30:40,318 --> 08:30:44,718 going to start in the app TSX where 10590 08:30:42,798 --> 08:30:48,478 we're looking for component and more 10591 08:30:44,718 --> 08:30:50,398 specifically the star and then 08 fetch 10592 08:30:48,478 --> 08:30:52,958 data and then if we take a look at the 10593 08:30:50,398 --> 08:30:55,280 browser this is what we should see we'll 10594 08:30:52,958 --> 08:30:57,718 also use the same URL like in 10595 08:30:55,280 --> 08:31:01,360 fundamentals so basically will fetch the 10596 08:30:57,718 --> 08:31:03,600 same doors from my server and therefore 10597 08:31:01,360 --> 08:31:06,878 I suggest just navigating to read me 10598 08:31:03,600 --> 08:31:09,238 you're looking for 08 fetch data and 10599 08:31:06,878 --> 08:31:12,040 just grab these two lines of code so 10600 08:31:09,238 --> 08:31:14,238 we'll utilize these hooks in a second 10601 08:31:12,040 --> 08:31:18,000 and then also here's the URL we're going 10602 08:31:14,238 --> 08:31:21,238 to use so grab it over here then we're 10603 08:31:18,000 --> 08:31:23,638 going to navigate back and I guess let's 10604 08:31:21,238 --> 08:31:26,398 just start setting up the state values 10605 08:31:23,638 --> 08:31:28,200 now eventually we'll set up the tors 10606 08:31:26,398 --> 08:31:30,080 but at the end of this video I'll show 10607 08:31:28,200 --> 08:31:33,040 you why we're not doing that right now 10608 08:31:30,080 --> 08:31:34,958 why essentially we need a type first and 10609 08:31:33,040 --> 08:31:37,718 then let's set up two more State values 10610 08:31:34,958 --> 08:31:40,840 we're going to have one for is 10611 08:31:37,718 --> 08:31:43,238 loading and then also set is loading so 10612 08:31:40,840 --> 08:31:45,200 this is going to be true while we're 10613 08:31:43,238 --> 08:31:47,958 fetching we're going to go with use 10614 08:31:45,200 --> 08:31:50,920 State and we can provide angle brackets 10615 08:31:47,958 --> 08:31:52,760 here it's definitely an option or we can 10616 08:31:50,920 --> 08:31:54,600 just type over here the default value 10617 08:31:52,760 --> 08:31:56,478 and then typescript is going to be super 10618 08:31:54,600 --> 08:31:59,000 smart and it's going to figure out that 10619 08:31:56,478 --> 08:32:01,120 essentially this will be a Boolean so I 10620 08:31:59,000 --> 08:32:03,080 guess let's just go with that one and as 10621 08:32:01,120 --> 08:32:05,520 far as my default value it will be set 10622 08:32:03,080 --> 08:32:08,120 equal to false then we also want to go 10623 08:32:05,520 --> 08:32:11,000 with is error and we're going to set it 10624 08:32:08,120 --> 08:32:14,318 to set is 10625 08:32:11,000 --> 08:32:16,718 error so that will be our set function 10626 08:32:14,318 --> 08:32:19,080 and here we're going to provide the 10627 08:32:16,718 --> 08:32:21,558 angle brackets and there's going to be a 10628 08:32:19,080 --> 08:32:24,080 union type so either it's going to be a 10629 08:32:21,558 --> 08:32:26,798 string so if there's error I'm actually 10630 08:32:24,080 --> 08:32:29,878 going to use the error message or or it 10631 08:32:26,798 --> 08:32:31,958 will be null and null will be my default 10632 08:32:29,878 --> 08:32:34,920 value so I'll just provide null over 10633 08:32:31,958 --> 08:32:37,318 here then the good old use effect and we 10634 08:32:34,920 --> 08:32:39,360 want to set up the fetch function inside 10635 08:32:37,318 --> 08:32:41,520 of it so let's go with our use effect 10636 08:32:39,360 --> 08:32:43,318 let's provide the Callback function 10637 08:32:41,520 --> 08:32:46,160 we're just going to invoke it when the 10638 08:32:43,318 --> 08:32:50,638 component mounts and inside of it let's 10639 08:32:46,160 --> 08:32:54,000 set up our fetch data which will be my 10640 08:32:50,638 --> 08:32:57,160 the same function so fetch data and 10641 08:32:54,000 --> 08:32:59,878 let's set it equal to a 10642 08:32:57,160 --> 08:33:02,558 sync and as far as the functionality 10643 08:32:59,878 --> 08:33:06,520 since it's a sync we want to nicely wrap 10644 08:33:02,558 --> 08:33:08,398 everything in the try and catch and also 10645 08:33:06,520 --> 08:33:11,318 we want to start by setting is loading 10646 08:33:08,398 --> 08:33:13,638 is true so while we're fetching we'll 10647 08:33:11,318 --> 08:33:16,760 set it equal to true and then at the end 10648 08:33:13,638 --> 08:33:18,718 we'll set it essentially equal to false 10649 08:33:16,760 --> 08:33:21,760 and you know what actually in here let's 10650 08:33:18,718 --> 08:33:25,120 go with finally so here we'll just set 10651 08:33:21,760 --> 08:33:27,798 it set is loading and equal to false so 10652 08:33:25,120 --> 08:33:29,958 whether we're getting the error or we're 10653 08:33:27,798 --> 08:33:31,718 successful doesn't really matter at the 10654 08:33:29,958 --> 08:33:35,238 end of the day we'll always set it back 10655 08:33:31,718 --> 08:33:38,440 to false now in the tri block this is 10656 08:33:35,238 --> 08:33:40,360 where we want to construct our fetch 10657 08:33:38,440 --> 08:33:42,798 which again we can right away start 10658 08:33:40,360 --> 08:33:45,280 using because we have access to it so 10659 08:33:42,798 --> 08:33:48,238 we're going to go here with await fetch 10660 08:33:45,280 --> 08:33:51,040 then we provide the URL then fetch API 10661 08:33:48,238 --> 08:33:54,398 has that Quirk where effectively we do 10662 08:33:51,040 --> 08:33:56,080 want to catch the 404 errors as well so 10663 08:33:54,398 --> 08:33:59,160 I'm just going to check for that okay 10664 08:33:56,080 --> 08:34:00,920 properly so if I have some kind of error 10665 08:33:59,160 --> 08:34:04,360 I actually want to throw it over here so 10666 08:34:00,920 --> 08:34:07,040 throw new error which of course will 10667 08:34:04,360 --> 08:34:09,760 right away lead to a catch block and 10668 08:34:07,040 --> 08:34:13,600 then as far as the message I'm just 10669 08:34:09,760 --> 08:34:16,200 going to say failed to fetch doers and 10670 08:34:13,600 --> 08:34:19,680 after this condition I want to set up my 10671 08:34:16,200 --> 08:34:22,478 raw data since eventually we'll set up 10672 08:34:19,680 --> 08:34:23,840 Zod as well and for now let's just go to 10673 08:34:22,478 --> 08:34:27,318 response. 10674 08:34:23,840 --> 08:34:29,520 Json and then let's log this data so 10675 08:34:27,318 --> 08:34:32,840 we're not going to set anything in the 10676 08:34:29,520 --> 08:34:34,478 state and when it comes to a catch let's 10677 08:34:32,840 --> 08:34:36,680 again construct that message so we're 10678 08:34:34,478 --> 08:34:39,080 going to go with message and this one is 10679 08:34:36,680 --> 08:34:43,798 equal to error and we're going to check 10680 08:34:39,080 --> 08:34:46,638 whether error is the instance off and 10681 08:34:43,798 --> 08:34:48,840 error and if that's the case then we're 10682 08:34:46,638 --> 08:34:51,440 going to go with error and 10683 08:34:48,840 --> 08:34:54,558 message or there was an error so 10684 08:34:51,440 --> 08:34:58,600 something generic and we also want to go 10685 08:34:54,558 --> 08:35:01,958 here with set is eror and we want to 10686 08:34:58,600 --> 08:35:06,080 provide our message so that should do it 10687 08:35:01,958 --> 08:35:08,718 we just want to invoke it over here and 10688 08:35:06,080 --> 08:35:11,440 we want to set up three returns so at 10689 08:35:08,718 --> 08:35:14,600 the moment we have one so basically this 10690 08:35:11,440 --> 08:35:17,520 is our standard return and now let's 10691 08:35:14,600 --> 08:35:19,520 check for loading let's check for is 10692 08:35:17,520 --> 08:35:21,798 error and then let's see whether we 10693 08:35:19,520 --> 08:35:24,238 actually are getting data and then we'll 10694 08:35:21,798 --> 08:35:26,638 discuss why we want to set up the tours 10695 08:35:24,238 --> 08:35:28,680 in the following video so let's start 10696 08:35:26,638 --> 08:35:33,040 over here we're going to go with if and 10697 08:35:28,680 --> 08:35:36,160 then is loading okay good and then we're 10698 08:35:33,040 --> 08:35:40,120 going to go with heading three loading 10699 08:35:36,160 --> 08:35:41,600 and then d da D okay good then we also 10700 08:35:40,120 --> 08:35:42,638 want to do the same thing pretty much 10701 08:35:41,600 --> 08:35:45,680 for 10702 08:35:42,638 --> 08:35:48,280 error so now we're checking for this 10703 08:35:45,680 --> 08:35:52,520 value and we're going to go with error 10704 08:35:48,280 --> 08:35:55,200 and actually we can display the message 10705 08:35:52,520 --> 08:35:58,280 as well just in case there is any so I'm 10706 08:35:55,200 --> 08:35:59,600 going to go with error and let's access 10707 08:35:58,280 --> 08:36:02,360 the error 10708 08:35:59,600 --> 08:36:04,238 message is error and then I'll just 10709 08:36:02,360 --> 08:36:06,760 leave this one for now So eventually 10710 08:36:04,238 --> 08:36:09,318 we'll iterate over that doors so now let 10711 08:36:06,760 --> 08:36:11,000 me navigate to the browser again if 10712 08:36:09,318 --> 08:36:13,318 everything is correct we should see 10713 08:36:11,000 --> 08:36:16,000 those doors and I can see the array of 10714 08:36:13,318 --> 08:36:18,718 Five Doors so that works should also 10715 08:36:16,000 --> 08:36:20,200 have that loading for a split second and 10716 08:36:18,718 --> 08:36:23,080 let's say if we're going to mess with 10717 08:36:20,200 --> 08:36:26,040 the URL if I'm going 10718 08:36:23,080 --> 08:36:29,638 to provide the wrong URL notice now I 10719 08:36:26,040 --> 08:36:32,040 have actually error failed to fetch T 10720 08:36:29,638 --> 08:36:36,318 and now again let's reiterate why do we 10721 08:36:32,040 --> 08:36:38,440 need a type so let's try to set up do 10722 08:36:36,318 --> 08:36:40,120 state value at the moment we don't have 10723 08:36:38,440 --> 08:36:44,238 the type so essentially we'll just 10724 08:36:40,120 --> 08:36:46,558 provide Mt then let's try to set it over 10725 08:36:44,238 --> 08:36:49,120 here in the fetch data and then let's 10726 08:36:46,558 --> 08:36:51,478 iterate over and see where the issue is 10727 08:36:49,120 --> 08:36:55,280 going to be so for STS we want to go 10728 08:36:51,478 --> 08:36:57,360 over here with const tours set tours 10729 08:36:55,280 --> 08:36:59,638 okay beautiful then we want to go with 10730 08:36:57,360 --> 08:37:02,040 use State and remember if we're just 10731 08:36:59,638 --> 08:37:05,920 going to provide over here Mt aray which 10732 08:37:02,040 --> 08:37:09,760 will be default value notice now T is 10733 08:37:05,920 --> 08:37:12,760 actually set equal to never which means 10734 08:37:09,760 --> 08:37:16,760 that yeah I can go over here and I can 10735 08:37:12,760 --> 08:37:19,760 go with set tours I can provide broad a 10736 08:37:16,760 --> 08:37:22,958 okay beautiful everything is awesome and 10737 08:37:19,760 --> 08:37:25,558 then let's say right after the react 10738 08:37:22,958 --> 08:37:29,000 typescript we're going to go with doors 10739 08:37:25,558 --> 08:37:31,318 then map so we're iterating over and 10740 08:37:29,000 --> 08:37:34,440 then as far as the function well let's 10741 08:37:31,318 --> 08:37:36,200 access the tour correct okay good and 10742 08:37:34,440 --> 08:37:39,238 then we're going to go with some kind of 10743 08:37:36,200 --> 08:37:41,120 heading two we provide hello we can even 10744 08:37:39,238 --> 08:37:44,200 see those hellos in the browser okay 10745 08:37:41,120 --> 08:37:47,040 beautiful but what if we're going to try 10746 08:37:44,200 --> 08:37:48,798 something like this let's say I want to 10747 08:37:47,040 --> 08:37:51,520 access one of the properties I'm going 10748 08:37:48,798 --> 08:37:55,120 to go with the name one so now if we go 10749 08:37:51,520 --> 08:37:56,958 here and say tour and that name this is 10750 08:37:55,120 --> 08:38:00,440 not going going to fly typescript is 10751 08:37:56,958 --> 08:38:03,120 going to say well you cannot access the 10752 08:38:00,440 --> 08:38:06,958 name property on type never and this is 10753 08:38:03,120 --> 08:38:08,920 why we do again need to set up D type 10754 08:38:06,958 --> 08:38:11,238 and in our case since we already know 10755 08:38:08,920 --> 08:38:14,000 how to work with Zod and since we're 10756 08:38:11,238 --> 08:38:17,440 going to use Zod 10757 08:38:14,000 --> 08:38:20,920 to evaluate our data at run time anyway 10758 08:38:17,440 --> 08:38:23,478 this is going to be our approach okay so 10759 08:38:20,920 --> 08:38:26,000 now let's set up our type and we'll do 10760 08:38:23,478 --> 08:38:29,200 that with the help of Zod Library since 10761 08:38:26,000 --> 08:38:31,398 that way we can also check the data at 10762 08:38:29,200 --> 08:38:35,040 runtime and this is totally up to you 10763 08:38:31,398 --> 08:38:37,120 you can set up the file name utls or 10764 08:38:35,040 --> 08:38:39,120 something along those lines I think in 10765 08:38:37,120 --> 08:38:42,600 my case I'm just going to go with types 10766 08:38:39,120 --> 08:38:44,840 now as a side note also in this file we 10767 08:38:42,600 --> 08:38:47,680 will set up a fetch 10768 08:38:44,840 --> 08:38:50,238 function with the axio so that is coming 10769 08:38:47,680 --> 08:38:52,200 up so basically we'll just Jam bunch of 10770 08:38:50,238 --> 08:38:54,280 the things normally in the bigger 10771 08:38:52,200 --> 08:38:55,840 project you would separate them but I 10772 08:38:54,280 --> 08:38:57,798 don't think there is any need so we're 10773 08:38:55,840 --> 08:38:59,920 looking for Zod Library which again is 10774 08:38:57,798 --> 08:39:02,360 already installed if you're using the 10775 08:38:59,920 --> 08:39:05,680 star if not then just please install the 10776 08:39:02,360 --> 08:39:09,398 library then let's construct that door 10777 08:39:05,680 --> 08:39:12,638 schema which will also then infer to be 10778 08:39:09,398 --> 08:39:15,040 our door so let's export that since we 10779 08:39:12,638 --> 08:39:19,558 also want to use that one in the inex so 10780 08:39:15,040 --> 08:39:22,478 we'll go with export then con store and 10781 08:39:19,558 --> 08:39:25,878 then we're looking for schema over here 10782 08:39:22,478 --> 08:39:27,360 so this one will be equal to Z do object 10783 08:39:25,878 --> 08:39:30,318 so this is what we're 10784 08:39:27,360 --> 08:39:32,840 constructing and I believe I had five 10785 08:39:30,318 --> 08:39:35,040 properties in there so all of them were 10786 08:39:32,840 --> 08:39:38,000 string that's for sure so we're going to 10787 08:39:35,040 --> 08:39:39,798 go over here then let's copy and paste 10788 08:39:38,000 --> 08:39:42,920 and I believe it's one too many by the 10789 08:39:39,798 --> 08:39:46,958 way so let me remove this one and then 10790 08:39:42,920 --> 08:39:51,840 we have one for name also we want to set 10791 08:39:46,958 --> 08:39:54,318 up over here image then same deal info 10792 08:39:51,840 --> 08:39:57,920 and at the very end we'll have the price 10793 08:39:54,318 --> 08:40:00,798 one I'm missing comma 10794 08:39:57,920 --> 08:40:04,040 so let me do that as well and now let's 10795 08:40:00,798 --> 08:40:06,638 construct our type this is something 10796 08:40:04,040 --> 08:40:08,840 also we're going to export so let's go 10797 08:40:06,638 --> 08:40:12,080 here with export type and then tour and 10798 08:40:08,840 --> 08:40:15,000 that is equal to Z infer so now we just 10799 08:40:12,080 --> 08:40:17,920 infer a type which is a generic and 10800 08:40:15,000 --> 08:40:20,718 we're just going to go type off and then 10801 08:40:17,920 --> 08:40:24,478 to our schema so now we have both we 10802 08:40:20,718 --> 08:40:26,478 have our schema and we also have our 10803 08:40:24,478 --> 08:40:28,638 type so now let's navigate back over 10804 08:40:26,478 --> 08:40:32,080 here and we want to import both of those 10805 08:40:28,638 --> 08:40:33,520 things we want to grab the tour as well 10806 08:40:32,080 --> 08:40:36,958 as the tour 10807 08:40:33,520 --> 08:40:40,398 schema so let me set her up I think I'm 10808 08:40:36,958 --> 08:40:43,238 going to type over here type tour just 10809 08:40:40,398 --> 08:40:45,238 to make it more explicit what we're 10810 08:40:43,238 --> 08:40:48,160 doing so we want to grab the scheme as 10811 08:40:45,238 --> 08:40:52,318 well and then this one is coming from 10812 08:40:48,160 --> 08:40:56,318 the types let's save this and now let's 10813 08:40:52,318 --> 08:40:59,280 just start using the type and I guess 10814 08:40:56,318 --> 08:41:02,520 right away also the tour schema so here 10815 08:40:59,280 --> 08:41:04,638 where I have the hug state for tours I 10816 08:41:02,520 --> 08:41:06,238 actually want to go with tour and the 10817 08:41:04,638 --> 08:41:10,238 array so essentially I'm telling 10818 08:41:06,238 --> 08:41:13,040 typescript that yeah this is an array 10819 08:41:10,238 --> 08:41:16,160 but eventually it will be an array off 10820 08:41:13,040 --> 08:41:18,798 TOS then we're going to keep on moving 10821 08:41:16,160 --> 08:41:21,318 and where we have the raw data same deal 10822 08:41:18,798 --> 08:41:25,840 I'm going to go a tour and I'll set it 10823 08:41:21,318 --> 08:41:28,680 as an array then we actually want to run 10824 08:41:25,840 --> 08:41:31,638 right now our tour schema remember there 10825 08:41:28,680 --> 08:41:33,760 is an array method and again I want to 10826 08:41:31,638 --> 08:41:36,718 save pars it so essentially I want to 10827 08:41:33,760 --> 08:41:40,040 throw the a myself and it's going to be 10828 08:41:36,718 --> 08:41:41,718 nicely caught over here so for now I 10829 08:41:40,040 --> 08:41:44,360 think I can remove both of these ones 10830 08:41:41,718 --> 08:41:46,718 over here we won't use them and 10831 08:41:44,360 --> 08:41:50,638 essentially we want to get that result 10832 08:41:46,718 --> 08:41:53,398 so let's go to the result then T schema 10833 08:41:50,638 --> 08:41:55,478 array let's invoke this and then we're 10834 08:41:53,398 --> 08:41:58,238 going to go save bars and and we're 10835 08:41:55,478 --> 08:42:01,040 looking for raw data and then as far as 10836 08:41:58,238 --> 08:42:02,680 the result remember we're checking for 10837 08:42:01,040 --> 08:42:06,280 the success 10838 08:42:02,680 --> 08:42:08,520 property and essentially if the data is 10839 08:42:06,280 --> 08:42:10,958 not present then we're just going to 10840 08:42:08,520 --> 08:42:12,280 throw the error so we're going to go if 10841 08:42:10,958 --> 08:42:13,878 and 10842 08:42:12,280 --> 08:42:17,558 result 10843 08:42:13,878 --> 08:42:19,520 success that's not the case then I mean 10844 08:42:17,558 --> 08:42:20,718 you know what I can actually log it so 10845 08:42:19,520 --> 08:42:25,478 let's go the 10846 08:42:20,718 --> 08:42:28,478 result error and message over here and 10847 08:42:25,478 --> 08:42:31,920 also we want to throw our own error so 10848 08:42:28,478 --> 08:42:34,718 let's go throw new error and again let's 10849 08:42:31,920 --> 08:42:35,840 do the same thing we'll go with failed 10850 08:42:34,718 --> 08:42:38,600 to 10851 08:42:35,840 --> 08:42:41,000 parse and tours and then at the very 10852 08:42:38,600 --> 08:42:44,360 very end we want to go here with set 10853 08:42:41,000 --> 08:42:48,080 tours and we want to pass in result. 10854 08:42:44,360 --> 08:42:50,558 data so if we're successful if we bypass 10855 08:42:48,080 --> 08:42:54,000 this condition pretty much we're good to 10856 08:42:50,558 --> 08:42:56,398 go and I think at this point essentially 10857 08:42:54,000 --> 08:43:00,840 we can just navigate to to our return 10858 08:42:56,398 --> 08:43:03,920 and we can just set up the proper return 10859 08:43:00,840 --> 08:43:06,080 so in here let's go with the heading two 10860 08:43:03,920 --> 08:43:07,520 I'm going to add some class so I'm going 10861 08:43:06,080 --> 08:43:10,638 to go with margin bottom and set it 10862 08:43:07,520 --> 08:43:14,840 equal to one then we're going to go here 10863 08:43:10,638 --> 08:43:17,520 with doors okay and then let's iterate 10864 08:43:14,840 --> 08:43:20,798 over so we have tours over here we want 10865 08:43:17,520 --> 08:43:25,120 to call the map and now once I type the 10866 08:43:20,798 --> 08:43:27,080 tour it will nicely be my object 10867 08:43:25,120 --> 08:43:30,520 everything starting with ID all the way 10868 08:43:27,080 --> 08:43:32,360 to a price and again this is something 10869 08:43:30,520 --> 08:43:34,638 that doesn't stop to amaze me so we're 10870 08:43:32,360 --> 08:43:36,958 going to go over here with return then 10871 08:43:34,638 --> 08:43:40,080 we want to set up the paragraph again 10872 08:43:36,958 --> 08:43:42,520 we're not going to really work on 10873 08:43:40,080 --> 08:43:44,558 Amazing result in the browser for now we 10874 08:43:42,520 --> 08:43:47,120 just want to grab the T ID we want to 10875 08:43:44,558 --> 08:43:48,958 add some classes over here so class name 10876 08:43:47,120 --> 08:43:53,638 and let's go with another margin bottom 10877 08:43:48,958 --> 08:43:55,798 one and then let's access the door Dot 10878 08:43:53,638 --> 08:43:57,280 and we're looking for the name and again 10879 08:43:55,798 --> 08:44:00,558 if everything is correct this is what we 10880 08:43:57,280 --> 08:44:05,840 should see in the browser yep everything 10881 08:44:00,558 --> 08:44:08,958 is awesome and if we go over here to the 10882 08:44:05,840 --> 08:44:10,558 types and let's say I'm going to add a 10883 08:44:08,958 --> 08:44:13,160 property I'm going to say that I'm 10884 08:44:10,558 --> 08:44:15,798 looking for something and that will be a 10885 08:44:13,160 --> 08:44:19,798 string and let's invoke that check it 10886 08:44:15,798 --> 08:44:21,840 out first I have over here a error 10887 08:44:19,798 --> 08:44:24,718 message in the console and we can also 10888 08:44:21,840 --> 08:44:27,798 see that over here now million doll 10889 08:44:24,718 --> 08:44:30,080 question do you always need to add Zod 10890 08:44:27,798 --> 08:44:32,080 when you're fetching data and to tell 10891 08:44:30,080 --> 08:44:33,440 honestly most likely that's not 10892 08:44:32,080 --> 08:44:35,280 something that you're going to see that 10893 08:44:33,440 --> 08:44:37,040 often for the most part you're just 10894 08:44:35,280 --> 08:44:40,120 going to rely on the fact that yeah the 10895 08:44:37,040 --> 08:44:43,120 API is supposed to return some kind of 10896 08:44:40,120 --> 08:44:45,600 type but it does come in handy when you 10897 08:44:43,120 --> 08:44:48,280 definitely definitely want to check for 10898 08:44:45,600 --> 08:44:51,798 that particular value so this is how we 10899 08:44:48,280 --> 08:44:54,040 can essentially Fetch with react and 10900 08:44:51,798 --> 08:44:57,080 typescript with the good old use effect 10901 08:44:54,040 --> 08:45:00,080 and fetch API and now let's see how we 10902 08:44:57,080 --> 08:45:02,558 can accomplish the same thing with less 10903 08:45:00,080 --> 08:45:04,680 lines of code and essentially no 10904 08:45:02,558 --> 08:45:08,440 headache and we're going to do that with 10905 08:45:04,680 --> 08:45:10,958 the help of axios and react query and if 10906 08:45:08,440 --> 08:45:13,360 you're not familiar with either of these 10907 08:45:10,958 --> 08:45:16,760 libraries please utilize the library 10908 08:45:13,360 --> 08:45:19,318 docs or some other online resource since 10909 08:45:16,760 --> 08:45:21,920 our main focus is going to be on 10910 08:45:19,318 --> 08:45:25,638 typescript and I actually want to start 10911 08:45:21,920 --> 08:45:29,120 by constructing fetch tours with 10912 08:45:25,638 --> 08:45:32,600 axios again normally you would set this 10913 08:45:29,120 --> 08:45:34,360 up in a separate file but in this case 10914 08:45:32,600 --> 08:45:37,558 I'm just going to do it right here in 10915 08:45:34,360 --> 08:45:39,238 the types where I want to install or you 10916 08:45:37,558 --> 08:45:41,280 know what actually it's already 10917 08:45:39,238 --> 08:45:45,760 available in the starter we simply want 10918 08:45:41,280 --> 08:45:50,000 to import the axios from axio library 10919 08:45:45,760 --> 08:45:52,040 which is an awesome HTTP library and 10920 08:45:50,000 --> 08:45:54,600 effectively we want to construct right 10921 08:45:52,040 --> 08:45:56,000 now the fetch doors and you know what I 10922 08:45:54,600 --> 08:45:58,760 also so I'm going to grab right away 10923 08:45:56,000 --> 08:46:01,120 this URL and I'll actually remove it 10924 08:45:58,760 --> 08:46:04,318 from the file since we'll set up use 10925 08:46:01,120 --> 08:46:06,478 Query over here as a side note as always 10926 08:46:04,318 --> 08:46:09,718 you can find 10927 08:46:06,478 --> 08:46:11,718 the code from previous lessons in the 10928 08:46:09,718 --> 08:46:14,440 read me so let me go over here this is 10929 08:46:11,718 --> 08:46:20,120 going to be my URL and essentially we 10930 08:46:14,440 --> 08:46:23,520 want to go with export const fetch doors 10931 08:46:20,120 --> 08:46:25,760 it will be a sync and as far as what 10932 08:46:23,520 --> 08:46:28,478 we're returning since it's a sync 10933 08:46:25,760 --> 08:46:32,398 function we want to go with promise and 10934 08:46:28,478 --> 08:46:34,360 then we'll set up our tour array and 10935 08:46:32,398 --> 08:46:37,160 then when it comes to functionality we 10936 08:46:34,360 --> 08:46:39,478 want to use axos and we want to stick a 10937 08:46:37,160 --> 08:46:41,398 weight in front of it so let's go with 10938 08:46:39,478 --> 08:46:44,718 const 10939 08:46:41,398 --> 08:46:48,878 response and that is equal to a 10940 08:46:44,718 --> 08:46:52,080 wait then axos doget remember this is 10941 08:46:48,878 --> 08:46:56,238 actually a generic and therefore in here 10942 08:46:52,080 --> 08:46:59,360 we pass in door and array so an array of 10943 08:46:56,238 --> 08:47:02,680 doors and effectively in this response 10944 08:46:59,360 --> 08:47:06,478 inside of the data property we'll get 10945 08:47:02,680 --> 08:47:10,718 back our array but it will be nicely 10946 08:47:06,478 --> 08:47:12,840 already set as type array off doors 10947 08:47:10,718 --> 08:47:16,920 hopefully that is clear and we actually 10948 08:47:12,840 --> 08:47:18,520 did cover that during the tutorial part 10949 08:47:16,920 --> 08:47:22,398 now let me double check over here it 10950 08:47:18,520 --> 08:47:24,440 looks like I have a syntax bug yep now 10951 08:47:22,398 --> 08:47:25,520 everything's correct so we have response 10952 08:47:24,440 --> 08:47:28,840 beautiful 10953 08:47:25,520 --> 08:47:31,200 then we'll do the same thing like in the 10954 08:47:28,840 --> 08:47:34,840 previous video where essentially I just 10955 08:47:31,200 --> 08:47:37,000 want to check using Zod and since it's 10956 08:47:34,840 --> 08:47:40,160 pretty much the same code let me again 10957 08:47:37,000 --> 08:47:42,958 just take it out and set it up over here 10958 08:47:40,160 --> 08:47:45,200 so we still use the result to or schema 10959 08:47:42,958 --> 08:47:48,520 in this case we're not passing a raw 10960 08:47:45,200 --> 08:47:50,760 data we actually go with response. data 10961 08:47:48,520 --> 08:47:53,680 again that's where the array is going to 10962 08:47:50,760 --> 08:47:56,318 be located and then from this function 10963 08:47:53,680 --> 08:48:00,360 we want to return 10964 08:47:56,318 --> 08:48:02,238 and we're looking for result. data so 10965 08:48:00,360 --> 08:48:04,718 since we have multiple libraries yes 10966 08:48:02,238 --> 08:48:07,440 we're pretty much passing this array 10967 08:48:04,718 --> 08:48:10,318 from one to another so first we use 10968 08:48:07,440 --> 08:48:13,000 axios and again just to demonstrate that 10969 08:48:10,318 --> 08:48:15,398 basically if we go over here with type 10970 08:48:13,000 --> 08:48:19,120 definition you'll notice somewhere here 10971 08:48:15,398 --> 08:48:21,120 get see over here it's a generic and 10972 08:48:19,120 --> 08:48:23,558 essentially it has this axio response 10973 08:48:21,120 --> 08:48:25,478 and then if you take a look at the xos 10974 08:48:23,558 --> 08:48:27,878 response one 10975 08:48:25,478 --> 08:48:30,360 notice this data it's a generic type 10976 08:48:27,878 --> 08:48:33,080 correct so whatever we pass in this is 10977 08:48:30,360 --> 08:48:34,958 what we're going to get back and once we 10978 08:48:33,080 --> 08:48:38,280 have this function in place now let's 10979 08:48:34,958 --> 08:48:41,398 navigate I guess actually to the main 10980 08:48:38,280 --> 08:48:44,478 DSX because we want to wrap everything 10981 08:48:41,398 --> 08:48:47,478 in the query client provider so we want 10982 08:48:44,478 --> 08:48:50,398 to install react query Library you can 10983 08:48:47,478 --> 08:48:52,160 also install Dev tools they have as some 10984 08:48:50,398 --> 08:48:54,920 Dev tools but in this case we're not 10985 08:48:52,160 --> 08:48:58,120 going to do that and in here we want 10986 08:48:54,920 --> 08:49:01,040 import the query client as well as query 10987 08:48:58,120 --> 08:49:02,600 client provider first we want to set up 10988 08:49:01,040 --> 08:49:04,718 a new 10989 08:49:02,600 --> 08:49:07,238 instance like 10990 08:49:04,718 --> 08:49:09,840 so and as a sign not something I want to 10991 08:49:07,238 --> 08:49:13,760 mention react query is actually very 10992 08:49:09,840 --> 08:49:16,040 good at inferring the types so as far as 10993 08:49:13,760 --> 08:49:19,160 types is concerned we actually don't 10994 08:49:16,040 --> 08:49:21,760 need to do much as long as we set up a 10995 08:49:19,160 --> 08:49:23,760 proper return type here in the function 10996 08:49:21,760 --> 08:49:25,160 basically what we are returning pretty 10997 08:49:23,760 --> 08:49:27,958 much we're good to go 10998 08:49:25,160 --> 08:49:30,878 so in here we want to go with new query 10999 08:49:27,958 --> 08:49:34,398 client let's invoke this and then we 11000 08:49:30,878 --> 08:49:37,360 want to wrap our app inside of the 11001 08:49:34,398 --> 08:49:40,440 render so let me set up my query client 11002 08:49:37,360 --> 08:49:43,440 provider we're looking for client that's 11003 08:49:40,440 --> 08:49:47,318 the prop and we'll set it equal to a 11004 08:49:43,440 --> 08:49:50,958 query client then let's wrap the app and 11005 08:49:47,318 --> 08:49:53,000 now let's navigate to index DSX and like 11006 08:49:50,958 --> 08:49:55,680 I said I'll pretty much start everything 11007 08:49:53,000 --> 08:49:58,200 from scratch and if you want 11008 08:49:55,680 --> 08:50:00,878 the code from the previous lesson please 11009 08:49:58,200 --> 08:50:04,280 just utilize the read me in here we're 11010 08:50:00,878 --> 08:50:06,958 looking for fetch dos a function we just 11011 08:50:04,280 --> 08:50:09,600 set up it's actually located in the 11012 08:50:06,958 --> 08:50:12,878 types okay and we also want to grab this 11013 08:50:09,600 --> 08:50:16,558 hook which we get from react query 11014 08:50:12,878 --> 08:50:19,638 called EU query again this is coming 11015 08:50:16,558 --> 08:50:23,000 from and in this case we have this stand 11016 08:50:19,638 --> 08:50:26,878 stack and react query so we have both of 11017 08:50:23,000 --> 08:50:29,280 these Imports now let's set up our our 11018 08:50:26,878 --> 08:50:31,718 component and let's also set up the 11019 08:50:29,280 --> 08:50:34,798 export since I know that I'm going to 11020 08:50:31,718 --> 08:50:37,718 forget to do that let's export the 11021 08:50:34,798 --> 08:50:40,760 component as default and for starters 11022 08:50:37,718 --> 08:50:43,280 let's invoke use Query so we're going to 11023 08:50:40,760 --> 08:50:45,840 get bunch of the things back and we want 11024 08:50:43,280 --> 08:50:47,478 to set it equal to use Query so notice 11025 08:50:45,840 --> 08:50:50,238 we don't need to set up our loading 11026 08:50:47,478 --> 08:50:52,040 error or any of that stuff and I'm not 11027 08:50:50,238 --> 08:50:54,398 even talking about the use effect 11028 08:50:52,040 --> 08:50:56,238 essentially we just invoke the use Query 11029 08:50:54,398 --> 08:50:59,040 and and we're good to go it's looking 11030 08:50:56,238 --> 08:51:02,558 for two things it's looking for a query 11031 08:50:59,040 --> 08:51:05,200 key since we'll nicely be able to 11032 08:51:02,558 --> 08:51:07,120 invalidate those queries later something 11033 08:51:05,200 --> 08:51:09,638 we're not going to do but just telling 11034 08:51:07,120 --> 08:51:11,200 you it is an option so essentially you 11035 08:51:09,638 --> 08:51:13,600 just provide some kind of string over 11036 08:51:11,200 --> 08:51:16,958 here so now this 11037 08:51:13,600 --> 08:51:19,478 query we will reference with the name of 11038 08:51:16,958 --> 08:51:21,000 doors just in case we ever want to do 11039 08:51:19,478 --> 08:51:24,558 something with the query like for 11040 08:51:21,000 --> 08:51:26,920 example invalidate and also we want to 11041 08:51:24,558 --> 08:51:29,760 set up a query function now this query 11042 08:51:26,920 --> 08:51:32,040 function has one rule and one rule only 11043 08:51:29,760 --> 08:51:33,920 it needs to return a promise that's it 11044 08:51:32,040 --> 08:51:35,958 that's all we need to do now if we 11045 08:51:33,920 --> 08:51:38,718 navigate to types what is this function 11046 08:51:35,958 --> 08:51:41,600 doing it's a sync what does that return 11047 08:51:38,718 --> 08:51:43,878 it returns a promise and like I said 11048 08:51:41,600 --> 08:51:46,840 react quer is awesome at inferring the 11049 08:51:43,878 --> 08:51:49,318 type so if we set up properly over here 11050 08:51:46,840 --> 08:51:52,000 that I'm going to be returning an array 11051 08:51:49,318 --> 08:51:54,718 of Tours react query is going to figure 11052 08:51:52,000 --> 08:51:57,398 that out so in here since I'm not not 11053 08:51:54,718 --> 08:52:00,958 passing anything in Fetch dos any kind 11054 08:51:57,398 --> 08:52:03,120 of argument I simply can set up the 11055 08:52:00,958 --> 08:52:06,280 reference now what are we getting back 11056 08:52:03,120 --> 08:52:08,638 over here we get back is pending so this 11057 08:52:06,280 --> 08:52:11,120 is while we're loading we get back is 11058 08:52:08,638 --> 08:52:13,238 error if there is any we also get the 11059 08:52:11,120 --> 08:52:16,760 actual error object just in case we want 11060 08:52:13,238 --> 08:52:20,238 to get the message and just like axios 11061 08:52:16,760 --> 08:52:22,280 in here we have this data one and inside 11062 08:52:20,238 --> 08:52:24,718 of that data we'll have that array of 11063 08:52:22,280 --> 08:52:26,360 Tours now since I want to use pretty 11064 08:52:24,718 --> 08:52:30,360 much the same name I'm just going to 11065 08:52:26,360 --> 08:52:32,238 give it an alas of Tours okay good and 11066 08:52:30,360 --> 08:52:35,238 with this in place now let's set up our 11067 08:52:32,238 --> 08:52:38,360 return so one by one uh first we're 11068 08:52:35,238 --> 08:52:41,280 going to go with is spending so check 11069 08:52:38,360 --> 08:52:43,080 that one if that's the case again same 11070 08:52:41,280 --> 08:52:45,920 deal we're going to go with heading two 11071 08:52:43,080 --> 08:52:48,280 or heading three and we'll go with 11072 08:52:45,920 --> 08:52:51,360 loading then we're going to go with is 11073 08:52:48,280 --> 08:52:53,440 error so if we have the error actually I 11074 08:52:51,360 --> 08:52:56,920 do want to 11075 08:52:53,440 --> 08:52:59,478 access the error message so let's go 11076 08:52:56,920 --> 08:53:02,280 here with error and we'll set it equal 11077 08:52:59,478 --> 08:53:04,398 to error object and in there we have a 11078 08:53:02,280 --> 08:53:07,478 message property and now let's iterate 11079 08:53:04,398 --> 08:53:09,840 over the tours and same deal essentially 11080 08:53:07,478 --> 08:53:12,478 we just want to showcase the tour name 11081 08:53:09,840 --> 08:53:16,120 in some kind of paragraph so let me set 11082 08:53:12,478 --> 08:53:17,680 up the return we're looking for div 11083 08:53:16,120 --> 08:53:21,360 inside of the div we're going to go with 11084 08:53:17,680 --> 08:53:23,798 heading two let's add a class name 11085 08:53:21,360 --> 08:53:25,318 margin bottom set it equal to one that 11086 08:53:23,798 --> 08:53:28,238 will be equal to 11087 08:53:25,318 --> 08:53:29,680 tours and then we want to iterate over 11088 08:53:28,238 --> 08:53:33,600 so let's call 11089 08:53:29,680 --> 08:53:36,520 map then each and every tour okay good 11090 08:53:33,600 --> 08:53:38,318 and then we want to return from the 11091 08:53:36,520 --> 08:53:41,000 function and I'm going to go with my 11092 08:53:38,318 --> 08:53:43,520 paragraph let's add a class name of 11093 08:53:41,000 --> 08:53:46,440 margin bottom one and also let's add a 11094 08:53:43,520 --> 08:53:50,478 key and we'll set it equal to tour and 11095 08:53:46,440 --> 08:53:54,238 then ID and then inside of it let's 11096 08:53:50,478 --> 08:53:57,000 render the door. name and if everything 11097 08:53:54,238 --> 08:54:00,600 is correct again the result is going to 11098 08:53:57,000 --> 08:54:02,920 be the same but like I said way less 11099 08:54:00,600 --> 08:54:05,200 headache way less State 11100 08:54:02,920 --> 08:54:10,120 values and pretty much everything is 11101 08:54:05,200 --> 08:54:13,600 already inferred so we only need to set 11102 08:54:10,120 --> 08:54:15,760 up a use Query we only need to set up 11103 08:54:13,600 --> 08:54:18,680 fetch doors and pretty much we're good 11104 08:54:15,760 --> 08:54:21,318 to go okay and now let's cover Redux 11105 08:54:18,680 --> 08:54:25,160 toolkit setup which is a good option if 11106 08:54:21,318 --> 08:54:27,440 you have a global State as side note if 11107 08:54:25,160 --> 08:54:30,760 you're not a fan of Redux 11108 08:54:27,440 --> 08:54:34,040 toit a Nifty alternative is Library 11109 08:54:30,760 --> 08:54:36,398 called zustand I believe that's how it's 11110 08:54:34,040 --> 08:54:39,238 pronounced which offers pretty much the 11111 08:54:36,398 --> 08:54:42,200 same functionality but with less 11112 08:54:39,238 --> 08:54:45,080 overhand and the code is also less 11113 08:54:42,200 --> 08:54:46,958 clunky I have to admit but since at 11114 08:54:45,080 --> 08:54:50,840 least at this point in time you will 11115 08:54:46,958 --> 08:54:55,160 encounter Redux to K it more often I 11116 08:54:50,840 --> 08:54:59,200 settled on rtk as far as typescript 11117 08:54:55,160 --> 08:55:02,638 Redux toolkit just like react query also 11118 08:54:59,200 --> 08:55:05,000 nicely infers bunch of the things so 11119 08:55:02,638 --> 08:55:08,958 it's actually less work than for example 11120 08:55:05,000 --> 08:55:13,238 use reducer but I'm not going to lie the 11121 08:55:08,958 --> 08:55:15,558 setup so the initial setup to add Redux 11122 08:55:13,238 --> 08:55:19,360 toolkit to our application with 11123 08:55:15,558 --> 08:55:22,520 typescript is somewhat of a pain now the 11124 08:55:19,360 --> 08:55:25,080 good news it's a boilerplate setup so we 11125 08:55:22,520 --> 08:55:28,478 can take it from Project to project so 11126 08:55:25,080 --> 08:55:31,798 most likely you'll only need to write it 11127 08:55:28,478 --> 08:55:34,398 once and in fact we will do a setup in 11128 08:55:31,798 --> 08:55:36,120 the following video so for now we'll 11129 08:55:34,398 --> 08:55:38,958 just prepare everything we'll actually 11130 08:55:36,120 --> 08:55:41,520 create our first slice and then in the 11131 08:55:38,958 --> 08:55:44,680 following video we'll complete the 11132 08:55:41,520 --> 08:55:48,080 boiler plate setup for Nots we want to 11133 08:55:44,680 --> 08:55:50,840 go to app DSX we're looking for starter 11134 08:55:48,080 --> 08:55:52,360 rtk the component if everything is 11135 08:55:50,840 --> 08:55:55,760 correct this is what we should see on 11136 08:55:52,360 --> 08:55:59,920 the screen and since we're shooting for 11137 08:55:55,760 --> 08:56:03,478 the same functionality as in use reducer 11138 08:55:59,920 --> 08:56:05,920 I suggest navigating to read me and just 11139 08:56:03,478 --> 08:56:08,440 grab this code so again this is just 11140 08:56:05,920 --> 08:56:11,958 going to set up some buttons 11141 08:56:08,440 --> 08:56:14,160 which we will use later to control our 11142 08:56:11,958 --> 08:56:17,080 state values so grab 11143 08:56:14,160 --> 08:56:18,958 everything in here as far as the 11144 08:56:17,080 --> 08:56:20,680 component is concerned you want to 11145 08:56:18,958 --> 08:56:24,280 navigate back over 11146 08:56:20,680 --> 08:56:26,398 here paste it and again you should have 11147 08:56:24,280 --> 08:56:28,680 five buttons and pretty much every time 11148 08:56:26,398 --> 08:56:31,440 you click on a button there's going to 11149 08:56:28,680 --> 08:56:35,318 be some kind of console log and then we 11150 08:56:31,440 --> 08:56:37,798 want to set up a slice now slice in 11151 08:56:35,318 --> 08:56:40,558 Redux tokit essentially is just a piece 11152 08:56:37,798 --> 08:56:43,000 of functionality so let's say in your 11153 08:56:40,558 --> 08:56:45,360 application you're handling the user so 11154 08:56:43,000 --> 08:56:47,520 the log out login whatever all kinds of 11155 08:56:45,360 --> 08:56:50,638 things well you'll set that one up in a 11156 08:56:47,520 --> 08:56:52,718 slice let's say you have a theme you'll 11157 08:56:50,638 --> 08:56:54,558 create a slice for that and hopefully 11158 08:56:52,718 --> 08:56:56,360 you see where I'm going with this now in 11159 08:56:54,558 --> 08:57:00,000 our case we'll just dump everything in 11160 08:56:56,360 --> 08:57:03,520 one so I'll say cter slice and we're 11161 08:57:00,000 --> 08:57:06,638 looking for TS since we're not creating 11162 08:57:03,520 --> 08:57:10,000 a react component then we want to import 11163 08:57:06,638 --> 08:57:11,760 two things so create slice is going to 11164 08:57:10,000 --> 08:57:14,160 create that slice for us it's a function 11165 08:57:11,760 --> 08:57:16,520 coming from Redux toolkit and second one 11166 08:57:14,160 --> 08:57:18,398 will be payload action which actually is 11167 08:57:16,520 --> 08:57:21,040 a type and this is something we're going 11168 08:57:18,398 --> 08:57:24,840 to use towards the end of the video so 11169 08:57:21,040 --> 08:57:28,318 now let's go with create and Slash 11170 08:57:24,840 --> 08:57:32,920 okay that's what we want to import over 11171 08:57:28,318 --> 08:57:36,718 here good then let's also grab that 11172 08:57:32,920 --> 08:57:38,638 payload but this is actually a type so 11173 08:57:36,718 --> 08:57:41,638 payload action and yep it's coming from 11174 08:57:38,638 --> 08:57:44,638 Redux toket beautiful now we will set up 11175 08:57:41,638 --> 08:57:45,840 some types in a second let's start by 11176 08:57:44,638 --> 08:57:48,478 creating that 11177 08:57:45,840 --> 08:57:51,600 slice so counter 11178 08:57:48,478 --> 08:57:54,360 slice and this will be equal to create 11179 08:57:51,600 --> 08:57:57,520 slice and second will provide some 11180 08:57:54,360 --> 08:58:01,478 values before I forget I do right away 11181 08:57:57,520 --> 08:58:04,558 want to export and actually we export 11182 08:58:01,478 --> 08:58:07,718 the reducer function so we go here with 11183 08:58:04,558 --> 08:58:09,878 default counter slice and then dot 11184 08:58:07,718 --> 08:58:12,878 reducer so this is something we're going 11185 08:58:09,878 --> 08:58:15,600 to use when we set up the store so think 11186 08:58:12,878 --> 08:58:18,478 of store as pretty much your entire 11187 08:58:15,600 --> 08:58:20,920 State and then the slice well this will 11188 08:58:18,478 --> 08:58:22,760 be that one functionality again in our 11189 08:58:20,920 --> 08:58:25,840 case pretty much that will be the only 11190 08:58:22,760 --> 08:58:29,200 functionality but in bigger applications 11191 08:58:25,840 --> 08:58:31,440 this is where you'll set this one thing 11192 08:58:29,200 --> 08:58:33,760 and as far as the crate slice it's 11193 08:58:31,440 --> 08:58:36,318 looking for a few things it needs to 11194 08:58:33,760 --> 08:58:38,718 have the initial State and we'll also 11195 08:58:36,318 --> 08:58:40,958 right away set up those reducers so you 11196 08:58:38,718 --> 08:58:43,360 know what let's just start setting 11197 08:58:40,958 --> 08:58:47,160 everything up if you remember when it 11198 08:58:43,360 --> 08:58:50,040 comes to the state we have count and 11199 08:58:47,160 --> 08:58:52,440 Status over there and then it will be 11200 08:58:50,040 --> 08:58:54,120 our initial State and this is something 11201 08:58:52,440 --> 08:58:56,840 that we're going to provide over here 11202 08:58:54,120 --> 08:58:58,798 here in the create slice so let's start 11203 08:58:56,840 --> 08:59:02,558 here const 11204 08:58:58,798 --> 08:59:04,920 initial state for now I'm not going to 11205 08:59:02,558 --> 08:59:07,958 set the type I'm just going to say that 11206 08:59:04,920 --> 08:59:10,878 count is equal to zero and then status 11207 08:59:07,958 --> 08:59:14,000 will be pending and remember we'll just 11208 08:59:10,878 --> 08:59:17,440 control it from active to inactive I 11209 08:59:14,000 --> 08:59:20,040 believe then inside of the create slice 11210 08:59:17,440 --> 08:59:21,478 we want to come up with the name and 11211 08:59:20,040 --> 08:59:24,440 essentially I'm going to call this 11212 08:59:21,478 --> 08:59:27,238 counter and then also we want to go with 11213 08:59:24,440 --> 08:59:29,840 our initial state in a second we'll set 11214 08:59:27,238 --> 08:59:32,040 up the reducers which control it but 11215 08:59:29,840 --> 08:59:34,718 before we do that well why don't we just 11216 08:59:32,040 --> 08:59:36,958 knock out those types over here so first 11217 08:59:34,718 --> 08:59:41,920 let's set up a type for the counter 11218 08:59:36,958 --> 08:59:44,600 state so type counter and state and in 11219 08:59:41,920 --> 08:59:47,360 here count will be equal to a number and 11220 08:59:44,600 --> 08:59:50,718 then when it comes to status I'll 11221 08:59:47,360 --> 08:59:54,718 actually set up the literal type because 11222 08:59:50,718 --> 08:59:56,958 we'll use it later to provide a proper 11223 08:59:54,718 --> 08:59:59,440 type to payload action so we might as 11224 08:59:56,958 --> 09:00:02,680 well set it up separately so let's set 11225 08:59:59,440 --> 09:00:04,958 up a status but instead of setting up as 11226 09:00:02,680 --> 09:00:08,638 a string I'll actually create another 11227 09:00:04,958 --> 09:00:10,878 type I'll call this counter status so 11228 09:00:08,638 --> 09:00:13,600 this is essentially something we control 11229 09:00:10,878 --> 09:00:15,878 when we add that payload over here now 11230 09:00:13,600 --> 09:00:17,520 when it comes to a counter we'll press 11231 09:00:15,878 --> 09:00:20,840 these buttons over here without the 11232 09:00:17,520 --> 09:00:23,360 payload and as far as the literal values 11233 09:00:20,840 --> 09:00:24,920 well we're going to go over here with 11234 09:00:23,360 --> 09:00:27,718 active 11235 09:00:24,920 --> 09:00:30,318 inactive and let's also add the bending 11236 09:00:27,718 --> 09:00:32,798 one and once I have this one in place 11237 09:00:30,318 --> 09:00:35,558 status actually is going to be equal to 11238 09:00:32,798 --> 09:00:39,160 a counter status so now where I have the 11239 09:00:35,558 --> 09:00:43,160 initial state I'm going to go with my 11240 09:00:39,160 --> 09:00:46,280 counter State and then we want to set up 11241 09:00:43,160 --> 09:00:49,200 those reducers so reducers are going to 11242 09:00:46,280 --> 09:00:52,558 be functions which essentially control D 11243 09:00:49,200 --> 09:00:54,238 State and then in order to invoke those 11244 09:00:52,558 --> 09:00:57,160 functions we'll need to display patch 11245 09:00:54,238 --> 09:01:00,920 them and again remember when it comes to 11246 09:00:57,160 --> 09:01:03,080 actual count the number the only thing 11247 09:01:00,920 --> 09:01:06,160 we need is whether we want to increase 11248 09:01:03,080 --> 09:01:08,040 decrease or reset but when it comes to 11249 09:01:06,160 --> 09:01:10,558 set status this is where we actually 11250 09:01:08,040 --> 09:01:13,120 pass in the payload now the good news 11251 09:01:10,558 --> 09:01:15,238 with Redux toit effectively if we're 11252 09:01:13,120 --> 09:01:17,200 just controlling this one value we don't 11253 09:01:15,238 --> 09:01:19,160 even need to provide D type and you'll 11254 09:01:17,200 --> 09:01:21,600 see what I'm talking about in a second 11255 09:01:19,160 --> 09:01:24,798 so we want to go with reducers it's an 11256 09:01:21,600 --> 09:01:26,200 object and then one by one let's up and 11257 09:01:24,798 --> 09:01:28,160 pretty much I just come up with a 11258 09:01:26,200 --> 09:01:30,600 function name so for example in this 11259 09:01:28,160 --> 09:01:34,000 case increment I right away have access 11260 09:01:30,600 --> 09:01:37,000 to a state and as side not yes redex to 11261 09:01:34,000 --> 09:01:39,478 it nicely inverse all of the values that 11262 09:01:37,000 --> 09:01:41,558 we need over here and then yes we can 11263 09:01:39,478 --> 09:01:45,080 essentially just overwrite so I can just 11264 09:01:41,558 --> 09:01:48,360 go with count plus and equals to one so 11265 09:01:45,080 --> 09:01:50,040 notice I don't need to provide my type 11266 09:01:48,360 --> 09:01:52,160 since I'm not providing any payload 11267 09:01:50,040 --> 09:01:54,680 that's it I only want to set up this 11268 09:01:52,160 --> 09:01:56,718 function over here and we will export it 11269 09:01:54,680 --> 09:01:59,520 in a second and eventually we will 11270 09:01:56,718 --> 09:02:02,680 dispatch it hopefully that is clear then 11271 09:01:59,520 --> 09:02:06,360 we want to go with uh decrement I 11272 09:02:02,680 --> 09:02:09,878 guess let's go with State same 11273 09:02:06,360 --> 09:02:13,318 deal we want to go with State count and 11274 09:02:09,878 --> 09:02:15,318 in this case we'll subtract and then 11275 09:02:13,318 --> 09:02:18,360 when it comes to 11276 09:02:15,318 --> 09:02:22,160 reset we just want to access the state 11277 09:02:18,360 --> 09:02:24,398 okay good and then set the state count 11278 09:02:22,160 --> 09:02:27,840 equals to zero as then note if you're 11279 09:02:24,398 --> 09:02:29,680 wondering why we can just modify the 11280 09:02:27,840 --> 09:02:32,000 state like that why don't we need to 11281 09:02:29,680 --> 09:02:34,398 return state from this function it's 11282 09:02:32,000 --> 09:02:37,600 because under the hood essentially Redux 11283 09:02:34,398 --> 09:02:40,718 is using a library which does that for 11284 09:02:37,600 --> 09:02:43,478 us so that's why we can actually kind of 11285 09:02:40,718 --> 09:02:45,160 modify the state directly in here under 11286 09:02:43,478 --> 09:02:47,520 the hood yeah we're still getting the 11287 09:02:45,160 --> 09:02:50,520 latest value don't worry and then we 11288 09:02:47,520 --> 09:02:52,760 have the St status which essentially is 11289 09:02:50,520 --> 09:02:55,520 going to have two things not 11290 09:02:52,760 --> 09:02:57,080 only we'll need access to a state but 11291 09:02:55,520 --> 09:02:59,360 this is the case where we will provide 11292 09:02:57,080 --> 09:03:02,520 eay load so effectively when we dispatch 11293 09:02:59,360 --> 09:03:05,398 set status we will provide that string 11294 09:03:02,520 --> 09:03:07,798 value and the string value is actually 11295 09:03:05,398 --> 09:03:10,798 one of these ones so either active 11296 09:03:07,798 --> 09:03:14,360 inactive or it's going to be pending and 11297 09:03:10,798 --> 09:03:16,520 when we use a Redux stol kit we do have 11298 09:03:14,360 --> 09:03:19,440 this special type which we need to use 11299 09:03:16,520 --> 09:03:23,958 so it's called payload and for some 11300 09:03:19,440 --> 09:03:28,440 reason not using so payload action not 11301 09:03:23,958 --> 09:03:30,120 sure why I'm not able to access that so 11302 09:03:28,440 --> 09:03:34,600 payload 11303 09:03:30,120 --> 09:03:39,238 action and it is a generic over 11304 09:03:34,600 --> 09:03:41,920 here and then inside of the generic we 11305 09:03:39,238 --> 09:03:43,958 want to pass in our own type so 11306 09:03:41,920 --> 09:03:46,318 basically we need to provide over here 11307 09:03:43,958 --> 09:03:48,238 this counter State now I do have over 11308 09:03:46,318 --> 09:03:51,360 here a syntax error and now as you can 11309 09:03:48,238 --> 09:03:53,360 see everything works so if you have the 11310 09:03:51,360 --> 09:03:58,280 action yes you'll need to provide this 11311 09:03:53,360 --> 09:04:00,600 payload action which again is a generic 11312 09:03:58,280 --> 09:04:01,920 and then in here you'll provide the type 11313 09:04:00,600 --> 09:04:03,638 so in this case it's somewhat 11314 09:04:01,920 --> 09:04:06,478 straightforward we just have a string 11315 09:04:03,638 --> 09:04:08,200 but keep in mind yes if you have let's 11316 09:04:06,478 --> 09:04:10,840 say an object with a bunch of properties 11317 09:04:08,200 --> 09:04:14,040 you'll have to set up the type and 11318 09:04:10,840 --> 09:04:15,520 provide it over here so typescript knows 11319 09:04:14,040 --> 09:04:18,478 what do you want to do essentially when 11320 09:04:15,520 --> 09:04:20,878 you pass in that payload and same deal 11321 09:04:18,478 --> 09:04:23,160 we want to go with state. status and 11322 09:04:20,878 --> 09:04:25,840 that will be equal to action payload 11323 09:04:23,160 --> 09:04:27,920 since that's where we have the string 11324 09:04:25,840 --> 09:04:30,238 now it looks like I have a bug my 11325 09:04:27,920 --> 09:04:32,840 apologies by mistake I provided the 11326 09:04:30,238 --> 09:04:35,878 counter state it should be 11327 09:04:32,840 --> 09:04:37,398 counter status my apologies now 11328 09:04:35,878 --> 09:04:41,638 everything's correct I don't have any 11329 09:04:37,398 --> 09:04:44,558 bugs and also we want to export the 11330 09:04:41,638 --> 09:04:48,000 increment decrement all the way to set 11331 09:04:44,558 --> 09:04:50,440 status so we won't need to provide in 11332 09:04:48,000 --> 09:04:53,238 our component let's say a type when we 11333 09:04:50,440 --> 09:04:55,680 dispatch we simply can pass in our 11334 09:04:53,238 --> 09:04:58,638 action and we'll be good to go so we go 11335 09:04:55,680 --> 09:05:00,958 here with export const and then we 11336 09:04:58,638 --> 09:05:04,718 should have access to all of our 11337 09:05:00,958 --> 09:05:07,318 functions so increment decrement reset 11338 09:05:04,718 --> 09:05:10,318 and then set status and that one will be 11339 09:05:07,318 --> 09:05:13,478 equal to counter slice and then we're 11340 09:05:10,318 --> 09:05:15,798 looking for actions this should do it 11341 09:05:13,478 --> 09:05:19,440 for the slice setup and in the following 11342 09:05:15,798 --> 09:05:22,398 video we'll do that somewhat annoying 11343 09:05:19,440 --> 09:05:25,760 boiler plate setup where we'll add the 11344 09:05:22,398 --> 09:05:26,920 Redux tokit to our application okay and 11345 09:05:25,760 --> 09:05:29,520 now let's take a look at that 11346 09:05:26,920 --> 09:05:31,160 boilerplate setup where effectively we 11347 09:05:29,520 --> 09:05:35,520 want to create two files we want to 11348 09:05:31,160 --> 09:05:37,360 create store DS as well as the Haw DS 11349 09:05:35,520 --> 09:05:40,200 and as a side not if you're using the 11350 09:05:37,360 --> 09:05:43,398 starter you should have already those 11351 09:05:40,200 --> 09:05:45,878 files now there shouldn't be any code in 11352 09:05:43,398 --> 09:05:48,280 them but you should have the files and 11353 09:05:45,878 --> 09:05:51,160 store is where we set up all our slices 11354 09:05:48,280 --> 09:05:52,840 so we create that Global store and hooks 11355 09:05:51,160 --> 09:05:55,120 is a special one because we're using 11356 09:05:52,840 --> 09:05:57,840 typescript and you'll see what we need 11357 09:05:55,120 --> 09:05:59,600 to do over there in a second so first 11358 09:05:57,840 --> 09:06:02,478 let's start with the store we want to 11359 09:05:59,600 --> 09:06:05,680 import configure store this is coming 11360 09:06:02,478 --> 09:06:08,078 from Redux tokit and also we want to 11361 09:06:05,680 --> 09:06:11,878 grab counter 11362 09:06:08,078 --> 09:06:15,278 reducer now this one is coming from our 11363 09:06:11,878 --> 09:06:16,640 slice the counter slice and I need to 11364 09:06:15,278 --> 09:06:19,200 make sure that it's coming from the 11365 09:06:16,640 --> 09:06:24,320 start so let me go over here with the 11366 09:06:19,200 --> 09:06:28,398 reducer from and let me look for 11367 09:06:24,320 --> 09:06:30,360 star then the rtk and then I want to get 11368 09:06:28,398 --> 09:06:33,200 the counter slice let me just double 11369 09:06:30,360 --> 09:06:36,000 check yep I'm exporting so everything is 11370 09:06:33,200 --> 09:06:38,360 correct and then back in a store we want 11371 09:06:36,000 --> 09:06:41,278 to invoke configure store so we want to 11372 09:06:38,360 --> 09:06:43,878 go here with export con store since 11373 09:06:41,278 --> 09:06:46,918 we'll wrap our entire application in the 11374 09:06:43,878 --> 09:06:50,598 store and inside of this function we 11375 09:06:46,918 --> 09:06:53,640 have a reducer property and essentially 11376 09:06:50,598 --> 09:06:55,960 this is where we set up our slices so in 11377 09:06:53,640 --> 09:06:58,238 our case again we have only one so I'm 11378 09:06:55,960 --> 09:07:00,800 going to go with counter is equal to 11379 09:06:58,238 --> 09:07:02,520 counter reducer since we're using 11380 09:07:00,800 --> 09:07:05,520 typescript we need to set up two more 11381 09:07:02,520 --> 09:07:08,480 types so export type and again in the 11382 09:07:05,520 --> 09:07:12,800 docs they use this root State they set 11383 09:07:08,480 --> 09:07:16,640 it equal to return type and inside of 11384 09:07:12,800 --> 09:07:19,840 this one they go type off and then store 11385 09:07:16,640 --> 09:07:22,878 and get state so this is what they pass 11386 09:07:19,840 --> 09:07:24,918 into this generic and the second type 11387 09:07:22,878 --> 09:07:29,320 when you want to set up over here is 11388 09:07:24,918 --> 09:07:32,278 export type app and dis 11389 09:07:29,320 --> 09:07:37,278 patch and this one will be equal to type 11390 09:07:32,278 --> 09:07:39,160 off and store this patch so once we have 11391 09:07:37,278 --> 09:07:42,040 these two types in place we want to 11392 09:07:39,160 --> 09:07:43,918 navigate to The Hook one and essentially 11393 09:07:42,040 --> 09:07:47,360 we want to grab use dispatch and use 11394 09:07:43,918 --> 09:07:51,000 selector so those are the two hooks we 11395 09:07:47,360 --> 09:07:53,238 use with Redux toit one is to access the 11396 09:07:51,000 --> 09:07:54,480 store and second one is to dispatch an 11397 09:07:53,238 --> 09:07:56,878 action 11398 09:07:54,480 --> 09:08:00,520 and we want to create our own and you'll 11399 09:07:56,878 --> 09:08:03,238 see what that entails in a second so 11400 09:08:00,520 --> 09:08:06,200 let's first grab use this patch again 11401 09:08:03,238 --> 09:08:08,640 this is coming from react Redux so this 11402 09:08:06,200 --> 09:08:10,840 is what I was talking about the annoying 11403 09:08:08,640 --> 09:08:14,040 boiler plate setup but again you only 11404 09:08:10,840 --> 09:08:16,840 need to realistically do it once so then 11405 09:08:14,040 --> 09:08:19,680 import this is actually a type and we're 11406 09:08:16,840 --> 09:08:22,960 looking for typed 11407 09:08:19,680 --> 09:08:26,758 use selector hook so this is coming from 11408 09:08:22,960 --> 09:08:29,160 react Redux and also we want to grab 11409 09:08:26,758 --> 09:08:31,960 that root State and app dispatch the 11410 09:08:29,160 --> 09:08:36,160 types we just set up so let's go here 11411 09:08:31,960 --> 09:08:38,558 with root and state somewhere yep that's 11412 09:08:36,160 --> 09:08:41,278 the one and I also want to grab the 11413 09:08:38,558 --> 09:08:43,800 dispatch okay we have both of these so 11414 09:08:41,278 --> 09:08:47,520 now let's set up both of those hooks so 11415 09:08:43,800 --> 09:08:50,520 we're looking for export const 11416 09:08:47,520 --> 09:08:54,040 use and the name is totally up to you 11417 09:08:50,520 --> 09:08:57,718 just make sure that it starts with use 11418 09:08:54,040 --> 09:09:00,800 that's just a rule for react talks and 11419 09:08:57,718 --> 09:09:03,438 in this case we'll actually go with the 11420 09:09:00,800 --> 09:09:07,040 function and we're going to go with app 11421 09:09:03,438 --> 09:09:09,438 dispatch and we'll set it equal to use 11422 09:09:07,040 --> 09:09:11,238 dispatch so it's going to suggest this 11423 09:09:09,438 --> 09:09:14,000 use app dispatch that's not what you 11424 09:09:11,238 --> 09:09:15,840 want to use you want to go with use 11425 09:09:14,000 --> 09:09:18,320 dispatch which is actually the second 11426 09:09:15,840 --> 09:09:21,360 one and then let's also set up the use 11427 09:09:18,320 --> 09:09:24,680 app selector which also we're going to 11428 09:09:21,360 --> 09:09:28,122 export so we go here with use 11429 09:09:24,680 --> 09:09:31,680 app selector and in this case we just go 11430 09:09:28,122 --> 09:09:34,078 with type selector hook we provide the 11431 09:09:31,680 --> 09:09:34,078 root 11432 09:09:34,122 --> 09:09:41,238 state or type that we have in a store 11433 09:09:37,122 --> 09:09:44,200 and we'll set it equal to use selector 11434 09:09:41,238 --> 09:09:48,360 here let's save it and now the last step 11435 09:09:44,200 --> 09:09:50,320 is going to be in the main DSX where we 11436 09:09:48,360 --> 09:09:53,800 want to import the entire 11437 09:09:50,320 --> 09:09:57,078 store from store file and and we want to 11438 09:09:53,800 --> 09:09:59,840 wrap it in the provider component which 11439 09:09:57,078 --> 09:10:01,480 is coming from react Redux library now 11440 09:09:59,840 --> 09:10:04,360 this provider component is looking for 11441 09:10:01,480 --> 09:10:07,558 store prop and as far as the value we'll 11442 09:10:04,360 --> 09:10:09,238 pass in our store so let's go here with 11443 09:10:07,558 --> 09:10:12,520 provider then 11444 09:10:09,238 --> 09:10:15,000 store set it equal to our 11445 09:10:12,520 --> 09:10:17,520 store then let's wrap it and with this 11446 09:10:15,000 --> 09:10:20,438 in place we have successfully added redu 11447 09:10:17,520 --> 09:10:23,000 toit to our application and in following 11448 09:10:20,438 --> 09:10:27,238 video we'll complete the functionality 11449 09:10:23,000 --> 09:10:31,598 okay so now let's navigate to 09 rtk 11450 09:10:27,238 --> 09:10:34,480 index DSX and we want to import few 11451 09:10:31,598 --> 09:10:38,078 things first we'll grab those hooks so 11452 09:10:34,480 --> 09:10:41,160 instead of use dispatch and use selector 11453 09:10:38,078 --> 09:10:43,878 we're going to go with use app selector 11454 09:10:41,160 --> 09:10:48,000 use app selector and we also want to 11455 09:10:43,878 --> 09:10:51,160 grab use app dispatch and then also 11456 09:10:48,000 --> 09:10:53,480 let's grab those actions from our slice 11457 09:10:51,160 --> 09:10:57,078 so I'm talking about decrement increment 11458 09:10:53,480 --> 09:11:00,398 reset and set status so let's grab also 11459 09:10:57,078 --> 09:11:03,680 this one this is coming from the counter 11460 09:11:00,398 --> 09:11:05,078 slice good then we have increment then 11461 09:11:03,680 --> 09:11:09,078 we have 11462 09:11:05,078 --> 09:11:10,878 reset and then we also have set status 11463 09:11:09,078 --> 09:11:14,598 all of that is coming from the counter 11464 09:11:10,878 --> 09:11:17,680 slice now in order to access the state 11465 09:11:14,598 --> 09:11:20,320 values we want to go with the use app 11466 09:11:17,680 --> 09:11:23,360 selector so that's the hook and 11467 09:11:20,320 --> 09:11:26,000 essentially by default it right away has 11468 09:11:23,360 --> 09:11:28,918 access to the entire state so I'm 11469 09:11:26,000 --> 09:11:30,878 talking about this one over here more 11470 09:11:28,918 --> 09:11:33,480 specifically we're looking for the 11471 09:11:30,878 --> 09:11:36,438 counter so we're going to go over here 11472 09:11:33,480 --> 09:11:39,078 and we're going to say State and as I 11473 09:11:36,438 --> 09:11:41,800 not I do need to pass here function and 11474 09:11:39,078 --> 09:11:44,558 then from the state we want to access 11475 09:11:41,800 --> 09:11:47,840 the counter so this will be our tiny 11476 09:11:44,558 --> 09:11:50,800 slice for the counter and we can nicely 11477 09:11:47,840 --> 09:11:52,840 right away the structure two things what 11478 09:11:50,800 --> 09:11:55,160 do we have in a state we have the count 11479 09:11:52,840 --> 09:11:57,398 as as well as the status so those are 11480 09:11:55,160 --> 09:11:59,320 the two things that I want to access so 11481 09:11:57,398 --> 09:12:02,078 now where I have the count I'll actually 11482 09:11:59,320 --> 09:12:05,160 change it around and I'll set it equal 11483 09:12:02,078 --> 09:12:08,398 to count and as far as the 11484 09:12:05,160 --> 09:12:11,040 status same deal over here now just to 11485 09:12:08,398 --> 09:12:13,840 keep it more interesting I'll actually 11486 09:12:11,040 --> 09:12:16,520 navigate back to my counter slice and 11487 09:12:13,840 --> 09:12:18,840 let's say set up my initial value to be 11488 09:12:16,520 --> 09:12:21,278 equal to 15 so now I can clearly see 11489 09:12:18,840 --> 09:12:24,238 that I have access to the counter as 11490 09:12:21,278 --> 09:12:27,078 well as my status one so now we just 11491 09:12:24,238 --> 09:12:29,758 want to navigate back and we also want 11492 09:12:27,078 --> 09:12:32,640 to invoke the use app dispatch which is 11493 09:12:29,758 --> 09:12:36,718 going to return a dispatch function we 11494 09:12:32,640 --> 09:12:40,800 can invoke so let's go here with cons 11495 09:12:36,718 --> 09:12:43,238 dispatch set it equal to use app 11496 09:12:40,800 --> 09:12:47,000 dispatch and then where we increment 11497 09:12:43,238 --> 09:12:48,878 decrement and reset we'll just invoke 11498 09:12:47,000 --> 09:12:51,758 these functions and then when it comes 11499 09:12:48,878 --> 09:12:55,000 to set status remember we also need to 11500 09:12:51,758 --> 09:12:57,200 pass in the payload so it's going to be 11501 09:12:55,000 --> 09:13:00,438 a little bit more code so we want to go 11502 09:12:57,200 --> 09:13:04,078 here with this patch and we just want to 11503 09:13:00,438 --> 09:13:05,840 invoke the increment same deal is going 11504 09:13:04,078 --> 09:13:08,480 to be for decrement and 11505 09:13:05,840 --> 09:13:12,122 reset so I'm pretty sure I can just copy 11506 09:13:08,480 --> 09:13:14,918 and paste so we're looking for decrement 11507 09:13:12,122 --> 09:13:18,122 over here and then lastly we have one 11508 09:13:14,918 --> 09:13:21,480 for reset as well so let me override 11509 09:13:18,122 --> 09:13:25,680 this one that should do it notice now 11510 09:13:21,480 --> 09:13:27,878 we're actually changing the count value 11511 09:13:25,680 --> 09:13:31,320 and when it comes to set 11512 09:13:27,878 --> 09:13:34,878 status it's pretty much the same we just 11513 09:13:31,320 --> 09:13:37,800 want to actually pass in the string 11514 09:13:34,878 --> 09:13:41,078 value whether that's active inactive or 11515 09:13:37,800 --> 09:13:44,160 pending so let's go here with dispatch 11516 09:13:41,078 --> 09:13:46,520 set status and again we'll get a nice 11517 09:13:44,160 --> 09:13:49,160 suggestion so these are the values that 11518 09:13:46,520 --> 09:13:53,960 we can pass in so it's kind of hard to 11519 09:13:49,160 --> 09:13:56,800 mess up so let me copy this set it up 11520 09:13:53,960 --> 09:14:00,278 over here and instead of active we're 11521 09:13:56,800 --> 09:14:03,558 going to pass in inactive so now let me 11522 09:14:00,278 --> 09:14:06,758 navigate quickly to browser and it looks 11523 09:14:03,558 --> 09:14:08,000 like everything is working as expected 11524 09:14:06,758 --> 09:14:12,000 and with this in place we have 11525 09:14:08,000 --> 09:14:14,758 successfully added Redux toolkit to our 11526 09:14:12,000 --> 09:14:17,918 application all right and up next let's 11527 09:14:14,758 --> 09:14:20,758 build a small tasks application before 11528 09:14:17,918 --> 09:14:23,438 we start coding away let me just mention 11529 09:14:20,758 --> 09:14:26,278 that if you enjoy the challenge and want 11530 09:14:23,438 --> 09:14:29,598 to build this project independently in 11531 09:14:26,278 --> 09:14:32,718 the readme you will find a list of steps 11532 09:14:29,598 --> 09:14:34,878 to build such app so if this is 11533 09:14:32,718 --> 09:14:38,160 something you're interested in pause the 11534 09:14:34,878 --> 09:14:40,122 video complete the steps independently 11535 09:14:38,160 --> 09:14:42,598 and resume once you're ready to compare 11536 09:14:40,122 --> 09:14:44,438 the results as far as the app it's 11537 09:14:42,598 --> 09:14:46,680 actually very similar to the app we 11538 09:14:44,438 --> 09:14:48,078 already built at the end of the 11539 09:14:46,680 --> 09:14:50,520 fundamental 11540 09:14:48,078 --> 09:14:54,122 section essentially we will create a 11541 09:14:50,520 --> 09:14:56,718 form and a list of tasks and if the user 11542 09:14:54,122 --> 09:14:58,840 provides a valid input we'll add a task 11543 09:14:56,718 --> 09:15:01,918 to the list and with the help of the 11544 09:14:58,840 --> 09:15:05,238 checkbox user will be able to toggle 11545 09:15:01,918 --> 09:15:08,040 whether the task is completed or not now 11546 09:15:05,238 --> 09:15:11,758 we'll start just like the other examples 11547 09:15:08,040 --> 09:15:13,960 where we want to navigate to app DSX and 11548 09:15:11,758 --> 09:15:16,918 we want to grab our components so in 11549 09:15:13,960 --> 09:15:20,758 this case we're looking 11550 09:15:16,918 --> 09:15:21,918 for folder number 10 I believe right so 11551 09:15:20,758 --> 09:15:23,758 we're looking 11552 09:15:21,918 --> 09:15:27,438 for 11553 09:15:23,758 --> 09:15:30,960 starter then forward slash and 11554 09:15:27,438 --> 09:15:31,758 tasks so let me replace whatever I have 11555 09:15:30,960 --> 09:15:34,718 in 11556 09:15:31,758 --> 09:15:38,680 here let me set up the component yep I 11557 09:15:34,718 --> 09:15:40,398 have task list so that is good and just 11558 09:15:38,680 --> 09:15:43,480 like before since we're working with 11559 09:15:40,398 --> 09:15:46,558 typescript one of the first things we'll 11560 09:15:43,480 --> 09:15:49,680 do is to set up a type and since we'll 11561 09:15:46,558 --> 09:15:53,558 use this type in multiple files it 11562 09:15:49,680 --> 09:15:56,360 definitely makes sense to set up types 11563 09:15:53,558 --> 09:15:59,960 yes and in this case there's going to be 11564 09:15:56,360 --> 09:16:03,598 only one type which will be a task so we 11565 09:15:59,960 --> 09:16:06,122 want to go with export type task and in 11566 09:16:03,598 --> 09:16:09,278 here we have three properties ID which 11567 09:16:06,122 --> 09:16:12,200 will be actually a string then a 11568 09:16:09,278 --> 09:16:15,680 description which will also be a string 11569 09:16:12,200 --> 09:16:18,398 and then is completed which will be a 11570 09:16:15,680 --> 09:16:20,278 Boolean let's set it up and then we want 11571 09:16:18,398 --> 09:16:24,238 to create two more components which will 11572 09:16:20,278 --> 09:16:29,320 render in the index DSX so so we have a 11573 09:16:24,238 --> 09:16:33,040 form component we set it up and also 11574 09:16:29,320 --> 09:16:35,758 there's going to be a list one new file 11575 09:16:33,040 --> 09:16:38,398 and we're looking for a list and then we 11576 09:16:35,758 --> 09:16:40,160 want to create our component and with 11577 09:16:38,398 --> 09:16:44,000 this in place now we want to navigate 11578 09:16:40,160 --> 09:16:46,598 back to index and we want to import use 11579 09:16:44,000 --> 09:16:50,320 effect use State as well as form and 11580 09:16:46,598 --> 09:16:53,122 list and also the type so one by one 11581 09:16:50,320 --> 09:16:57,800 let's set it up so use State then let's 11582 09:16:53,122 --> 09:16:59,040 also grab use effect and then let's grab 11583 09:16:57,800 --> 09:17:01,520 the form 11584 09:16:59,040 --> 09:17:04,160 one list 11585 09:17:01,520 --> 09:17:07,078 one and at the very end let's also grab 11586 09:17:04,160 --> 09:17:10,398 that type so import explicitly I'm going 11587 09:17:07,078 --> 09:17:13,238 to type task and then this is coming 11588 09:17:10,398 --> 09:17:16,320 from the types now inside of this 11589 09:17:13,238 --> 09:17:19,480 component essentially we want to start 11590 09:17:16,320 --> 09:17:24,438 by setting up the state value so in here 11591 09:17:19,480 --> 09:17:28,078 let's go with tasks set t tasks and this 11592 09:17:24,438 --> 09:17:30,438 one will be equal to use State let's 11593 09:17:28,078 --> 09:17:32,160 invoke this one and by default it's 11594 09:17:30,438 --> 09:17:35,122 going to be an empty array but we do 11595 09:17:32,160 --> 09:17:36,960 want to provide the generic over here 11596 09:17:35,122 --> 09:17:41,200 and we'll say that it's going to be an 11597 09:17:36,960 --> 09:17:44,040 array of tasks and when it comes to our 11598 09:17:41,200 --> 09:17:47,758 return I think I'm going to replace the 11599 09:17:44,040 --> 09:17:50,598 div with a section and in here let's 11600 09:17:47,758 --> 09:17:52,960 type the form component first and we 11601 09:17:50,598 --> 09:17:56,718 also want to display the list let's save 11602 09:17:52,960 --> 09:18:00,278 it over here now for some reason I don't 11603 09:17:56,718 --> 09:18:03,398 see anything looks like there's a bug so 11604 09:18:00,278 --> 09:18:04,758 it complains that Forum doesn't provide 11605 09:18:03,398 --> 09:18:08,238 the 11606 09:18:04,758 --> 09:18:10,238 import this is strange oh yeah probably 11607 09:18:08,238 --> 09:18:12,122 because I didn't save it my bad let me 11608 09:18:10,238 --> 09:18:13,718 try one more time if everything is 11609 09:18:12,122 --> 09:18:16,320 correct this is what we should see in a 11610 09:18:13,718 --> 09:18:19,160 browser and with this impl now we can 11611 09:18:16,320 --> 09:18:21,360 start working on the form functionality 11612 09:18:19,160 --> 09:18:23,160 okay and when it comes to form 11613 09:18:21,360 --> 09:18:25,160 essentially we want to set up the form 11614 09:18:23,160 --> 09:18:27,680 element inside of it there's going to be 11615 09:18:25,160 --> 09:18:30,000 an input and it's going to be a 11616 09:18:27,680 --> 09:18:32,598 controlled input so there will be a 11617 09:18:30,000 --> 09:18:35,360 state value as well and once we submit 11618 09:18:32,598 --> 09:18:37,840 the form we want to prevent the default 11619 09:18:35,360 --> 09:18:41,000 we want to check whether there is a 11620 09:18:37,840 --> 09:18:43,680 value provided in the input if not then 11621 09:18:41,000 --> 09:18:45,640 we will trigger the alert and if 11622 09:18:43,680 --> 09:18:49,040 everything is correct We'll add task to 11623 09:18:45,640 --> 09:18:52,800 the list something that's coming up and 11624 09:18:49,040 --> 09:18:54,640 also we'll set text back to empty value 11625 09:18:52,800 --> 09:18:57,320 I guess I'm going to start by setting up 11626 09:18:54,640 --> 09:19:02,122 two Imports over here so we're looking 11627 09:18:57,320 --> 09:19:04,480 for use State and also we want to grab 11628 09:19:02,122 --> 09:19:07,360 that type because eventually we will use 11629 09:19:04,480 --> 09:19:10,680 it not for now but let's go with type 11630 09:19:07,360 --> 09:19:13,438 and task which is coming from our types 11631 09:19:10,680 --> 09:19:16,520 and then let's start I guess by setting 11632 09:19:13,438 --> 09:19:19,840 up that state value so we have text and 11633 09:19:16,520 --> 09:19:22,120 set text and that one is equal to use 11634 09:19:19,840 --> 09:19:24,360 State and we want to start with empty 11635 09:19:22,120 --> 09:19:26,720 string eventually we'll set up the 11636 09:19:24,360 --> 09:19:29,960 handle submit for now I just want to 11637 09:19:26,720 --> 09:19:32,480 work on the return so we're going to 11638 09:19:29,960 --> 09:19:36,720 return a form element We'll add some 11639 09:19:32,480 --> 09:19:38,756 classes just so it looks a bit more 11640 09:19:36,720 --> 09:19:42,080 representable and we're going to go with 11641 09:19:38,756 --> 09:19:44,276 task form so again on submit is coming 11642 09:19:42,080 --> 09:19:45,960 up let's not worry about it right now 11643 09:19:44,276 --> 09:19:49,520 inside of the form we want to go with 11644 09:19:45,960 --> 09:19:54,120 input type will be equal to a text class 11645 09:19:49,520 --> 09:19:56,200 name is going to be form input 11646 09:19:54,120 --> 09:19:59,480 and also we want to set up the value 11647 09:19:56,200 --> 09:20:01,756 since this will be a controlled input 11648 09:19:59,480 --> 09:20:04,436 and let's also set up the 11649 09:20:01,756 --> 09:20:07,320 onchange I'm going to provide the inline 11650 09:20:04,436 --> 09:20:10,080 function since I don't want to bother 11651 09:20:07,320 --> 09:20:13,200 with the event type and we're just going 11652 09:20:10,080 --> 09:20:17,320 to go with set text and we'll go with 11653 09:20:13,200 --> 09:20:20,320 event. Target and value okay good and 11654 09:20:17,320 --> 09:20:25,276 then we also want to set up the submit 11655 09:20:20,320 --> 09:20:28,520 button so button then type is equal to 11656 09:20:25,276 --> 09:20:30,756 submit and class name will be equal to a 11657 09:20:28,520 --> 09:20:33,000 button and we're going to go with add 11658 09:20:30,756 --> 09:20:35,080 task let's save it this is what we 11659 09:20:33,000 --> 09:20:37,680 should see in the browser so now let's 11660 09:20:35,080 --> 09:20:40,720 just navigate back and set up that and 11661 09:20:37,680 --> 09:20:43,560 I'll submit so first let me I guess set 11662 09:20:40,720 --> 09:20:47,040 up the function this is going to be 11663 09:20:43,560 --> 09:20:48,596 essentially a repetition pretty much for 11664 09:20:47,040 --> 09:20:50,240 stuff that we already covered during 11665 09:20:48,596 --> 09:20:53,200 tutorial so we're going to go here with 11666 09:20:50,240 --> 09:20:55,480 handle submit remember we do want to 11667 09:20:53,200 --> 09:20:57,880 access the event but we do need to 11668 09:20:55,480 --> 09:20:59,916 provide the type ourselves in this case 11669 09:20:57,880 --> 09:21:04,000 so we're going to go with form event and 11670 09:20:59,916 --> 09:21:06,080 we're looking for HTML and form element 11671 09:21:04,000 --> 09:21:07,320 this is what we want to pass in and as 11672 09:21:06,080 --> 09:21:09,596 far as the 11673 09:21:07,320 --> 09:21:10,916 functionality first we're going to 11674 09:21:09,596 --> 09:21:13,800 prevent a 11675 09:21:10,916 --> 09:21:17,400 default so prevent default let's invoke 11676 09:21:13,800 --> 09:21:20,080 that then we'll check for text so if no 11677 09:21:17,400 --> 09:21:23,040 text provided then we'll go with alert 11678 09:21:20,080 --> 09:21:25,840 and let's just go with please enter a 11679 09:21:23,040 --> 09:21:27,640 task then we're going to return so we're 11680 09:21:25,840 --> 09:21:30,200 not going to do anything and if 11681 09:21:27,640 --> 09:21:32,520 everything is correct basically if we 11682 09:21:30,200 --> 09:21:34,800 bypass this condition then we're going 11683 09:21:32,520 --> 09:21:37,880 to go with that task and we'll go with 11684 09:21:34,800 --> 09:21:39,880 set text and we'll set it equal to an 11685 09:21:37,880 --> 09:21:42,120 empty string so now let's just look for 11686 09:21:39,880 --> 09:21:45,596 the form we're looking for 11687 09:21:42,120 --> 09:21:48,276 onsubmit and we'll set it equal to andle 11688 09:21:45,596 --> 09:21:51,960 submit so let's see let's see let's see 11689 09:21:48,276 --> 09:21:55,480 I think we can just log so instead of 11690 09:21:51,960 --> 09:21:57,560 adding the task for now let's just see 11691 09:21:55,480 --> 09:21:59,916 what is the text value whether we are 11692 09:21:57,560 --> 09:22:02,560 successful let me type some nonsense 11693 09:21:59,916 --> 09:22:05,680 here and looks like everything's correct 11694 09:22:02,560 --> 09:22:08,756 so I get the value in the console and I 11695 09:22:05,680 --> 09:22:11,276 also end up with an empty input now if 11696 09:22:08,756 --> 09:22:14,680 I'll try to submit without providing any 11697 09:22:11,276 --> 09:22:16,200 value then we'll get back the alert and 11698 09:22:14,680 --> 09:22:19,520 with this in place now we can start 11699 09:22:16,200 --> 09:22:21,240 working on the add task functionality 11700 09:22:19,520 --> 09:22:24,400 all right and up next let's set up 11701 09:22:21,240 --> 09:22:27,200 functionality to add task to the list 11702 09:22:24,400 --> 09:22:29,680 now we can take multiple approaches 11703 09:22:27,200 --> 09:22:32,916 essentially we can pass down set tasks 11704 09:22:29,680 --> 09:22:35,756 to a form and let's say if the value is 11705 09:22:32,916 --> 09:22:38,880 provided we can invoke set tasks it's 11706 09:22:35,756 --> 09:22:40,840 definitely an option but in my case it's 11707 09:22:38,880 --> 09:22:43,436 just a preference to set up everything 11708 09:22:40,840 --> 09:22:46,520 in the parent component and therefore I 11709 09:22:43,436 --> 09:22:49,756 will create another function and I'll 11710 09:22:46,520 --> 09:22:51,960 call this add task now this function is 11711 09:22:49,756 --> 09:22:55,120 looking for one thing and one thing only 11712 09:22:51,960 --> 09:22:57,640 which is a task but remember we have a 11713 09:22:55,120 --> 09:23:00,560 specific type for that so we want to go 11714 09:22:57,640 --> 09:23:02,800 over here with the task now it doesn't 11715 09:23:00,560 --> 09:23:05,800 return anything so we can just call here 11716 09:23:02,800 --> 09:23:08,400 void and as far as the functionality 11717 09:23:05,800 --> 09:23:11,800 inside of the function body we just want 11718 09:23:08,400 --> 09:23:14,880 to go with set tasks we want to spread 11719 09:23:11,800 --> 09:23:17,596 out all the existing tasks and then we 11720 09:23:14,880 --> 09:23:20,360 want to add the new one so we'll go over 11721 09:23:17,596 --> 09:23:23,080 here with tasks comma and then we will 11722 09:23:20,360 --> 09:23:26,400 add the task which was Prov divided to 11723 09:23:23,080 --> 09:23:30,120 add task we do want to pass this down so 11724 09:23:26,400 --> 09:23:32,200 add task is equal to add task and if 11725 09:23:30,120 --> 09:23:35,276 everything is correct we should get 11726 09:23:32,200 --> 09:23:38,960 bunch of red squiggly lines somewhere in 11727 09:23:35,276 --> 09:23:42,320 this case it's in the index DSX simply 11728 09:23:38,960 --> 09:23:45,040 because we haven't set up form to accept 11729 09:23:42,320 --> 09:23:47,840 any props so let's work on that we 11730 09:23:45,040 --> 09:23:51,200 already have D type task so now let's 11731 09:23:47,840 --> 09:23:55,000 set up our type here for the props I'm 11732 09:23:51,200 --> 09:23:58,480 going to call this for props and in here 11733 09:23:55,000 --> 09:24:00,840 I'm looking for add task function same 11734 09:23:58,480 --> 09:24:04,276 deal it's looking for this argument the 11735 09:24:00,840 --> 09:24:08,360 task one and it's actually type task and 11736 09:24:04,276 --> 09:24:10,880 then when it comes to the return we're 11737 09:24:08,360 --> 09:24:13,400 just going to go with void and then 11738 09:24:10,880 --> 09:24:15,160 right after our condition I think I can 11739 09:24:13,400 --> 09:24:17,120 remove that console log I don't think 11740 09:24:15,160 --> 09:24:19,960 there's a point anymore we can just go 11741 09:24:17,120 --> 09:24:22,596 with add task and essentially one by one 11742 09:24:19,960 --> 09:24:25,436 let's provide those properties we have 11743 09:24:22,596 --> 09:24:28,320 over here remember it has to be a string 11744 09:24:25,436 --> 09:24:32,640 so I'm just going to go with date then 11745 09:24:28,320 --> 09:24:35,040 I'm going to invoke get time method 11746 09:24:32,640 --> 09:24:38,880 and eventually I'm just going to turn 11747 09:24:35,040 --> 09:24:40,756 this into a string then I still have red 11748 09:24:38,880 --> 09:24:44,240 squiggly line 11749 09:24:40,756 --> 09:24:46,560 because I mean I actually haven't set up 11750 09:24:44,240 --> 09:24:49,520 the props properly my bad what am I 11751 09:24:46,560 --> 09:24:51,520 doing let me go here with add task and 11752 09:24:49,520 --> 09:24:52,880 this is actually going to be equal to a 11753 09:24:51,520 --> 09:24:55,160 form props 11754 09:24:52,880 --> 09:24:58,000 and now we get the correct red squiggly 11755 09:24:55,160 --> 09:25:00,080 lines where essentially we can see that 11756 09:24:58,000 --> 09:25:02,040 few properties are missing so let's 11757 09:25:00,080 --> 09:25:04,520 provide them we're going to go with 11758 09:25:02,040 --> 09:25:07,800 description so this one will be equal to 11759 09:25:04,520 --> 09:25:10,120 a text and also let's go with is 11760 09:25:07,800 --> 09:25:12,720 completed which we will hardcode and set 11761 09:25:10,120 --> 09:25:15,040 it equal to false now I don't have any 11762 09:25:12,720 --> 09:25:18,436 red squiggly lines and once we navigate 11763 09:25:15,040 --> 09:25:21,240 to the browser I'm actually going to 11764 09:25:18,436 --> 09:25:24,720 open up the components just so I can 11765 09:25:21,240 --> 09:25:28,400 see what we have as far as the state and 11766 09:25:24,720 --> 09:25:31,240 now let me go with first task me invoke 11767 09:25:28,400 --> 09:25:34,276 this and yep notice over here we are 11768 09:25:31,240 --> 09:25:36,800 successfully adding this task to our 11769 09:25:34,276 --> 09:25:38,480 list all right so we're pretty much done 11770 09:25:36,800 --> 09:25:41,520 with the form I don't think we're going 11771 09:25:38,480 --> 09:25:45,120 to work in that component anymore now we 11772 09:25:41,520 --> 09:25:49,360 just want to render the list and also 11773 09:25:45,120 --> 09:25:52,360 toggle the task remember it has that is 11774 09:25:49,360 --> 09:25:54,480 completed property which is Boolean um 11775 09:25:52,360 --> 09:25:56,720 and just like in the fundamentals 11776 09:25:54,480 --> 09:25:59,520 project we just want to display some 11777 09:25:56,720 --> 09:26:02,040 checkbox and then every time we will 11778 09:25:59,520 --> 09:26:04,756 click on that checkbox then we'll toggle 11779 09:26:02,040 --> 09:26:07,756 that value and actually before we 11780 09:26:04,756 --> 09:26:09,680 render the list I do want to right away 11781 09:26:07,756 --> 09:26:11,436 set up this function since we'll pass it 11782 09:26:09,680 --> 09:26:15,480 down to a list 11783 09:26:11,436 --> 09:26:17,276 component the functionality is super 11784 09:26:15,480 --> 09:26:20,120 straightforward where we want to go 11785 09:26:17,276 --> 09:26:23,720 toggle task now this function is looking 11786 09:26:20,120 --> 09:26:26,960 for the ID so so therefore let's right 11787 09:26:23,720 --> 09:26:29,240 away set up the inline type annotation 11788 09:26:26,960 --> 09:26:32,480 and let's say that ID is actually going 11789 09:26:29,240 --> 09:26:34,880 to be type string now as far as the 11790 09:26:32,480 --> 09:26:38,080 actual functionality we want to go with 11791 09:26:34,880 --> 09:26:41,720 set tasks and then remember when we 11792 09:26:38,080 --> 09:26:43,916 invoke map we get back a new array so 11793 09:26:41,720 --> 09:26:47,596 now I just want to iterate over the 11794 09:26:43,916 --> 09:26:50,200 array and if the ID matches the task ID 11795 09:26:47,596 --> 09:26:52,400 I want to set the task is completed to 11796 09:26:50,200 --> 09:26:54,680 the opposite value now if it's not the 11797 09:26:52,400 --> 09:26:57,200 case if they don't match then we just 11798 09:26:54,680 --> 09:27:00,160 return task as it is so let's go with 11799 09:26:57,200 --> 09:27:03,080 tasks map and yes as a sign not there 11800 09:27:00,160 --> 09:27:05,160 are Million Ways How we can write this 11801 09:27:03,080 --> 09:27:07,120 functionality I just went with this 11802 09:27:05,160 --> 09:27:11,000 approach so we're going to go here with 11803 09:27:07,120 --> 09:27:14,960 task then we'll check if task ID is 11804 09:27:11,000 --> 09:27:16,120 equal to ID we're passing in then we 11805 09:27:14,960 --> 09:27:20,040 want to 11806 09:27:16,120 --> 09:27:22,480 return this task with a new is completed 11807 09:27:20,040 --> 09:27:24,640 value so we want to spread out the 11808 09:27:22,480 --> 09:27:27,200 existing task and we want to go is 11809 09:27:24,640 --> 09:27:29,916 completed and set it equal to the 11810 09:27:27,200 --> 09:27:32,520 opposite so we're going to look for task 11811 09:27:29,916 --> 09:27:35,800 and then is completed now if it's not 11812 09:27:32,520 --> 09:27:38,960 the case then we'll simply return the 11813 09:27:35,800 --> 09:27:42,120 task and with this in place now we can 11814 09:27:38,960 --> 09:27:47,436 pass this toggle task down as well as 11815 09:27:42,120 --> 09:27:50,916 our entire tasks list so first prop will 11816 09:27:47,436 --> 09:27:54,040 be tasks and the second one actually is 11817 09:27:50,916 --> 09:27:57,120 going to be toggle task now we haven't 11818 09:27:54,040 --> 09:27:59,520 set up of course anything in the list so 11819 09:27:57,120 --> 09:28:02,400 all of that is coming up let me just 11820 09:27:59,520 --> 09:28:07,200 pass this down we're looking for toggle 11821 09:28:02,400 --> 09:28:10,916 task then let's navigate to our list and 11822 09:28:07,200 --> 09:28:15,276 first let's grab our task we will need 11823 09:28:10,916 --> 09:28:20,360 our type so import type task and that is 11824 09:28:15,276 --> 09:28:22,320 coming from my types yep that's the one 11825 09:28:20,360 --> 09:28:26,276 and let's set up right away the list 11826 09:28:22,320 --> 09:28:29,680 props so type list props and in here we 11827 09:28:26,276 --> 09:28:32,840 have tasks which is actually an array of 11828 09:28:29,680 --> 09:28:36,800 tasks and also we want to set up that 11829 09:28:32,840 --> 09:28:40,040 toggle task so toggle task same 11830 09:28:36,800 --> 09:28:43,560 deal it's going to have the ID and 11831 09:28:40,040 --> 09:28:45,800 actually that ID will be a string but 11832 09:28:43,560 --> 09:28:48,840 notice how essentially we're providing 11833 09:28:45,800 --> 09:28:51,480 an object correct so make sure you have 11834 09:28:48,840 --> 09:28:53,436 the same syntax okay good and in here we 11835 09:28:51,480 --> 09:28:57,040 want to the structure both of them so 11836 09:28:53,436 --> 09:29:01,520 tasks and toggle task and we'll set it 11837 09:28:57,040 --> 09:29:04,120 equal to list props beautiful and now we 11838 09:29:01,520 --> 09:29:07,436 want to iterate over the list display 11839 09:29:04,120 --> 09:29:10,360 all the items and also let's right away 11840 09:29:07,436 --> 09:29:13,160 set up the toggle task functionality so 11841 09:29:10,360 --> 09:29:17,800 we're going to go over here with Anor 11842 09:29:13,160 --> 09:29:20,320 list let's add a class name of list then 11843 09:29:17,800 --> 09:29:25,720 inside of the unor list let's go with 11844 09:29:20,320 --> 09:29:27,960 tasks. map let's invoke this and over 11845 09:29:25,720 --> 09:29:30,120 here we're going to have a task and then 11846 09:29:27,960 --> 09:29:33,040 for every task we want to set up the 11847 09:29:30,120 --> 09:29:36,800 return it's going to be a list item and 11848 09:29:33,040 --> 09:29:39,000 also we'll provide a key that's a must 11849 09:29:36,800 --> 09:29:42,640 so we're going to go here with task 11850 09:29:39,000 --> 09:29:44,720 ID Also let's set up the paragraph This 11851 09:29:42,640 --> 09:29:47,000 is where we'll display the task 11852 09:29:44,720 --> 09:29:52,320 description as far as the classes we're 11853 09:29:47,000 --> 09:29:55,596 looking for task and text and over here 11854 09:29:52,320 --> 09:29:57,680 let's set up task and description so we 11855 09:29:55,596 --> 09:30:00,800 have access to that and now let's set up 11856 09:29:57,680 --> 09:30:02,840 our input so at the very end over here 11857 09:30:00,800 --> 09:30:06,800 we're going to go with input now it's 11858 09:30:02,840 --> 09:30:09,040 not going to be a text actually we'll go 11859 09:30:06,800 --> 09:30:11,480 with checkbox we're going to go with 11860 09:30:09,040 --> 09:30:13,640 checked so it has this prop and we're 11861 09:30:11,480 --> 09:30:18,560 going to set it equal to task and then 11862 09:30:13,640 --> 09:30:20,200 dot is completed so if it's true then 11863 09:30:18,560 --> 09:30:22,640 the checkbox is going to be checked if 11864 09:30:20,200 --> 09:30:25,040 not then it's not going to be the case 11865 09:30:22,640 --> 09:30:28,560 and we'll right away add this onchange I 11866 09:30:25,040 --> 09:30:30,840 will pass over here the inline function 11867 09:30:28,560 --> 09:30:33,520 we don't need actually the event I 11868 09:30:30,840 --> 09:30:35,680 simply want to go with title task and 11869 09:30:33,520 --> 09:30:38,120 then I want to provide the object with 11870 09:30:35,680 --> 09:30:41,640 the ID property where I'm looking for 11871 09:30:38,120 --> 09:30:44,640 TAS ID now before you ask can you 11872 09:30:41,640 --> 09:30:48,120 essentially set up this 11873 09:30:44,640 --> 09:30:50,640 ID as a straight up argument so 11874 09:30:48,120 --> 09:30:53,240 essentially without that object yes 11875 09:30:50,640 --> 09:30:56,480 absolutely it's just my preference to 11876 09:30:53,240 --> 09:30:58,800 set it up as an object in this case and 11877 09:30:56,480 --> 09:31:01,756 with this in place we're done with the 11878 09:30:58,800 --> 09:31:05,596 list as well as toggle task 11879 09:31:01,756 --> 09:31:08,160 functionality so if I add a second one I 11880 09:31:05,596 --> 09:31:12,080 can nicely toggle over here this value 11881 09:31:08,160 --> 09:31:13,960 as well now if I'm going to refresh the 11882 09:31:12,080 --> 09:31:16,640 page of course I'm going to lose all of 11883 09:31:13,960 --> 09:31:19,320 the values since we haven't set up the 11884 09:31:16,640 --> 09:31:20,880 local storage yet and in fact this is 11885 09:31:19,320 --> 09:31:23,436 something we're going to do in the 11886 09:31:20,880 --> 09:31:26,480 following video okay so now let's just 11887 09:31:23,436 --> 09:31:28,240 add local storage to our application and 11888 09:31:26,480 --> 09:31:31,200 I want to start by navigating to the 11889 09:31:28,240 --> 09:31:34,880 browser to the application and I want to 11890 09:31:31,200 --> 09:31:37,200 remove the previous ones I believe I 11891 09:31:34,880 --> 09:31:39,560 still have values from the fundamental 11892 09:31:37,200 --> 09:31:42,360 section then we want to create two 11893 09:31:39,560 --> 09:31:45,680 functions one is going to be load tasks 11894 09:31:42,360 --> 09:31:47,596 so we want to load the tasks when the 11895 09:31:45,680 --> 09:31:50,520 application starts up and we'll also 11896 09:31:47,596 --> 09:31:52,840 have one to update the storage and since 11897 09:31:50,520 --> 09:31:55,040 it's a react project we can use use 11898 09:31:52,840 --> 09:31:56,960 effect and then pretty much every time 11899 09:31:55,040 --> 09:32:00,276 there's going to be some change in a 11900 09:31:56,960 --> 09:32:03,360 tasks list we will invoke the update 11901 09:32:00,276 --> 09:32:07,160 storage let's start with the 11902 09:32:03,360 --> 09:32:11,400 load tasks function so this function is 11903 09:32:07,160 --> 09:32:14,400 going to return an array of tasks as far 11904 09:32:11,400 --> 09:32:18,000 as the functionality we want to go with 11905 09:32:14,400 --> 09:32:21,596 stored tasks so that is equal to local 11906 09:32:18,000 --> 09:32:24,840 storage then we're looking for get item 11907 09:32:21,596 --> 09:32:26,840 that tasks something we don't have yet 11908 09:32:24,840 --> 09:32:30,240 and then we want to 11909 09:32:26,840 --> 09:32:33,160 return and then based on what we have in 11910 09:32:30,240 --> 09:32:36,560 the local storage either we're going to 11911 09:32:33,160 --> 09:32:38,596 return those tasks or we're going to 11912 09:32:36,560 --> 09:32:40,480 return an empty array so let's set up 11913 09:32:38,596 --> 09:32:44,840 turning operator we're going to go with 11914 09:32:40,480 --> 09:32:47,276 Json parse and we'll pass in stored 11915 09:32:44,840 --> 09:32:50,756 tasks that's not the case we'll return 11916 09:32:47,276 --> 09:32:54,400 empty array and then in here I'm just 11917 09:32:50,756 --> 09:32:58,240 going to provide a function and then I'm 11918 09:32:54,400 --> 09:33:00,680 going to invoke load tasks and then like 11919 09:32:58,240 --> 09:33:02,756 I said we also want to set up one to 11920 09:33:00,680 --> 09:33:05,360 update storage so every time we'll 11921 09:33:02,756 --> 09:33:08,840 change something about the list be it 11922 09:33:05,360 --> 09:33:11,360 add the task to the list or toggle D is 11923 09:33:08,840 --> 09:33:14,200 completed we'll update the storage so 11924 09:33:11,360 --> 09:33:17,276 update storage now this one is looking 11925 09:33:14,200 --> 09:33:20,400 for the tasks and actually it's going to 11926 09:33:17,276 --> 09:33:22,520 be an array of task now it's not going 11927 09:33:20,400 --> 09:33:25,080 to return anything so we're just going 11928 09:33:22,520 --> 09:33:26,680 to go with void and then we want to go 11929 09:33:25,080 --> 09:33:30,080 with local 11930 09:33:26,680 --> 09:33:33,916 storage then dot we're looking for set 11931 09:33:30,080 --> 09:33:36,480 item the name is going to be tasks and 11932 09:33:33,916 --> 09:33:39,840 we're going to go Json stringify since 11933 09:33:36,480 --> 09:33:42,680 we can only store strings over there and 11934 09:33:39,840 --> 09:33:44,480 we'll provide the tasks so this is how 11935 09:33:42,680 --> 09:33:46,080 we're going to create those items and 11936 09:33:44,480 --> 09:33:48,480 then when the project 11937 09:33:46,080 --> 09:33:50,916 loads this is how we're going to access 11938 09:33:48,480 --> 09:33:52,400 them like I mentioned since this is a 11939 09:33:50,916 --> 09:33:55,040 react project 11940 09:33:52,400 --> 09:33:58,080 we can go with use effect and then 11941 09:33:55,040 --> 09:34:01,116 pretty much every time there's going to 11942 09:33:58,080 --> 09:34:04,680 be some change in our dependency array 11943 09:34:01,116 --> 09:34:07,680 which contains the tasks we will also 11944 09:34:04,680 --> 09:34:11,040 invoke the update storage and we'll grab 11945 09:34:07,680 --> 09:34:14,360 the tasks value so now let me navigate 11946 09:34:11,040 --> 09:34:16,840 back to the browser or it's over here 11947 09:34:14,360 --> 09:34:21,116 everything is empty I have no tasks but 11948 09:34:16,840 --> 09:34:23,080 I can go with first task okay good 11949 09:34:21,116 --> 09:34:25,916 second task task notice how they are 11950 09:34:23,080 --> 09:34:28,080 actually saved in local storage I can 11951 09:34:25,916 --> 09:34:31,080 also toggle the is completed and then 11952 09:34:28,080 --> 09:34:33,916 once I refresh I still preserve the 11953 09:34:31,080 --> 09:34:36,560 values that should do it for typescript 11954 09:34:33,916 --> 09:34:38,400 with react section hopefully everyone 11955 09:34:36,560 --> 09:34:40,320 enjoyed it and up next we're going to 11956 09:34:38,400 --> 09:34:42,800 start working on a big typescript 11957 09:34:40,320 --> 09:34:45,840 project where we're going to implement 11958 09:34:42,800 --> 09:34:48,960 most of the things that we have learned 11959 09:34:45,840 --> 09:34:52,640 in fundamentals and react with 11960 09:34:48,960 --> 09:34:52,640 typescript sections 875157

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.