Day 2 of TypeScript — As a Beginner
Things that I’ve learned on day 3…
Things I’ve Learned On Day 3:
- Higher-order Functions
- Quick Difference Between JS and TS.
1. Higher-Order Functions
Let’s recap what the Higher-order functions is.
They are functions that can accept other functions as arguments and/or return functions as their results.
Some key points are as follows:
- Accepting functions as arguments
- Returning functions
- Function composition
- Encapsulation of behavior
- Abstraction and generalization
Now let’s see how we can use TypeScript for implementing Higher-order Functions.
Problem:
Write a function calculateSum
that takes an array of numbers as input and a callback function. The function should calculate the sum of the numbers in the array and pass the result to the callback function.

Explanation:
- The
calculateSum
function takes in an array of numbers (numArr
) and a callback function (callBack
) as parameters. - It initializes a variable
sum
to keep track of the sum of the numbers. The initial value is set to 0. - Using a
for
loop, it iterates through each element of thenumArr
array. - Inside the loop, it mistakenly adds the loop index (
i
) instead of the element at indexi
to thesum
. This should be corrected by changingsum += i;
tosum += numArr[i];
. - After the loop, it calls the provided callback function (
callBack
) and passes the calculatedsum
as an argument. - The purpose of the callback function is to allow customization of how the calculated sum is handled or processed. It can be provided by the caller of the
calculateSum
function.

Explanation:
- This
processSum
used to display the output by takingsum
as parameter with the “Number” Type. - Then it logs the output as a string template.

Explanation:
- The
numbers
array is defined with the values[1, 2, 3, 4, 5]
. - The
calculateSum
function is called with thenumbers
array and theprocessSum
function as arguments.
The Whole Code Explanation:

Explanation:
- The
calculateSum
function takes in an array of numbers (numArr
) and a callback function (callBack
) as arguments. - Inside the
calculateSum
function, we initialize a variable calledsum
to keep track of the sum and set it to 0. - Using a
for
loop, we go through each element in thenumArr
array. For each iteration, we mistakenly add the loop index (i
) to thesum
. Oops! We should fix that to add the actual elementnumArr[i]
instead. - Once we finish the loop, we call the provided callback function (
callBack
) and pass the calculatedsum
as an argument. - The purpose of the callback function is to process or handle the calculated sum in some way. In this case, we have another function called
processSum
that accepts the sum as a parameter. - The
processSum
function simply logs a message to the console, displaying the sum using string interpolation. - Finally, we create an array called
numbers
with the values[1, 2, 3, 4, 5]
. - We invoke the
calculateSum
function, passing thenumbers
array and theprocessSum
function as arguments. - The
calculateSum
function calculates the sum of the numbers in thenumbers
array (although it currently has a bug in the loop), and then calls theprocessSum
function, passing the calculated sum. - As a result, we should see the output in the console: “The sum is: 15” (assuming we fix the bug in the code).
2. Quick Differences Between JS and TS (with some funny comparisons)
a. TypeScript Types:
- Like a friendly librarian, TypeScript is all about organization and categorization.
- It asks you to explicitly label your variables, function parameters, and return types with specific types. It’s like putting books on well-labeled shelves, so you can find them easily and avoid mixing up romance novels with cookbooks.
- JavaScript, on the other hand, is like a free-spirited artist who loves surprises and embraces the unpredictable.
- It’s perfectly fine with you using a variable to hold a number one moment and a string the next. It’s like having a magical box where objects can shape-shift and change their nature whenever they want.
b. Type Annotations:
- TypeScript is the responsible friend who always carries a notebook with clearly written labels.
- It loves to jot down the types of variables, function parameters, and return values explicitly, so there’s no confusion or guessing games.
- It’s like having a personal assistant who keeps everything organized and reminds you of what type each thing is.
c. Type Inference:
- TypeScript also has a hidden talent for mind-reading.
- It can often figure out the type of a variable or function without you explicitly telling it.
- It’s like having a Sherlock Holmes detective who observes the clues (assigned values and context) and deduces the most probable types, saving you from writing down every single type annotation.
d. Additional Types:
- TypeScript introduces some fancy new types that JavaScript doesn’t have, like union types and intersection types.
- It’s like having a superhero toolkit with extra gadgets that allow you to combine types, creating powerful and flexible data structures.
- JavaScript, on the other hand, is content with its classic superhero powers, like being able to hold any type and adapting to the situation.
So there you have it!
I hope you liked this blog.
Share it with your friendly developers and clap this blog and comment what you think of this blog.
I’m open to suggestions as well. So make sure to let me know what are the programming-related things I can cover in future.
Thanks, and bye for now…👋💘