Easy TypeScript Examples for Beginners: Learn Step by Step

Logan Jul 13, 2026

Are you a beginner eager to dip your toes into the world of TypeScript, but feeling overwhelmed by all the new syntax and concepts? You're not alone! TypeScript, a statically typed superset of JavaScript, offers powerful features to catch mistakes early and enable rich tooling. Let's embark on a journey to explore some beginner-friendly TypeScript examples and demystify this exciting language.

Learn TypeScript – The Ultimate Beginners Guide
Learn TypeScript – The Ultimate Beginners Guide

Before we dive in, make sure you have Node.js installed. You can verify this by running node -v and npm -v in your terminal. Now, let's set up a new directory for our TypeScript files and initialize it with npm. Open your terminal, navigate to your desired project location, and run:

Advance guide for Typescript utility types
Advance guide for Typescript utility types

mkdir typescript-examples
cd typescript-examples
npm init -y
npm install --save-dev typescript ts-node

Our First TypeScript File

With our project set up, let's create our first TypeScript file. Open your favorite code editor, create a file named hello.ts, and let's start by writing a simple "Hello, World!" program.

TypeScript Tutorial For Beginners
TypeScript Tutorial For Beginners

TypeScript recognizes JavaScript syntax, so you can write this program the same way you would in JavaScript:

```typescript console.log("Hello, World!") ```

Understanding TypeScript File Extensions

TypeScript Course for Beginners - Learn TypeScript from Scratch!
TypeScript Course for Beginners - Learn TypeScript from Scratch!

You might be wondering why we're using the .ts extension instead of .js. TypeScript files are transpiled (converted) into JavaScript files before they can be run. So, let's transpile our file using the TypeScript compiler (tsc) and run the compiled JavaScript file.

tsc hello.ts
node hello.js

Basic Types in TypeScript

Now that we've seen how TypeScript is used, let's explore some basic types. Open your hello.ts file and let's declare some variables with explicit types:

the different types of computers are shown in this diagram, and each one has its own language
the different types of computers are shown in this diagram, and each one has its own language

```typescript let message: string; message = "Hello, World!"; let age: number; age = 25; let isAdmin: boolean; isAdmin = true; ```

The above snippet demonstrates how to declare variables with TypeScript's basic types: strings, numbers, and booleans.

Functions: Another Core Building Block

Functions are another essential component of programming. Let's create a simple function to greet a person and explore TypeScript's function syntax.

How to set up a TypeScript project
How to set up a TypeScript project

In the hello.ts file, define a function named greet with a parameter name of type string.

```typescript function greet(name: string): void { console.log(`Hello, ${name}!`); } ```

To call this function, simply add greet("Alice") anywhere in your file. The void keyword tells TypeScript that this function does not return any value.

🚀 Tech Resources That Can Change Your Learning Journey
🚀 Tech Resources That Can Change Your Learning Journey
TypeScript for Beginners: Learn the Basics of This Powerful JavaScript Superset
TypeScript for Beginners: Learn the Basics of This Powerful JavaScript Superset
10 Best TypeScript Books For Beginners To Learn TypeScript
10 Best TypeScript Books For Beginners To Learn TypeScript
TypeScript Tutorial: A Simple Guide for Beginners
TypeScript Tutorial: A Simple Guide for Beginners
8 TypeScript Utility Types That Will Save You Hours
8 TypeScript Utility Types That Will Save You Hours
TypeScript Tutorial
TypeScript Tutorial
the typescript simple types are shown in this diagram
the typescript simple types are shown in this diagram
the book cover for learn react with typescript 3, which includes an orange and blue background
the book cover for learn react with typescript 3, which includes an orange and blue background
TypeScript Partial: Simplify Object Updates with Optional Properties
TypeScript Partial: Simplify Object Updates with Optional Properties
Online TypeScript Compiler: Code Anywhere, Instantly
Online TypeScript Compiler: Code Anywhere, Instantly
#typescript #javascript #webdevelopment #programming #softwareengineering #codetips #frontend | Sonu Sindhu
#typescript #javascript #webdevelopment #programming #softwareengineering #codetips #frontend | Sonu Sindhu
Key Features of TypeScript
Key Features of TypeScript
👉 TypeScript Utility Types Mastery 2026 (Partial, Pick, Omit Explained)
👉 TypeScript Utility Types Mastery 2026 (Partial, Pick, Omit Explained)
Creating NPM Package: Your React TypeScript Guide to Create, Test, and Publish NPM Libraries
Creating NPM Package: Your React TypeScript Guide to Create, Test, and Publish NPM Libraries
TypeScript Beginner to Advanced Guide
TypeScript Beginner to Advanced Guide
Free Typing Practice Sheet for Beginners
Free Typing Practice Sheet for Beginners
Free Pack: Start With the Basics
Free Pack: Start With the Basics
a computer screen with the words typescript on it
a computer screen with the words typescript on it
Top 10 Rich Text Editors for React + TypeScript (2025 Guide)
Top 10 Rich Text Editors for React + TypeScript (2025 Guide)
a black background with text that reads basic type 2 string the string types all text data
a black background with text that reads basic type 2 string the string types all text data

Optional Parameters and Default Values

TypeScript allows us to define optional parameters with a question mark (?). We can also assign default values to parameters.

```typescript function greet(name: string = "Friend", greeting: string = "Hello"): void { console.log(`${greeting}, ${name}!`); } ```

Now, you can call this function with fewer or more arguments, like greet("Alice", "Hi") or greet().

Arrays and Tuples

TypeScript enables us to define arrays with a specific type. Let's create an array of strings:

```typescript let friends: string[] = ["Bob", "Charlie", "Diana"]; ```

But what if we want an array that contains both strings and numbers? This is where tuples come in handy. Tuples allow us to define arrays with different types at specific indices:

```typescript let pair: [string, number]; pair = ["Eve", 30]; ```

Tuples can have methods too! Try defining a tuple type with methods, like so:

```typescript interface PairTuple { [index: number]: string | number; length: number; } function getLength(pair: PairTuple) { return pair.length; } ```

Now, you can call getLength(pair) to return the length of your tuple.

Any and Void

TypeScript includes two special types: any and void. The any type represents any JavaScript value and is useful when working with existing JavaScript code. The void type is used to represent the absence of a type, often used to declare functions that do not return a value.

Here's an example of using the any type and void type in a function:

```typescript let notSure: any; notSure = 20; notSure = "twenty"; notSure = true; function warnUser(): void { console.log("This is my warning message"); } ```

Start exploring more examples and features of TypeScript, such as interfaces, enums, and classes, to deepen your understanding. Happy coding!