TypeScript contains various annotations similar to data types in any static language in the form of strings, numbers, boolean, arrays, records, etc.
You can assign a type annotation to variables, functions arguments, etc.
The cool thing about primitive types is that you don't need to manually assign any type when manually setting a default value.
In the code above, TypeScript will ensure that name always has a string type of value. This is referred to as an infer type.
You can begin writing code by visiting the typescript playground.
Let's see what happens when we assign a number to a name!
You will see the below message.
Type Safety in Functions
Let's understand what's going on here.
We are passing
stringargument. So the data type of
namecan only be a
At the end of the
)bracket, we have added another
stringannotation. It will make sure that
getEmailAddresswill always return
stringtype of data.
This is the beauty of TypeScript. Without even running the code, the code can easily be understood.
Let's see how we can provide type safety for traditional functions.
Non-Primitive Type Annotations
Typescript provides type safety to primitive types like array and objects.
The code above, looks similar to primitive code. We have added  at the end of the string type annotation. This will let us add only string type of data to the array.
Examples of Non-Primitive Type Annotations
Let's see how we can use a tuple.
You can use a tuple to have multiple data types in an array at a specific location.
What about the objects, though?
To provide type safety for object data type, TypeScript offers different ways of implementation.
The syntax looks similar to how we create objects in
We can make it reusable by creating
Although they serve different purposes,
types function in the same way.
interface can extend another
interface in the same way that classes do.
types are used to modify other types. It is known as types of types.
You only want to pass all of the arguments in rare instances, such as when an object contains optional properties. We can mark type annotations as optional. Let's see how we can do that.
When you run this code, you can skip the second argument. There will be no type error.
? is used to make an optional type.
In functions, you need to pass optional types after the usual types, in order to avoid confusion.
Similarly, we can use
type to make some props optional.
You can use the same syntax for
type as well. All you need to do is add
:. This way you can create flexible types.
With TypeScript, we can modify types through a few operations. We can create another kind by combining different types. We don't need to write any extra code to accomplish this.
An Intersection works as a logical
and operator. We can combine more than one type to create another type.
& to perform intersection operations.
Union works as a logical
or operator. We can combine types, but those types are optional.
intersection, we can skip some types properties.
| to perform union operations.
So far, we have seen how to create types statically, but what if we could create dynamic types? That's where the generic type comes into the picture.
Let's understand what is happening here. We have discovered a new syntax here in
This is a signature of generic type. You can pass and accept type in
- Here we are passing type
Tto the function like we pass an argument.
- After that we are assigning that type
Tto the argument. Finally, the syntax will look like this.
Generic type with traditional functions
Similarly, we can use generic types for non-primitive data types. Here's an example of how you can use generic types with objects.
T as an argument and then we can use that anywhere we want.
So far, we've looked at Typescript's fundamental building components.
The adventure does not end here. We'll delve deeper into typescript.
In the future blogs, we'll look at typescript tools, kinds of types, and conditional types.
Thank you for taking the time to read this blog.