0
0
Typescriptprogramming~3 mins

Why Parameters type in Typescript? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your functions could warn you before you make a mistake with the wrong input?

The Scenario

Imagine you are writing a function that takes some inputs, but you don't specify what kind of inputs it should accept. You just write the code and hope the user passes the right values.

For example, a function that adds two numbers but you don't say they must be numbers.

The Problem

Without telling the function what types of inputs to expect, mistakes happen easily. Someone might pass a word instead of a number, and your program breaks or gives wrong answers.

It becomes hard to find these errors because the code doesn't clearly say what it needs.

The Solution

By using parameter types, you tell the function exactly what kind of inputs it should get. This helps catch mistakes early, before running the program.

It also makes your code easier to understand and safer to use.

Before vs After
Before
function add(a, b) {
  return a + b;
}
After
function add(a: number, b: number): number {
  return a + b;
}
What It Enables

It enables writing clear, safe functions that catch input mistakes early and help others understand how to use them correctly.

Real Life Example

Think of a calculator app that only works if you enter numbers. Parameter types make sure the calculator never tries to add words or other wrong inputs.

Key Takeaways

Parameter types tell functions what inputs to expect.

This prevents errors by catching wrong inputs early.

It makes code clearer and safer to use.