0:03 before we proceed further in this course
0:05 and learn about other data types which
0:07 we have in typescript let's continue the
0:08 session which we started in the last
0:11 lecture so in this lecture we will learn
0:13 how to specify a data type explicitly to
0:16 a variable or to a function parameter
0:18 and then we will also talk about
0:21 something called as type inference so
0:23 let's go to vs
0:26 code and here I have commented the code
0:28 from the previous lectures now here what
0:29 I'm going to do is I'm going to create
0:32 create a function and I'm simply going
0:34 to call this function as sum and this
0:36 function is going to take two parameters
0:40 num one and num two and from within this
0:42 function I simply want to return the sum
0:45 of num one and num
0:50 two okay now what we want is we want to
0:53 use this sum function to add two numbers
0:56 for that we can simply call this sum and
0:59 there we can specify the value for Num
1:00 one and num two so for for example 12 and
1:02 and
1:05 13 okay and it is going to return us a
1:08 value let's simply go ahead and let's
1:12 log that value in the console like this
1:15 all right let's save this file let's
1:18 compile this app. TS file using TSC
1:20 space app. TS
1:23 command and you'll see 25 is logged here
1:25 so this is the correct result the sum of
1:27 12 and 13 is
1:32 25 but what I can also do is instead of
1:36 passing two numbers here I can also pass
1:38 a string and a number and we will not
1:40 get any error because here we have not
1:42 specified any data type for this num one
1:45 and num two right we want to use this
1:48 sum function only to add two numbers but
1:50 since we have not specified the data
1:52 type for Num one and num two parameter
1:54 we can pass any type of value to this
1:56 num one and num two so again if I save
2:01 the file here and again if we compile
2:03 dots you will notice that in the result
2:07 we have 1 12 13 and this is an
2:09 unexpected result because here what we
2:13 wanted is we wanted to add 12 and 13 but
2:15 since we are passing 12 as a string
2:17 value here this num one is going to
2:18 store a string value and num two is
2:21 going to store a number value so when we
2:24 are using this plus operator on a string
2:26 and a number it is basically going to
2:29 convert this num to also to a string and
2:31 then it will concatenate these two
2:35 string values right so since we are not
2:37 specifying the data type of this num1
2:39 and num2 the type of value which we want
2:41 to expect for this num one and num two
2:45 parameter we can pass any type of value
2:48 to this num1 and num two parameter but
2:51 in typescript we have this option to
2:54 explicitly specify what type of data we
2:57 want to get for some parameters for
3:00 example for this number one I only want
3:04 to receive number type so I can specify
3:07 its type as number like this and for
3:10 this number two also I only want to get
3:13 a number value and now you see when we
3:15 are passing numeric values here we are
3:17 not getting any error but when we are
3:20 passing a string value for this num one
3:22 since it is expecting a number you see
3:26 we have this error so now we will get a
3:29 compile time error whenever we try to
3:31 assign any other type of value to this
3:34 num one or num two parameter apart from
3:37 number type and this is the advantage we
3:40 have when we use typescript because in
3:41 JavaScript we cannot do something like
3:45 this right so in JavaScript for Num one
3:47 and num two if we only want to receive
3:49 numeric values we will have to write
3:51 some IFL statement and then we will have
3:53 to do data sanitization and all those
3:56 things but in typescript it is as simple
3:59 as this we can simply specify the type
4:01 of value which we want to receive for that
4:02 that
4:05 parameter so in typescript we can
4:09 identify any bugs or any issues which we
4:12 can introduce but in JavaScript we
4:14 cannot do that in JavaScript since we
4:16 don't have any option to specify the
4:19 data type for num1 and num two there we
4:21 will not get any error like this and in
4:23 that way we might introduce some bug in our
4:24 our
4:28 code so here we are basically specifying
4:31 the type for the parameters then we can
4:33 also set a type for a variable for
4:36 example let me go ahead and let me
4:40 create maybe N1 and I want this N1 to be
4:43 of number type so let's use this number
4:47 here and let's assign 10 to it then
4:50 let's also create another variable N2
4:52 this also should be of number type enter
4:54 this LS sign
4:57 20 and here let's remove this
5:00 console.log statement and here I want to
5:05 pass N1 for num1 parameter and here I
5:08 want to pass N2 for num2 parameter and
5:10 you see we don't have any error because
5:14 both N1 and N2 are of number type so if
5:17 I save the changes if we compile this
5:20 app. TS we should get the result as 30
5:23 as you can see now here we are
5:26 explicitly specifying the data type of
5:29 N1 and N2 but we actually don't need to
5:33 do this so if I remove this data
5:37 type and if we hover over this N1 or N2
5:39 variable so for example if I H over this
5:42 N1 you will see that the data type of N1
5:45 is inferred as number because we are
5:48 storing a numeric value to it so its
5:50 data type has been inferred as number in
5:53 the same way for this N2 also the data
5:55 type of this N2 has been inferred as
5:58 number and this is called as type
6:02 inference in typescript when we assign a
6:05 value to a variable the data type of
6:08 that variable is automatically inferred
6:09 based on the value with which we have
6:12 initialized it in this case we have
6:15 initialized this N1 with this value 10
6:17 this 10 is of number type so the data
6:20 type of this N1 has been set to number
6:22 and once its data type is inferred and
6:26 set we cannot change its data type so in
6:27 JavaScript you can create a variable
6:29 like this you can assign a number to it
6:31 and letter if you want you can assign
6:33 any other type of value to it for
6:36 example a Boolean value like this but
6:39 that will not work in typescript because
6:42 here the data type of this N1 it is
6:44 already inferred as number and it has
6:47 been set as number so letter if you try
6:49 to assign any other type of value to
6:51 that variable it will throw an error so
6:53 it says type Boolean is not assignable
6:56 to type number here we are trying to
6:58 assign a Boolean value to number type
7:00 and that's why we have this error
7:02 so once the data type of a variable is
7:06 inferred and set we cannot change the
7:08 data type of that variable we always
7:11 need to store the same type of value in that
7:12 that
7:16 variable I hope this point is clear so
7:19 if I try to assign a numeric value to it
7:21 in that case we will not get any error
7:24 so this all right here we need to assign
7:30 N1 okay so this N1 is of number type and
7:32 after assigning 10 to it we are trying
7:34 to reassign it with this value th000 so
7:36 we are not getting any error but if I
7:39 try to assign a string value to it we
7:42 will get an error and it says a string
7:44 is not assignable to type number because
7:47 N1 is of number type and we can only
7:49 assign a numeric value to it we cannot
7:52 assign string value to it and same is
7:54 true for other data types also for
7:57 example if I create a string variable
8:01 let's say St and to this if I assign
8:04 hello you will notice that the data type
8:08 of this s is inferred and set to string
8:10 so if I try to assign any other type of
8:12 value to it for example a numeric
8:15 value it will throw an error and it will
8:18 says that type number is not assignable
8:20 to type string because the data type of
8:22 this St variable it has been inferred as
8:25 string and it has been set as string so
8:27 we can only assign a string value to it
8:29 we cannot assign any other type of value
8:36 okay so as we learned if we want we can
8:39 explicitly specify the data type of a
8:43 variable something like this so this
8:46 will also work but we don't need to do
8:49 this because typescript automatically
8:53 infers the data type of a variable based
8:55 on the value which we have initialized
8:57 it with here we are initializing this N1
9:00 with this numeric value so its data type
9:03 will be inferred as
9:06 number so what is Type inference in
9:08 typescript when we assign a value to a
9:11 variable the data type of that variable
9:14 is automatically inferred and set and
9:16 that data type cannot be changed letter
9:17 in the
9:19 program and that's why we say that
9:22 typescript is strongly typed JavaScript
9:25 is dynamically typed in JavaScript let
9:30 me open a Javascript file here so in
9:32 JavaScript when we have created this N1
9:36 variable letter if we want we can assign
9:38 a value of any other type to this N1
9:40 variable for example I can assign a
9:43 string value to it like this and we will
9:46 not get any error initially we assigned
9:48 a number to it and letterer we trying to
9:50 assign a string to it and we are not
9:52 getting any error so this is how
9:54 JavaScript Works JavaScript is
9:56 dynamically typed the data type of a
9:59 variable is set based on what data we
10:01 have assigned to it initially we
10:03 assigned 10 to it so the data type of
10:06 this N1 variable was number and letter
10:08 we assigning a string to it so its data
10:10 type changed to string so here as you
10:13 can see in JavaScript the data type of a
10:15 variable can change based on what value
10:19 we are storing in it but in typescript
10:22 that is not the case in typescript once
10:25 the data type of a variable is set let
10:29 me close this app.js file so here when
10:31 we assigned this value 10 to this N1
10:35 variable its data type was set to number
10:37 as you can see and once it is set to
10:40 number its data type now cannot be
10:42 changed we cannot go ahead and assign a
10:45 string value to it in order to change
10:47 its data type to string that is not
10:49 possible in typescript and that's why
10:51 typescript is strongly typed but
10:54 JavaScript is dynamically
10:57 typed now here in this function let's
11:00 also specify what one more parameter
11:04 let's call it maybe is print and let's
11:06 say for this is print we want to receive
11:08 a Boolean
11:11 value okay now when we are calling this
11:13 sum function we only passing value for
11:16 num1 and num two let's also pass a value
11:20 for isprint and let's pass true here so
11:23 what we want is if this is print is true
11:26 so before returning from here let's say
11:31 we will check if is print so if the is
11:39 message and here let's
11:43 say sum equals and then we want to
11:46 display the sum of num one and num2 so
11:48 here let's create a variable let's call
11:51 it maybe s
11:56 equals num one plus num two and here
12:00 let's say some equals X s which is the
12:03 sum of num1 and num2 let's save the
12:08 changes let's compile this app. TS
12:12 file and here will say sum equals 30 but
12:14 if we pass false here in that case you
12:16 will not see that
12:18 message so let's save the file
12:22 again let's compile this app. TS and now
12:25 you will not see that message sum equals
12:28 and the sum of num one and num2 because
12:30 this message will only be printed if
12:32 this is print is
12:34 true then let's also create another
12:38 variable let's say message and let's say
12:40 this is going to be of type
12:44 string and now what we want is instead
12:47 of using this string value here we want
12:51 to use that message string and I'll also
12:54 add a space after that
12:58 message okay so now it is expecting four
12:59 parameters so let's also pass a value
13:01 for the message parameter and there
13:07 let's say the message is sum is
13:10 equal and let's save the changes and
13:12 here let's also set it to true because
13:14 then only the message will be
13:16 printed let's save the
13:20 changes let's compile this file
13:25 again and you see sum is equal to 30 so
13:27 it is printing the same message which we
13:29 passed here
13:31 but here if we try to assign any other
13:33 type of value to this message here we
13:34 have set the data type of this message
13:37 as string if we try to assign any other
13:40 type of value like for example number we
13:42 will get an
13:45 error so that's why remember that
13:48 typescript is strongly typed in
13:50 typescript a variable can have only one
13:53 data type its data type cannot be
13:55 changed this is all from this lecture if
13:57 you have any questions then feel free to
13:59 ask it thank you for listening and have