Sitemap
Kavyabhat

Kavyabhat

Front-End Developer | Web Enthusiast

TypeScript Basics for JavaScript Developers

3 min readMay 21, 2026

--

“TypeScript is no longer just a ‘nice-to-have’ skill. Many companies now expect frontend developers to write type-safe and scalable applications.”

Press enter or click to view image in full size

Why TypeScript?

JavaScript Problem

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

console.log(add(10, "20"));

output : 1020

JavaScript does not check data types.
Instead of adding numbers, it converts 10 into a string and joins them together.

This can create unexpected bugs in large applications.

TypeScript Solution

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

console.log(add(10, 20));

Here:

  • a: number → parameter must be a number
  • b: number → parameter must be a number
  • : number after function → return value must be a number
add(10, "20");

TypeScript immediately shows an error before running the code.

That is the biggest advantage of TypeScript.

Variables and Types

JavaScript

let username = "Kavya";

JavaScript automatically guesses the type.

TypeScript

let username: string = "Kavya";

Here:

  • string defines the variable type
  • TypeScript ensures only strings can be assigned
username = 100;

TypeScript shows: Type ‘number’ is not assignable to type ‘string’

Arrays

JavaScript

let skills = ["React", 10, true];

Different data types are allowed.

TypeScript

let skills: string[] = ["React", "TypeScript"];

string[] means this array can only contain strings.

skills.push(10);

TypeScript prevents this mistake.

Objects

JavaScript

const user = {
name: "Kavya",
age: 25
};

No validation exists.

TypeScript

const user: { name: string; age: number } = {
name: "Kavya",
age: 25
};

TypeScript checks:

  • name must be string
  • age must be number
age: "25"

This gives an error.

Interfaces

Interfaces help define reusable object structures.

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

const user1: User = {
name: "Kavya",
age: 25
};

Instead of repeating object types everywhere, we create one reusable structure.

Get Kavyabhat’s stories in your inbox

Join Medium for free to get updates from this writer.

Benefits:

  • Cleaner code
  • Easier maintenance
  • Better scalability

Optional Properties

interface Employee {
name: string;
company?: string;
}

? means optional.

const emp1 = {
name: "Kavya"
};

const emp2 = {
name: "Kavya",
company: "Infosys"
};

Both work correctly.

Union Types

Sometimes variables can store multiple types.

let id: string | number;

id = 101;
id = "EMP101";

let id: string | number;

id = 101;
id = “EMP101”;

Useful in APIs and database IDs.

Functions in TypeScript

function greet(name: string): string {
return `Hello ${name}`;
}
  • name: string → only string allowed
  • return type : string
greet(100);

TypeScript prevents invalid input.

Any vs Unknown

Any

let value: any = 10;

value = "Hello";
value = true;

any disables TypeScript checking.

Too much any makes TypeScript useless.

Unknown

let value: unknown = "Hello";

unknown is safer because TypeScript forces validation before usage.

if (typeof value === "string") {
console.log(value.toUpperCase());
}

Generics

Generics create reusable functions.

function identity<T>(value: T): T {
return value;
}

identity<string>("Hello");
identity<number>(100);

<T> represents a placeholder type.

Benefits:

  • Reusable code
  • Type safety
  • Flexible functions

TypeScript does not replace JavaScript.
It improves JavaScript by adding:

  • type safety
  • better tooling
  • fewer bugs
  • improved scalability

For JavaScript developers, TypeScript feels familiar but much safer for building modern applications.

--

--

Kavyabhat
Kavyabhat

Written by Kavyabhat

Front-End Developer | Web Enthusiast