Series: TypeScript from Basics to Advanced
Part: 1 — Introduction to TypeScript
Level: Beginner
Reading time: 8–10 minutes
Introduction
If you have worked with JavaScript, you may have encountered code like this:
function add(a, b) {
return a + b;
}
At first glance, this looks perfectly fine.
But what happens when someone calls:
add(10, "20");
JavaScript allows this and produces:
"1020"
This behavior can sometimes lead to unexpected bugs, especially in large applications.
This is one of the problems TypeScript helps solve.
TypeScript is a programming language built on top of JavaScript that adds static type checking and other developer features to JavaScript development.
In this article, we'll understand what TypeScript is, how it differs from JavaScript, how types work, what compile-time and runtime mean, and how TypeScript code becomes JavaScript.
What Is TypeScript?
TypeScript is a superset of JavaScript developed by Microsoft.
Being a superset means that valid JavaScript is generally also valid TypeScript.
For example:
const name = "Sundar";
console.log(name);
This is valid TypeScript even though we haven't explicitly added any types.
TypeScript adds features such as:
-
Static typing
-
Type inference
-
Interfaces
-
Generics
-
Type aliases
-
Enums
-
Advanced type manipulation
-
Better tooling and editor support
The most important feature to understand initially is static typing.
TypeScript vs JavaScript
The biggest difference is how they handle types.
Consider JavaScript:
function add(a, b) {
return a + b;
}
JavaScript doesn't require us to specify what a and b should be.
We can do:
add(10, 20);
and:
add("Hello", "World");
Both are valid JavaScript.
Now look at TypeScript:
function add(a: number, b: number): number {
return a + b;
}
We're telling TypeScript:
-
amust be a number -
bmust be a number -
the function returns a number
Now this is valid:
add(10, 20);
But this produces a TypeScript error:
add(10, "20");
TypeScript can identify the problem before the program runs.
That's the main idea behind TypeScript.
What Is Static Typing?
Static typing means that types are checked during development/compilation rather than waiting until the program is running.
For example:
let age: number = 25;
Because age is declared as a number, this is invalid:
age = "twenty-five";
TypeScript will report an error.
Type 'string' is not assignable to type 'number'.
This helps developers catch many mistakes early.
Type Annotation
A type annotation is when we explicitly tell TypeScript what type a value should have.
For example:
let username: string = "Sundar";
let age: number = 25;
let isDeveloper: boolean = true;
The syntax is:
variableName: type
For example:
let age: number;
Here, number is the type annotation.
Type annotations can be used with:
-
Variables
-
Function parameters
-
Function return values
-
Objects
-
Arrays
-
Function types
-
Class properties
For example:
function greet(name: string): string {
return `Hello ${name}`;
}
Here:
name: string
is a parameter type annotation.
And:
): string
specifies the return type.
Type Inference
One of the great things about TypeScript is that you don't always need to explicitly write types.
TypeScript can often infer the type automatically.
For example:
let username = "Sundar";
TypeScript understands that:
username → string
Similarly:
let age = 25;
TypeScript infers:
age → number
And:
let isLoggedIn = true;
TypeScript infers:
isLoggedIn → boolean
So you don't necessarily need to write:
let username: string = "Sundar";
let age: number = 25;
let isLoggedIn: boolean = true;
You can usually write:
let username = "Sundar";
let age = 25;
let isLoggedIn = true;
TypeScript understands the types automatically.
Type Annotation vs Type Inference
Type annotation:
let age: number = 25;
We explicitly tell TypeScript the type.
Type inference:
let age = 25;
TypeScript figures out the type.
A good TypeScript developer knows when to use each approach.
Compile-Time vs Runtime
This is an important concept to understand.
Compile-Time
Compile-time is when TypeScript analyzes your code and checks for type-related problems.
For example:
let age: number = 25;
age = "hello";
TypeScript detects the problem during development/compilation.
You don't have to run the application to discover this particular type error.
Runtime
Runtime is when your JavaScript application is actually executing.
For example:
const result = 10 / 0;
console.log(result);
JavaScript executes this code at runtime.
The important thing to understand is:
TypeScript's type system primarily helps you before runtime. It does not automatically validate data at runtime.
Consider an API response:
interface User {
name: string;
age: number;
}
You may expect the API to return:
{
"name": "Sundar",
"age": 25
}
But if an external API actually returns:
{
"name": "Sundar",
"age": "twenty-five"
}
TypeScript doesn't automatically stop that response.
Why?
Because TypeScript types are removed when the code is compiled to JavaScript.
For runtime validation, applications often use libraries such as Zod.
We'll cover this in a later article.
How Does TypeScript Become JavaScript?
Browsers don't directly execute TypeScript.
For example, this code:
const message: string = "Hello TypeScript";
contains TypeScript syntax.
The browser doesn't understand the : string annotation.
TypeScript therefore needs to be transpiled/compiled into JavaScript.
Conceptually:
TypeScript
↓
TypeScript Compiler
↓
JavaScript
↓
Browser / Node.js
For example:
TypeScript
const message: string = "Hello TypeScript";
console.log(message);
JavaScript
const message = "Hello TypeScript";
console.log(message);
The type annotation has disappeared.
This is why TypeScript doesn't change the JavaScript runtime itself. It provides additional checks and developer tooling before the JavaScript runs.
What Is the TypeScript Compiler?
The TypeScript compiler is commonly called tsc.
It is responsible for processing TypeScript code and producing JavaScript.
For example:
tsc app.ts
If app.ts contains:
const name: string = "Sundar";
console.log(name);
TypeScript can generate JavaScript from it.
You can also configure the compiler using:
tsconfig.json
For example:
{
"compilerOptions": {
"target": "ES2022",
"strict": true
}
}
The tsconfig.json file controls many aspects of TypeScript compilation and type checking.
We'll explore tsconfig.json in a dedicated article later in this series.
What Are .ts Files?
A .ts file is a TypeScript source file.
For example:
app.ts
user.ts
api.ts
utils.ts
You can write normal TypeScript code inside these files:
function add(a: number, b: number): number {
return a + b;
}
What Are .tsx Files?
A .tsx file is a TypeScript file that supports JSX.
This is especially important when working with React.
For example:
function Welcome() {
return <h1>Hello TypeScript</h1>;
}
Because the file contains JSX, we typically use:
Welcome.tsx
instead of:
Welcome.ts
A simple way to remember:
.ts → TypeScript
.tsx → TypeScript + JSX
For React applications, you'll commonly see:
src/
├── components/
│ ├── Button.tsx
│ └── UserCard.tsx
├── pages/
│ └── Home.tsx
├── types/
│ └── user.ts
└── utils/
└── format.ts
Why Do Developers Use TypeScript?
As applications become larger, JavaScript code can become harder to maintain.
Imagine a function used by 50 different components.
If the expected data structure changes, finding every place affected by that change can become difficult.
TypeScript can help by describing the expected structure.
For example:
interface User {
id: number;
name: string;
email: string;
}
Now when working with a User, your editor can provide useful information about its properties.
const user: User = {
id: 1,
name: "Sundar",
email: "sundar@example.com"
};
If you accidentally write:
const user: User = {
id: "1",
name: "Sundar",
email: "sundar@example.com"
};
TypeScript catches the mistake.
Benefits of TypeScript
1. Catch Errors Earlier
Many type-related bugs can be identified before the application runs.
2. Better Developer Experience
Editors such as VS Code can provide:
-
Autocomplete
-
Type information
-
Error highlighting
-
Safer refactoring
-
Navigation to definitions
3. Easier Maintenance
Types can document the expected structure of your application.
Instead of guessing what a function expects, you can inspect its type.
4. Safer Refactoring
When you change a type or function signature, TypeScript can identify code that may have been affected.
5. Better Large-Scale Development
TypeScript becomes particularly useful as applications grow and multiple developers work on the same codebase.
Is TypeScript a Replacement for JavaScript?
No.
TypeScript builds on JavaScript.
You still need to understand JavaScript to use TypeScript effectively.
In fact, TypeScript doesn't introduce a completely separate runtime environment.
Instead, it adds a type system and other development features around JavaScript.
Think of it like this:
JavaScript
+
Type System
+
Developer Tooling
+
Additional TypeScript Features
↓
TypeScript
A Simple Mental Model
If you're just starting with TypeScript, remember these four ideas:
JavaScript
Write code → Run code → Discover some errors
TypeScript
Write code
↓
TypeScript checks types
↓
Fix type errors
↓
Compile to JavaScript
↓
Run JavaScript
TypeScript doesn't eliminate all bugs.
Instead, it catches an important category of bugs before your code reaches runtime.
TypeScript in Modern Development
TypeScript is widely used in modern JavaScript ecosystems.
You'll commonly encounter it when working with:
-
React
-
Next.js
-
Node.js
-
Express
-
React Native
-
NestJS
-
Backend APIs
-
Full-stack applications
For example, a modern full-stack application might look like:
React / React Native
↓
TypeScript
↓
API
↓
Node.js + TypeScript
↓
Database
Having a good understanding of TypeScript therefore makes it easier to work across the entire JavaScript ecosystem.
What You Should Know After This Article
After completing this article, you should understand:
-
What TypeScript is
-
How TypeScript relates to JavaScript
-
What static typing means
-
What type annotations are
-
What type inference is
-
The difference between compile-time and runtime
-
What the TypeScript compiler does
-
What
.tsfiles are -
What
.tsxfiles are -
Why TypeScript is useful for large applications
These concepts form the foundation for everything else we'll learn.