GyanMilega

What is TypeScript?

Author: Sagar Kudu
Sagar Kudu

what is typescript

An Introduction to TypeScript 📜

TypeScript is a superset of JavaScript that adds static typing to the language. This means you can define the type of a variable, function parameter, or object property, allowing the compiler to catch type-related errors before your code runs. This leads to more robust and maintainable code.


1. Basic Types 🔢

TypeScript includes several basic types to define variables.

  • number: Used for numeric values (integers and floats).

    let age: number = 25;
    
  • string: Used for text.

    let name: string = "Alice";
    
  • boolean: Used for true or false values.

    let isStudent: boolean = true;
    

Type Safety in Action:

If you try to assign a value of a different type to a variable, TypeScript will throw a compile-time error, preventing potential bugs.

let age: number = 25;
age = "hello"; // ❌ Error: Type 'string' is not assignable to type 'number'.

2. Functions with Types ✍️

TypeScript allows you to specify the types of a function's parameters and its return value. This ensures the function is called correctly and returns the expected data type.

Parameter & Return Types

You can define the type of each parameter and add a return type after the parameter list.

function add(a: number, b: number): number {
  return a + b;
}
  • add(2, 3): This call is valid and will return 5. ✅

  • add(2, "3"): This call will result in a compile-time error, as the second argument is a string, not a number. ❌

Optional Parameters

To make a function parameter optional, you can use a ? after the parameter name. Optional parameters must be placed after all required parameters.

function greet(name: string, age?: number): string {
  if (age) {
    return `${name} age is ${age}`;
  }
  return `Hello ${name}`;
}
  • greet("sagar", 20): This call is valid and returns "sagar age is 20".

  • greet("sagar"): This call is also valid because age is optional, and it will return "Hello sagar".


3. Interfaces for Objects 🧠

Interfaces are a powerful way to define the structure of an object. They ensure that any object that "implements" the interface has the specified properties and types.

interface User {
  name: string;
  age: number;
}

Now, any object declared with the User interface must have a name property of type string and an age property of type number.

const user: User = { name: 'Sagar', age: 25 };
// The object `user` conforms to the `User` interface.

4. Arrays & Objects 🗂️

Arrays

To define an array of a specific type, you can use the syntax type[].

let numbers: number[] = [1, 2, 3]; // An array of numbers.
let fruits: string[] = ["orange", "pineapple"]; // An array of strings.

Objects

You can also define the type of a plain object using an inline type declaration.

let person: { name: string; age: number } = {
  name: "Sagar",
  age: 25,
};

This is useful for simple objects, but for more complex or reusable object structures, it's better to use an interface.

Powered by wisp

Related Posts

📘TypeScript: Day 1 – Variables, Types & Functions

TypeScript: Variables, Types & Functions

Read Full Story

📘 TypeScript: Day 2 – Interfaces & Object Types

Let us consider a previous example: const car: { brand: string; year: number } = { brand: "Tata", year: 2025 }; That works fine, but imagine you have lots of objects with the same shape. Typing { brand: string; year: number } again and again is messy. 👉 This is where interfaces come in.

Read Full Story

📘 Day 3 – TypeScript: Union Types, Type Aliases & Enums

Unlock the power of **Union Types**, **Type Aliases**, and **Enums** in TypeScript to write cleaner, more flexible code. With **Union Types**, you can let variables hold multiple types, making your code more versatile. **Type Aliases** make complex types easier to read and reuse, while **Enums** help you manage related constants in a more organized way. These features give you the tools to build robust and maintainable applications.

Read Full Story

What is a Function in JavaScript? | Function Examples & Guide

Functions in a JavaScript. Learn what functions are in JavaScript with examples. This beginner-friendly guide covers function declarations, parameters, return values, and more.

Read Full Story
Loading...
© Gyan Milega 2025