What is TypeScript?
TypeScript is a programming language built on top of JavaScript that adds static typing capabilities. TypeScript allows developers to define in advance what type of data variables, functions, objects, and other structures should contain. In simple terms, some errors in JavaScript may only appear after the program starts running. TypeScript helps detect certain type-related errors before the application runs by checking the code in advance. For example, if a function is expected to receive a number, TypeScript can show an error during development if text is accidentally passed to that function. This feature is especially useful in large projects, when working in teams, and when using modern Frontend technologies such as React and Next.js.
What Is TypeScript Used For?
TypeScript is mainly used to make JavaScript projects clearer, easier to manage, and safer. In a small JavaScript project, it may be easy to remember what type of data a variable stores. As the project grows, however, developers may need to work with hundreds of functions, components, objects, and API responses. TypeScript makes it possible to clearly define what types of data these structures are expected to work with.
For example, user information can be defined like this:
type User = {
name: string;
age: number;
active: boolean;
};
Here, name must contain text, age must contain a number, and active must be either true or false. If a developer later tries to assign text to the age field by mistake, TypeScript can show it as an error.
How Does TypeScript Work?
Browsers traditionally do not run TypeScript code directly. During the build or compilation stage, TypeScript code is converted into JavaScript, and most type information is removed from the runtime code.
For example, this TypeScript code:
const age: number = 25;
is converted into JavaScript where the main logic remains:
const age = 25;
The : number part is not part of the runtime behavior of the application. It is mainly used to check the code before execution. For this reason, the type safety provided by TypeScript mainly works during development and build stages.
How Is TypeScript Different from JavaScript?
JavaScript is a dynamically typed programming language. The type of a variable is determined by the value it holds while the program is running.
For example:
let value = 10;
value = "Hello";
JavaScript allows this.
In TypeScript, however, you can define that the variable should contain only a number:
let value: number = 10;
value = "Hello";
In this case, TypeScript will show the second line as a type error. The main difference is that JavaScript handles runtime behavior, while TypeScript adds an additional type system and development tools on top of JavaScript. TypeScript is not a completely separate ecosystem that replaces JavaScript. Instead, it is built on JavaScript syntax and capabilities.
What Is Static Typing?
Static Typing is an approach in which the types of variables and other program elements are checked before the program runs.
For example:
let username: string = "Aysel";
let age: number = 24;
let active: boolean = true;
Here, the type of data each variable can contain is defined in advance.
If the developer writes:
age = "twenty four";
TypeScript can detect this mismatch.
This feature can help reduce accidental type-related errors, especially in large projects.
What Is Type Annotation?
Type Annotation is the explicit declaration of the type of a variable, function parameter, or return value.
For example:
function calculatePrice(
price: number,
quantity: number
): number {
return price * quantity;
}
Here, price and quantity must both be numbers. The : number shown after the function parameters indicates that the result returned by the function must also be a number. This makes it clearer to other developers reading the code what type of data the function works with.
What Is Type Inference?
In TypeScript, you do not always need to manually write the type of every variable. In many cases, TypeScript can automatically determine the type based on the assigned value.
For example:
const course = "Frontend";
Even though string is not explicitly written, TypeScript can determine that the course variable contains text. This process is called Type Inference. Type Inference helps prevent the code from becoming unnecessarily overloaded with type annotations.
What Are the Main Data Types in TypeScript?
TypeScript works with JavaScript data types such as:
- string;
- number;
- boolean;
- object;
- null;
- undefined.
In addition, the TypeScript type system includes concepts such as:
- any;
- unknown;
- never;
- Union Types;
- Literal Types.
For example:
let name: string = "Murad";
let age: number = 22;
let student: boolean = true;
For an Array, you can write:
const technologies: string[] = [
"HTML",
"CSS",
"TypeScript"
];
Here, the Array is expected to contain only string values.
What Is any?
The any type allows TypeScript's type checking to be largely disabled for a specific value.
For example:
let data: any = 10;
data = "Hello";
data = true;
This code can be accepted. Although any can be useful in certain situations, excessive use weakens one of the main advantages of TypeScript: type safety. For this reason, it is generally better to use more specific types whenever possible.
What Is unknown?
The unknown type can be used when the type of incoming data is not known in advance. Compared with any, unknown is a safer approach because the value must be checked before performing operations on it.
For example:
function printValue(value: unknown) {
if (typeof value === "string") {
console.log(value.toUpperCase());
}
}
Here, after the typeof check, TypeScript understands that value is a string inside that block. This process is called Type Narrowing.
What Is a Union Type?
Sometimes a variable may accept one of several different types. In this case, a Union Type can be used.
For example:
let id: string | number;
id = 10;
id = "USR-10";
Here, id can be either a string or a number. Union Types are widely used when working with API data, component props, and different application states.
What Are Type and Interface?
In TypeScript, type and interface can be used to describe the structure of objects.
For example:
interface User {
id: number;
name: string;
email: string;
}
Then:
const user: User = {
id: 1,
name: "Aysel",
email: "aysel@example.com"
};
TypeScript checks whether the user object matches the structure required by the User interface.
A similar structure can also be created with type:
type Product = {
id: number;
title: string;
price: number;
};
Although type and interface can be used in many similar situations, there are differences in their capabilities and extension behavior.
What Is an Optional Property?
Sometimes a specific property of an object is not required.
In such cases, the ? symbol can be used.
interface User {
name: string;
phone?: string;
}
Here, name is required, while phone is optional. This feature is especially useful when working with real API data and forms.
What Are Generics?
Generics allow developers to create reusable functions and components that can work with different types while preserving type safety.
For example:
function getFirst<T>(items: T[]): T {
return items[0];
}
This function can work with arrays of text, numbers, or other data types.
getFirst<string>(["HTML", "CSS"]);
getFirst<number>([10, 20, 30]);
Here, T is not limited to one specific type. The function can work with different types while still preserving information about the type being used. Generics are especially useful for creating reusable components in large projects.
What Is Type Narrowing?
Type Narrowing is the process by which TypeScript determines which specific type is being used in a particular situation from a broader type.
For example:
function format(value: string | number) {
if (typeof value === "string") {
return value.toUpperCase();
}
return value.toFixed(2);
}
Inside the if block, TypeScript knows that value is a string. In the other part of the function, it determines that value is a number. This approach makes it possible to work safely with Union Types.
How Do TypeScript and APIs Work Together?
In modern Frontend projects, data is often retrieved from a Backend through an API. For example, if user information comes from an API, its expected structure can be defined inside the application:
interface User {
id: number;
name: string;
email: string;
}
Functions that work with the API can then use this type. This makes it clearer what kind of data is expected throughout the codebase.
However, there is an important point:
TypeScript does not automatically guarantee at runtime that data received from an API actually matches the defined structure. If external API data needs to be validated reliably, runtime validation must be implemented separately.
How Is TypeScript Used with React?
TypeScript is widely used in React projects.
Defining the props of React components is a simple example:
type ButtonProps = {
text: string;
disabled?: boolean;
};
function Button({
text,
disabled
}: ButtonProps) {
return (
<button disabled={disabled}>
{text}
</button>
);
}
Here, the allowed props for the component are defined in advance. If an incompatible type is passed instead of text, TypeScript can show an error during development. As the number of components increases, this feature helps make large projects easier to manage.
What Is TSX?
TSX is a file format that allows JSX syntax to be used with TypeScript. React components written with JavaScript often use the .jsx file extension, while TypeScript-based React components usually use .tsx.
For example:
function Header() {
return <h1>JET Academy</h1>;
}
TSX is commonly used in React and Next.js projects.
Is TypeScript Used with Next.js?
Yes.
Next.js supports TypeScript, and TypeScript is widely used in modern Next.js projects.
With TypeScript, developers can define types for:
- components;
- server functions;
- route parameters;
- API data;
- other structures within the application.
This approach can help keep code clearer and more manageable, especially in large Next.js projects.
Is TypeScript Used in Backend Development?
Yes. TypeScript is not intended only for Frontend Development. Backend applications can also be built with TypeScript using Node.js and server frameworks built on top of it. This means TypeScript can be used in both the Frontend and Backend parts of the same project. This approach can help manage shared type structures more systematically in Full-Stack JavaScript/TypeScript projects.
How Does TypeScript Help with Debugging?
TypeScript does not eliminate all programming errors. Logic errors, incorrect business rules, API problems, and runtime errors may still occur. However, TypeScript helps detect certain type mismatches before the code runs.
For example, the following issues may become visible during development:
- accessing a property that does not exist;
- passing the wrong type to a function;
- using an unexpected value.
Therefore, TypeScript does not completely remove the need for Debugging, but it can help identify some errors earlier.
What Are the Advantages of TypeScript?
TypeScript can make code structure clearer in large and long-term projects.
Type information shows:
- which parameters a function accepts;
- which properties an object contains;
- what kind of data a component works with.
IDEs can also use TypeScript type information to provide:
- better autocomplete;
- safer refactoring;
- more useful error warnings.
TypeScript can also make it easier for team members to read code and understand the expected data structures shared between different parts of a project.
Are There Any Disadvantages to TypeScript?
TypeScript requires an additional learning stage.
For someone who is just learning JavaScript, concepts such as:
- type;
- interface;
- generics;
- union;
- narrowing
may make the topic seem more complex.
TypeScript can also add extra steps to the project's build and configuration processes. In addition, TypeScript does not prevent all runtime problems. The real structure of data coming from a server or user must still be validated separately. For this reason, TypeScript is not a technology that replaces JavaScript knowledge. A strong understanding of JavaScript makes learning TypeScript significantly easier.
Where Should You Start Learning TypeScript?
Before moving on to TypeScript, it is better to have a strong understanding of JavaScript fundamentals.
Useful JavaScript topics include:
- variables;
- data types;
- functions;
- Arrays and Objects;
- Scope;
- DOM;
- async/await;
- modules;
- working with APIs.
After that, you can move on to TypeScript topics such as:
- Type Annotations;
- Type Inference;
- Union Types;
- Interfaces;
- Type Aliases;
- Generics;
- Narrowing;
- Utility Types.
Developers who use React can then learn how to type:
- component props;
- state;
- events;
- hooks;
- API data
with TypeScript. After that, building practical projects with TSX and Next.js can help strengthen your TypeScript knowledge.
For more detailed information about the recommended learning order for Frontend Development, you can read JET Academy's article “How to Start Becoming a Frontend Developer?”
If you want to learn TypeScript, JavaScript, React, Redux Toolkit, Next.js, and other modern Frontend technologies through practical projects, you can explore JET Academy's Frontend Programming Course.
