Typeguards enable us to process function arguments based on their types.
Consider this example -
|
|
What if we want to use join
for strings or numbers? We can do a union type like so -
|
|
But, a real world problem would lie in processing the input arguments - not just returning them. Also, the methods and properties of the argument will change within a function based on their types. Typescript solves this problem using Typeguard.
Typeguard is a way if isolating types within an argument in specific blocks. Typescript provides different ways of solving this problem in different situations - typeof
, instanceof
, literals and in
. You can also go ahead and create your own typeguards if none of those satisfy your use case.
typeof
We can build on the previous example -
|
|
By simply the process of checking the type with typeof a == "string"
, Typescript will infer that anything in the block will use a
as a string
. So, you can easily use a.toUpperCase()
within the block but a.toFixed(0)
will produce a compilation error.
In the above code block, you could drop the else if
and include just an else
to get the same result.
|
|
Typescript will understand that the only other type option for a
and b
is a number
. So it will apply number
properties and methods to the variables within the else
block.
instanceof
Similar to the use-case for variables described above, instanceof
applies itself to user-defined classes and objects.
|
|
in
in
is similar to instanceof
but rather than using an instance of the class, we use a prop of the object to distinguish between two types.
|
|
literals
Literals just refer to literal values within an object and to use the possible options as typeguards.
|
|