All language subtitles for 010 Diving Into Generics_en

af Afrikaans
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bn Bengali
bs Bosnian
bg Bulgarian
ca Catalan
ceb Cebuano
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
tl Filipino
fi Finnish
fr French
fy Frisian
gl Galician
ka Georgian
de German
el Greek
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
km Khmer
ko Korean
ku Kurdish (Kurmanji)
ky Kyrgyz
lo Lao
la Latin
lv Latvian
lt Lithuanian
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mn Mongolian
my Myanmar (Burmese)
ne Nepali
no Norwegian
ps Pashto
fa Persian Download
pl Polish
pt Portuguese
pa Punjabi
ro Romanian
ru Russian
sm Samoan
gd Scots Gaelic
sr Serbian
st Sesotho
sn Shona
sd Sindhi
si Sinhala
sk Slovak
sl Slovenian
so Somali
es Spanish
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
te Telugu
th Thai
tr Turkish
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu
or Odia (Oriya)
rw Kinyarwanda
tk Turkmen
tt Tatar
ug Uyghur
Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated: 1 00:00:02,090 --> 00:00:04,990 Now, we'll of course, work a lot with functions 2 00:00:04,990 --> 00:00:08,029 once we start working with React and TypeScript 3 00:00:08,029 --> 00:00:09,280 and therefore we'll then see 4 00:00:09,280 --> 00:00:11,880 all those function types in action there. 5 00:00:11,880 --> 00:00:15,200 I wanna conclude this basic introduction 6 00:00:15,200 --> 00:00:18,560 to TypeScript with a slightly more advanced 7 00:00:18,560 --> 00:00:22,200 yet very important TypeScript feature. 8 00:00:22,200 --> 00:00:24,373 And that would be Generics. 9 00:00:25,570 --> 00:00:28,560 Now this can be tricky to wrap your head around, 10 00:00:28,560 --> 00:00:32,910 but I'll try my best to explain it such that it makes sense. 11 00:00:32,910 --> 00:00:35,770 Let's say we have a function here, 12 00:00:35,770 --> 00:00:37,957 which I'll name 'insertAtBeginning', 13 00:00:39,842 --> 00:00:42,840 and this function takes an array and the value 14 00:00:42,840 --> 00:00:44,860 as parameters, 15 00:00:44,860 --> 00:00:46,627 and the array should be 16 00:00:46,627 --> 00:00:49,420 any array full of any kinds of values. 17 00:00:49,420 --> 00:00:51,820 And the values should be any kind of value 18 00:00:51,820 --> 00:00:53,650 because I don't care in this function 19 00:00:53,650 --> 00:00:56,330 if it's an array full of numbers or strings 20 00:00:56,330 --> 00:00:58,363 or of personal objects. 21 00:00:59,230 --> 00:01:02,190 Whatever it is, what I wanna do in this function, 22 00:01:02,190 --> 00:01:04,803 is I wanna create a new array, 23 00:01:07,500 --> 00:01:09,760 for example by using the spread operator 24 00:01:09,760 --> 00:01:12,300 and copying the existing array, 25 00:01:12,300 --> 00:01:15,290 that's a standard modern JavaScript feature 26 00:01:15,290 --> 00:01:17,093 also supported by TypeScript. 27 00:01:17,950 --> 00:01:20,770 And then in front of that old array, 28 00:01:20,770 --> 00:01:22,443 I wanna insert the value. 29 00:01:25,320 --> 00:01:28,623 And then I want to return this new array here. 30 00:01:29,780 --> 00:01:31,980 That could be a utility helper function, 31 00:01:31,980 --> 00:01:34,160 which we create to have a function 32 00:01:34,160 --> 00:01:37,030 which we can easily call to add a value 33 00:01:37,030 --> 00:01:40,080 in an array without changing the existing array. 34 00:01:40,080 --> 00:01:42,040 So the advantage of this function could be 35 00:01:42,040 --> 00:01:44,570 that the original array never changes, 36 00:01:44,570 --> 00:01:47,033 but instead we're getting a brand new array. 37 00:01:48,530 --> 00:01:49,363 Now for this, 38 00:01:49,363 --> 00:01:51,700 I could of course have a demo array with, 39 00:01:51,700 --> 00:01:53,930 let's say free values, 40 00:01:53,930 --> 00:01:57,940 and then I get my updated array 41 00:01:57,940 --> 00:01:59,540 Where I call 'insertAtBeginning' 42 00:02:00,520 --> 00:02:02,560 and I pass in the demo array. 43 00:02:02,560 --> 00:02:07,283 And then a value of minus one let's say. 44 00:02:08,120 --> 00:02:10,800 With that, I know that the updated array 45 00:02:10,800 --> 00:02:14,940 will be this array with minus one inserted at the beginning. 46 00:02:14,940 --> 00:02:19,940 So this will give me minus one, one, two, three. 47 00:02:20,060 --> 00:02:23,200 And hence thereafter I might wanna do things with that array 48 00:02:23,200 --> 00:02:26,320 which I can do with an array full of numbers. 49 00:02:26,320 --> 00:02:28,410 But the problem is that updated array, 50 00:02:28,410 --> 00:02:29,630 if I hover over it 51 00:02:29,630 --> 00:02:32,480 is inferred to be of type any array. 52 00:02:32,480 --> 00:02:35,850 So an array full of any kind of values. 53 00:02:35,850 --> 00:02:38,470 TypeScript here doesn't pick up 54 00:02:38,470 --> 00:02:40,550 that it's an array full of numbers. 55 00:02:40,550 --> 00:02:44,950 Why, because here I have any, as a type 56 00:02:44,950 --> 00:02:48,200 And I can't say that I only wanna allow numbers here, 57 00:02:48,200 --> 00:02:51,690 because what if I want to use this utility function 58 00:02:51,690 --> 00:02:53,703 with an array full of strings? 59 00:02:54,740 --> 00:02:57,170 So we need any, but at the same time 60 00:02:57,170 --> 00:03:01,350 that kind of removes all kind of TypeScript support 61 00:03:01,350 --> 00:03:04,180 we might be getting after calling this function. 62 00:03:04,180 --> 00:03:07,880 We're not getting any good TypeScript support for updated 63 00:03:07,880 --> 00:03:10,550 array thereafter, because that is an inferred 64 00:03:10,550 --> 00:03:13,240 to be an array full of any kind of objects 65 00:03:13,240 --> 00:03:15,200 or any kind of values. 66 00:03:15,200 --> 00:03:17,220 And that's not useful. 67 00:03:17,220 --> 00:03:20,630 If I then for example, try to access the first element 68 00:03:20,630 --> 00:03:24,200 and called split here, which I could call on a string. 69 00:03:24,200 --> 00:03:26,030 I don't get an error here. 70 00:03:26,030 --> 00:03:28,720 I would get a runtime error instead 71 00:03:28,720 --> 00:03:31,340 because I can't call split on a number. 72 00:03:31,340 --> 00:03:34,840 But again, TypeScript doesn't know that the first value 73 00:03:34,840 --> 00:03:37,350 in updated array is a number. 74 00:03:37,350 --> 00:03:40,360 We know it as a developer, but because of our type 75 00:03:40,360 --> 00:03:42,660 definitions here, which we need, 76 00:03:42,660 --> 00:03:44,870 TypeScript doesn't know it. 77 00:03:44,870 --> 00:03:46,980 And to work around such problems, 78 00:03:46,980 --> 00:03:49,220 we have this feature of Generics 79 00:03:49,220 --> 00:03:52,510 and that's why I had does lengthy introduction here 80 00:03:52,510 --> 00:03:55,610 because it is important to understand why 81 00:03:55,610 --> 00:03:57,320 this feature exists. 82 00:03:57,320 --> 00:04:01,640 With this Generics feature, we can convert this function 83 00:04:01,640 --> 00:04:04,050 to a Generic function. 84 00:04:04,050 --> 00:04:06,560 For this we use a special syntax here 85 00:04:06,560 --> 00:04:08,230 after the function name, 86 00:04:08,230 --> 00:04:12,310 in front of the parameter list, we add angle brackets 87 00:04:12,310 --> 00:04:15,680 and that's now not a standard JavaScript feature 88 00:04:15,680 --> 00:04:18,070 but really only a feature we can use here 89 00:04:18,070 --> 00:04:20,370 since we're using TypeScript. 90 00:04:20,370 --> 00:04:25,040 And in here we can define a Generic type which will only 91 00:04:25,040 --> 00:04:27,880 be available inside of this function. 92 00:04:27,880 --> 00:04:30,490 Typically that's called T for type 93 00:04:30,490 --> 00:04:33,763 but any identifier of your choices okay. 94 00:04:35,230 --> 00:04:37,710 Now you can use this type in your function, 95 00:04:37,710 --> 00:04:40,030 including the parameter list. 96 00:04:40,030 --> 00:04:44,120 And here I'm saying, I want to use this for my array. 97 00:04:44,120 --> 00:04:48,553 So my array will be full of Ts and my value. 98 00:04:50,130 --> 00:04:51,913 This should always be of type T. 99 00:04:52,920 --> 00:04:54,920 But how does this help us? 100 00:04:54,920 --> 00:04:58,190 Well like this set of codes it's just very cryptic, 101 00:04:58,190 --> 00:05:02,810 but now when we call this function, now TypeScript actually 102 00:05:02,810 --> 00:05:05,600 is able to understand that 103 00:05:05,600 --> 00:05:10,150 it should look at the concrete values of the arguments here. 104 00:05:10,150 --> 00:05:13,070 And it understands that this is an array of numbers 105 00:05:13,070 --> 00:05:16,290 and this is just a number and they are free to understand 106 00:05:16,290 --> 00:05:19,910 that updated array will be an array of numbers. 107 00:05:19,910 --> 00:05:23,530 Because of this generic type feature, because we're telling 108 00:05:23,530 --> 00:05:28,530 TypeScript that the type here is actually not any type 109 00:05:28,920 --> 00:05:30,830 it's not any kind of value. 110 00:05:30,830 --> 00:05:35,790 Instead, we tell it that the type of this array and 111 00:05:35,790 --> 00:05:37,940 of this value should be the same 112 00:05:37,940 --> 00:05:39,510 just that this is an array, 113 00:05:39,510 --> 00:05:42,880 but it's an array full of the same types of values 114 00:05:42,880 --> 00:05:45,640 as this single value has it. 115 00:05:45,640 --> 00:05:48,300 And that's an important piece of information. 116 00:05:48,300 --> 00:05:51,330 Therefore, TypeScript is able to look at the type 117 00:05:51,330 --> 00:05:53,670 of demo array, which it knows. 118 00:05:53,670 --> 00:05:56,810 It knows that this is a, an array of numbers it's able 119 00:05:56,810 --> 00:06:00,140 to infer this, and it looks at this type. 120 00:06:00,140 --> 00:06:03,530 And then it knows that logically, the array which gets 121 00:06:03,530 --> 00:06:06,980 constructed here has to be of the same type 122 00:06:06,980 --> 00:06:08,190 as the input array. 123 00:06:08,190 --> 00:06:10,040 It's able to infer this here. 124 00:06:10,040 --> 00:06:11,840 And hence, it's able to infer 125 00:06:11,840 --> 00:06:15,850 that what we get back here has to be an array of numbers. 126 00:06:15,850 --> 00:06:20,850 And if I would call, 'insertAtBeginning' here 127 00:06:20,960 --> 00:06:24,350 with an array of strings 128 00:06:24,350 --> 00:06:28,103 Like this, and I then pass in a string here, 129 00:06:28,960 --> 00:06:31,060 and I have my string array here, 130 00:06:31,060 --> 00:06:34,130 TypeScript is also able to infer this correctly 131 00:06:34,130 --> 00:06:36,890 because again, it looks at this type and this type 132 00:06:36,890 --> 00:06:40,300 and it knows that the return type is of the same value 133 00:06:40,300 --> 00:06:43,460 as this type, as T input array, enhance it again, 134 00:06:43,460 --> 00:06:45,200 infers this correctly. 135 00:06:45,200 --> 00:06:48,480 And therefore for the updated array here where it knows 136 00:06:48,480 --> 00:06:50,660 that it has to be an array full of numbers. 137 00:06:50,660 --> 00:06:54,910 It's now able to warn me when I try to call split. 138 00:06:54,910 --> 00:06:58,003 It tells me that this does not exist on type number. 139 00:06:59,560 --> 00:07:02,200 So that's just as important feature of generics. 140 00:07:02,200 --> 00:07:05,930 It's simply helps you write functions in this case, 141 00:07:05,930 --> 00:07:09,680 which are type safe yet flexible. 142 00:07:09,680 --> 00:07:10,910 They are flexible. 143 00:07:10,910 --> 00:07:14,090 They work with any type, but then once a certain type 144 00:07:14,090 --> 00:07:17,150 is used for that function, execution, 145 00:07:17,150 --> 00:07:20,750 that type is locked in and known. 146 00:07:20,750 --> 00:07:22,740 And that allows you to get the best 147 00:07:22,740 --> 00:07:27,740 of both worlds, flexibility and type safety. 148 00:07:27,920 --> 00:07:31,410 Again, I am fully aware that this generics feature can still 149 00:07:31,410 --> 00:07:33,660 be tricky to wrap your head around, 150 00:07:33,660 --> 00:07:35,450 but we'll also see it in action. 151 00:07:35,450 --> 00:07:38,700 Soon when we start working with reactants type stripped 152 00:07:38,700 --> 00:07:41,430 and then combined with what you learn here, 153 00:07:41,430 --> 00:07:43,620 it should become clearer and clearer 154 00:07:43,620 --> 00:07:45,783 how it works and why it's important. 155 00:07:46,910 --> 00:07:51,210 And speaking off that let's know, leave this basics file 156 00:07:51,210 --> 00:07:55,140 and this non react project, and let's instead move 157 00:07:55,140 --> 00:07:59,163 towards a react project, which is using TypeScript. 12479

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