runtime.boot

Full Stack Systems
Production Mindset

CodeWithMihir

Engineering thoughtful products from interface to infrastructure.

CodeWithMihir

TypeScript Tutorial

TypeScript Basic Types: string, number, boolean, null, undefined, any, unknown, never, and void

Learn the basic TypeScript types every beginner should know, including string, number, boolean, null, undefined, any, unknown, never, and void.

Welcome back! I am Mihir, and in this lesson we will learn the basic types in TypeScript.

In the previous lessons, you learned type annotations and type inference. Now it is time to understand the common types you will write every day.

TypeScript uses types to describe what kind of value a variable, function parameter, object property, or return value can hold.


Why Basic Types Matter

Basic types help TypeScript catch mistakes before your code runs.

Example:

let courseName: string = "TypeScript";

courseName = 100;

TypeScript warns because courseName should be a string, not a number.

This is the main benefit of TypeScript: it helps you find problems while writing code.


string

Use string for text values.

let firstName: string = "Mihir";
let course: string = "TypeScript";
let message: string = `Welcome to ${course}`;

A string can use:

  • double quotes
  • single quotes
  • template literals

Invalid:

let username: string = 101;

101 is a number, so TypeScript gives an error.


number

Use number for integers and decimals.

let age: number = 25;
let price: number = 1499.99;
let temperature: number = -4;

TypeScript does not have separate int or float types.

Both of these are number:

let quantity: number = 10;
let rating: number = 4.8;

Invalid:

let total: number = "999";

Even if "999" looks numeric, it is still a string.


boolean

Use boolean for true or false.

let isLoggedIn: boolean = true;
let hasAccess: boolean = false;
let isPublished: boolean = true;

Booleans are commonly used for conditions:

if (isLoggedIn) {
  console.log("Show dashboard");
}

Invalid:

let isActive: boolean = "yes";

TypeScript only allows true or false for boolean values.


null

null means an intentional empty value.

let selectedUser: null = null;

Usually, you do not use null alone. You combine it with another type:

type User = {
  id: number;
  name: string;
};

let selectedUser: User | null = null;

selectedUser = {
  id: 1,
  name: "Mihir",
};

This means selectedUser can be a User or null.


undefined

undefined means a value has not been assigned yet.

let email: undefined = undefined;

Like null, you usually combine undefined with another type:

let discountCode: string | undefined;

discountCode = "SAVE20";
discountCode = undefined;

Optional object properties also include undefined:

type Product = {
  title: string;
  description?: string;
};

Here, description may be a string or may be missing.


any

any means TypeScript should stop checking that value.

let data: any = "Mihir";

data = 25;
data = true;
data.toUpperCase();

This is flexible, but dangerous.

With any, TypeScript allows almost anything, even code that may crash at runtime.

Use any only when you truly have no better option.


unknown

unknown means a value can be anything, but you must check it before using it.

let value: unknown = "TypeScript";

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

This is safer than any.

With unknown, TypeScript forces you to narrow the type first.


void

void is usually used for functions that do not return a useful value.

function logMessage(message: string): void {
  console.log(message);
}

This function performs an action but does not return data.

Common void examples:

  • logging
  • event handlers
  • sending emails
  • updating UI state

never

never represents a value that should never happen.

Example: a function that always throws an error:

function throwError(message: string): never {
  throw new Error(message);
}

Another example is exhaustive checking:

type Status = "success" | "error";

function handleStatus(status: Status) {
  switch (status) {
    case "success":
      return "Done";
    case "error":
      return "Failed";
    default:
      const impossible: never = status;
      return impossible;
  }
}

You will understand never more clearly when we learn discriminated unions and exhaustive checks.


Basic Types Quick Table

TypeUsed ForExample
stringtext"Mihir"
numberintegers and decimals25, 4.8
booleantrue or false valuestrue
nullintentional empty valuenull
undefinednot assigned or missingundefined
anydisables type checkinglet data: any
unknownsafe unknown valuelet value: unknown
voidno useful return valuefunction log(): void
neverimpossible valuefunction that always throws

Type Inference with Basic Types

You do not always need annotations.

let course = "TypeScript";
let lessons = 30;
let completed = false;

TypeScript infers:

  • course is string
  • lessons is number
  • completed is boolean

This is clean and readable.

Use explicit annotations when the type is not obvious:

let selectedCourseId: number | null = null;

Common Beginner Mistakes

Using Capitalized Primitive Types

Prefer lowercase:

let name: string = "Mihir";
let age: number = 25;
let active: boolean = true;

Avoid:

let name: String = "Mihir";
let age: Number = 25;
let active: Boolean = true;

For normal primitive values, use string, number, and boolean.

Overusing any

let response: any = getData();

This removes safety. Prefer unknown when you need to validate the value first.

Forgetting null and undefined

If a value can be missing, include that in the type:

let userId: number | null = null;
let token: string | undefined;

Practice

Add types to this user profile:

let username: string = "mihir";
let followers: number = 1200;
let verified: boolean = true;
let bio: string | null = null;

function printProfile(): void {
  console.log(username, followers, verified, bio);
}

Try assigning wrong values and see what TypeScript warns about:

followers = "many";
verified = "yes";

What You've Learned

You now understand:

  • What TypeScript basic types are
  • How to use string, number, and boolean
  • How null and undefined represent missing values
  • Why any is unsafe
  • Why unknown is safer than any
  • How void works with functions
  • What never means at a high level
  • When to use inference and when to write annotations

What's Next?

In the next lesson, we will learn the string Type in TypeScript.

We will go deeper into text values, template literals, string methods, and common string mistakes.


Need Help?

  • Have questions, confusion, or want to know more? Contact me

Basic types are small, but they are the vocabulary of TypeScript. Once these feel natural, bigger types become much easier.