GyanMilega

📘TypeScript: Day 1 – Variables, Types & Functions

Author: Sagar Kudu
Sagar Kudu

TypeScript: Variables, Types & Functions

1. Variables with Types

In TypeScript, you declare the type after a :.

let username: string = "Sagar";
let age: number = 25;
let isStudent: boolean = true;

❌ If you assign the wrong type:

age = "30"; // Error: Type 'string' is not assignable to type 'number'

2. Arrays

You can say what type the array holds:

let numbers: number[] = [1, 2, 3];
let fruits: string[] = ["pineapple", "kiwi"];

❌ This won’t work:

numbers.push("hello"); // Error

3. Objects

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

4. Functions with Types

You type both parameters and the return value:

function add(a: number, b: number): number {
  return a + b;
}

console.log(add(2, 3));   // ✅ 5
console.log(add(2, "3")); // ❌ Error

Optional parameters:

function greet(name: string, age?: number): string {
  return age ? `${name} is ${age}` : `Hello ${name}`;
}

console.log(greet("Sagar"));       // Hello Sagar
console.log(greet("Sagar", 30));   // Sagar is 25

📝 Day 1 Exercises

Try these in the TypeScript Playground:

  1. Create a variable city (string), population (number), isCapital (boolean).

  2. Make an array of numbers called scores.

  3. Define an object car with { brand: string; year: number }.

  4. Write a function multiply That takes two numbers and returns a number.

Solution:

✅ Exercise 1: Create a variable city (string), population (number), isCapital (boolean).

const city: string = 'Palghar'
const population: number = 10000000
const isCapital: boolean = true

console.log(city)        // Palghar
console.log(population)  // 10000000
console.log(isCapital)   // true

✅ Exercise 2: Make an array of numbers called scores.

const scores: number[] = [1, 2, 3, 4, 5]

console.log(scores) // [1, 2, 3, 4, 5]

✅ Exercise 3: Define an object car with { brand: string; year: number }.

const car: { brand: string, year: number } = {
  brand: 'Tata',
  year: 2025
}

console.log(car)
// { brand: 'Tata', year: 2025 }

⚠️ Exercise 4: Write a function multiply That takes two numbers and returns a number.

function multiply(a: number, b: number): number {
  return a * b
}

const output = multiply(5, 3)
console.log(output) // 15

Also Learn

Powered by wisp

Related Posts

What is 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.

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