Tutorials Logic, IN info@tutorialslogic.com

TypeScript Basic Types: string, number, boolean, null and undefined

What Are Basic Types?

TypeScript starts with the same runtime values JavaScript already has: strings, numbers, booleans, arrays, objects, null, undefined, symbols, and bigints. TypeScript adds a type system on top so your editor and compiler can catch incorrect usage before the code runs.

A type describes what kind of value a variable can hold and which operations are safe. If a value is a string, TypeScript allows string methods. If it is a number, TypeScript allows number methods. If you try to mix them incorrectly, TypeScript warns you early.

Type Example Value Common Use
string "admin" Names, emails, messages, slugs
number 42 Prices, counts, scores, IDs
boolean true Flags, toggles, yes/no state
null null Intentional empty value
undefined undefined Value not provided yet

Primitive Types

Primitive Types
let username: string = "meera";
let score: number = 92.5;
let isActive: boolean = true;

console.log(username.toUpperCase());
console.log(score.toFixed(1));
console.log(isActive ? "Active" : "Inactive");

// score = "high";
// Error: string is not assignable to number.

Type Inference

You do not need to annotate every variable. TypeScript can infer many types from assigned values. Inference keeps code clean while still giving type safety.

Use explicit annotations when the type is not obvious, when the variable starts empty, or when you want to document a wider type than the initial value.

Inferred Types

Inferred Types
const city = "Pune";       // inferred as the literal type "Pune"
let temperature = 31;      // inferred as number
let online = false;        // inferred as boolean

temperature = 29;
online = true;

// online = "yes";
// Error: string is not assignable to boolean.

Literal Types

A literal type is a type that allows one exact value. For example, "success" is more specific than string. Literal types are useful when a variable should only accept known values.

Literal types become powerful when combined with unions. Together, they can model statuses, roles, button variants, routes, payment states, and other controlled choices.

Literal Values

Literal Values
let requestStatus: "idle" | "loading" | "success" | "error";

requestStatus = "loading";
requestStatus = "success";

// requestStatus = "done";
// Error: "done" is not one of the allowed values.

type ButtonSize = "sm" | "md" | "lg";
const size: ButtonSize = "md";

Arrays and Object Basics

Arrays describe repeated values, while objects describe named properties. You will use both constantly in TypeScript applications.

For arrays, write the item type followed by []. For objects, describe each property name and its type. Optional properties use ?.

Simple Array and Object Types

Simple Array and Object Types
const tags: string[] = ["typescript", "frontend", "web"];
const scores: number[] = [80, 90, 100];

type User = {
  id: number;
  name: string;
  email?: string;
  active: boolean;
};

const user: User = {
  id: 1,
  name: "Asha",
  active: true,
};

null and undefined

null usually means the program intentionally has no value. undefined usually means a value has not been assigned or provided. With strict null checks enabled, TypeScript forces you to handle these cases before using the value.

This is one of TypeScript's most useful safety features because many runtime bugs come from trying to read a property or call a method on a missing value.

Handling Missing Values

Handling Missing Values
type Profile = {
  displayName: string;
  avatarUrl: string | null;
};

function getAvatarLabel(profile: Profile): string {
  if (profile.avatarUrl === null) {
    return "No avatar uploaded";
  }

  return `Avatar: ${profile.avatarUrl}`;
}

function greet(name?: string): string {
  return name ? `Hello ${name}` : "Hello Guest";
}

unknown, any and never

any disables type checking and should be used rarely. It is sometimes needed while migrating old JavaScript code, but leaving too much any removes the main benefit of TypeScript.

unknown is safer because TypeScript forces you to check the value before using it. never represents a value that should not exist, such as an impossible branch or a function that always throws.

Type Meaning Advice
any Skip type checking Avoid when possible
unknown Value exists but type is not known yet Check before use
never No possible value Useful for exhaustive checks

Safer Unknown Values

Safer Unknown Values
function printLength(value: unknown): void {
  if (typeof value === "string") {
    console.log(value.length);
    return;
  }

  if (Array.isArray(value)) {
    console.log(value.length);
    return;
  }

  console.log("Length is not available");
}

printLength("TypeScript");
printLength(["a", "b", "c"]);
Before you move on

TypeScript Basic Types: string, number, boolean, null and undefined Mastery Check

4 checks
  • You do not need to annotate every variable.
  • TypeScript can infer many types from assigned values.
  • Inference keeps code clean while still giving type safety.
  • A literal type is a type that allows one exact value.
Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.