This content explains how to explicitly define data types for variables and function parameters in TypeScript, and introduces the concept of type inference, highlighting TypeScript's strong typing compared to JavaScript's dynamic typing.
Mind Map
Click to expand
Click to explore the full interactive mind map • Zoom, pan, and navigate
before we proceed further in this course
and learn about other data types which
we have in typescript let's continue the
session which we started in the last
lecture so in this lecture we will learn
how to specify a data type explicitly to
a variable or to a function parameter
and then we will also talk about
something called as type inference so
let's go to vs
code and here I have commented the code
from the previous lectures now here what
I'm going to do is I'm going to create
create a function and I'm simply going
to call this function as sum and this
function is going to take two parameters
num one and num two and from within this
function I simply want to return the sum
of num one and num
two okay now what we want is we want to
use this sum function to add two numbers
for that we can simply call this sum and
there we can specify the value for Num
one and num two so for for example 12 and
and
13 okay and it is going to return us a
value let's simply go ahead and let's
log that value in the console like this
all right let's save this file let's
compile this app. TS file using TSC
space app. TS
command and you'll see 25 is logged here
so this is the correct result the sum of
12 and 13 is
25 but what I can also do is instead of
passing two numbers here I can also pass
a string and a number and we will not
get any error because here we have not
specified any data type for this num one
and num two right we want to use this
sum function only to add two numbers but
since we have not specified the data
type for Num one and num two parameter
we can pass any type of value to this
num one and num two so again if I save
the file here and again if we compile
dots you will notice that in the result
we have 1 12 13 and this is an
unexpected result because here what we
wanted is we wanted to add 12 and 13 but
since we are passing 12 as a string
value here this num one is going to
store a string value and num two is
going to store a number value so when we
are using this plus operator on a string
and a number it is basically going to
convert this num to also to a string and
then it will concatenate these two
string values right so since we are not
specifying the data type of this num1
and num2 the type of value which we want
to expect for this num one and num two
parameter we can pass any type of value
to this num1 and num two parameter but
in typescript we have this option to
explicitly specify what type of data we
want to get for some parameters for
example for this number one I only want
to receive number type so I can specify
its type as number like this and for
this number two also I only want to get
a number value and now you see when we
are passing numeric values here we are
not getting any error but when we are
passing a string value for this num one
since it is expecting a number you see
we have this error so now we will get a
compile time error whenever we try to
assign any other type of value to this
num one or num two parameter apart from
number type and this is the advantage we
have when we use typescript because in
JavaScript we cannot do something like
this right so in JavaScript for Num one
and num two if we only want to receive
numeric values we will have to write
some IFL statement and then we will have
to do data sanitization and all those
things but in typescript it is as simple
as this we can simply specify the type
of value which we want to receive for that
that
parameter so in typescript we can
identify any bugs or any issues which we
can introduce but in JavaScript we
cannot do that in JavaScript since we
don't have any option to specify the
data type for num1 and num two there we
will not get any error like this and in
that way we might introduce some bug in our
our
code so here we are basically specifying
the type for the parameters then we can
also set a type for a variable for
example let me go ahead and let me
create maybe N1 and I want this N1 to be
of number type so let's use this number
here and let's assign 10 to it then
let's also create another variable N2
this also should be of number type enter
this LS sign
20 and here let's remove this
console.log statement and here I want to
pass N1 for num1 parameter and here I
want to pass N2 for num2 parameter and
you see we don't have any error because
both N1 and N2 are of number type so if
I save the changes if we compile this
app. TS we should get the result as 30
as you can see now here we are
explicitly specifying the data type of
N1 and N2 but we actually don't need to
do this so if I remove this data
type and if we hover over this N1 or N2
variable so for example if I H over this
N1 you will see that the data type of N1
is inferred as number because we are
storing a numeric value to it so its
data type has been inferred as number in
the same way for this N2 also the data
type of this N2 has been inferred as
number and this is called as type
inference in typescript when we assign a
value to a variable the data type of
that variable is automatically inferred
based on the value with which we have
initialized it in this case we have
initialized this N1 with this value 10
this 10 is of number type so the data
type of this N1 has been set to number
and once its data type is inferred and
set we cannot change its data type so in
JavaScript you can create a variable
like this you can assign a number to it
and letter if you want you can assign
any other type of value to it for
example a Boolean value like this but
that will not work in typescript because
here the data type of this N1 it is
already inferred as number and it has
been set as number so letter if you try
to assign any other type of value to
that variable it will throw an error so
it says type Boolean is not assignable
to type number here we are trying to
assign a Boolean value to number type
and that's why we have this error
so once the data type of a variable is
inferred and set we cannot change the
data type of that variable we always
need to store the same type of value in that
that
variable I hope this point is clear so
if I try to assign a numeric value to it
in that case we will not get any error
so this all right here we need to assign
N1 okay so this N1 is of number type and
after assigning 10 to it we are trying
to reassign it with this value th000 so
we are not getting any error but if I
try to assign a string value to it we
will get an error and it says a string
is not assignable to type number because
N1 is of number type and we can only
assign a numeric value to it we cannot
assign string value to it and same is
true for other data types also for
example if I create a string variable
let's say St and to this if I assign
hello you will notice that the data type
of this s is inferred and set to string
so if I try to assign any other type of
value to it for example a numeric
value it will throw an error and it will
says that type number is not assignable
to type string because the data type of
this St variable it has been inferred as
string and it has been set as string so
we can only assign a string value to it
we cannot assign any other type of value
okay so as we learned if we want we can
explicitly specify the data type of a
variable something like this so this
will also work but we don't need to do
this because typescript automatically
infers the data type of a variable based
on the value which we have initialized
it with here we are initializing this N1
with this numeric value so its data type
will be inferred as
number so what is Type inference in
typescript when we assign a value to a
variable the data type of that variable
is automatically inferred and set and
that data type cannot be changed letter
in the
program and that's why we say that
typescript is strongly typed JavaScript
is dynamically typed in JavaScript let
me open a Javascript file here so in
JavaScript when we have created this N1
variable letter if we want we can assign
a value of any other type to this N1
variable for example I can assign a
string value to it like this and we will
not get any error initially we assigned
a number to it and letterer we trying to
assign a string to it and we are not
getting any error so this is how
JavaScript Works JavaScript is
dynamically typed the data type of a
variable is set based on what data we
have assigned to it initially we
assigned 10 to it so the data type of
this N1 variable was number and letter
we assigning a string to it so its data
type changed to string so here as you
can see in JavaScript the data type of a
variable can change based on what value
we are storing in it but in typescript
that is not the case in typescript once
the data type of a variable is set let
me close this app.js file so here when
we assigned this value 10 to this N1
variable its data type was set to number
as you can see and once it is set to
number its data type now cannot be
changed we cannot go ahead and assign a
string value to it in order to change
its data type to string that is not
possible in typescript and that's why
typescript is strongly typed but
JavaScript is dynamically
typed now here in this function let's
also specify what one more parameter
let's call it maybe is print and let's
say for this is print we want to receive
a Boolean
value okay now when we are calling this
sum function we only passing value for
num1 and num two let's also pass a value
for isprint and let's pass true here so
what we want is if this is print is true
so before returning from here let's say
we will check if is print so if the is
message and here let's
say sum equals and then we want to
display the sum of num one and num2 so
here let's create a variable let's call
it maybe s
equals num one plus num two and here
let's say some equals X s which is the
sum of num1 and num2 let's save the
changes let's compile this app. TS
file and here will say sum equals 30 but
if we pass false here in that case you
will not see that
message so let's save the file
again let's compile this app. TS and now
you will not see that message sum equals
and the sum of num one and num2 because
this message will only be printed if
this is print is
true then let's also create another
variable let's say message and let's say
this is going to be of type
string and now what we want is instead
of using this string value here we want
to use that message string and I'll also
add a space after that
message okay so now it is expecting four
parameters so let's also pass a value
for the message parameter and there
let's say the message is sum is
equal and let's save the changes and
here let's also set it to true because
then only the message will be
printed let's save the
changes let's compile this file
again and you see sum is equal to 30 so
it is printing the same message which we
passed here
but here if we try to assign any other
type of value to this message here we
have set the data type of this message
as string if we try to assign any other
type of value like for example number we
will get an
error so that's why remember that
typescript is strongly typed in
typescript a variable can have only one
data type its data type cannot be
changed this is all from this lecture if
you have any questions then feel free to
ask it thank you for listening and have
Click on any text or timestamp to jump to that moment in the video
Share:
Most transcripts ready in under 5 seconds
One-Click Copy125+ LanguagesSearch ContentJump to Timestamps
Paste YouTube URL
Enter any YouTube video link to get the full transcript
Transcript Extraction Form
Most transcripts ready in under 5 seconds
Get Our Chrome Extension
Get transcripts instantly without leaving YouTube. Install our Chrome extension for one-click access to any video's transcript directly on the watch page.