This site uses tracking technologies. You may opt in or opt out of the use of these technologies.

Back to Typescript

Typescript Primitive Types

get familiar with basic types like string, number, boolean, null, undefined, and any.

In TypeScript, primitive types represent the most basic data structures, and they are essential building blocks. Here’s a quick overview of each:

string

  • Represents text data.

  • Example:

let firstName: string = "Amir";

number

  • Represents numerical values, both integers and floating-point numbers.

  • Example:

let age: number = 30;

boolean

  • Represents true/false values.

  • Example:

let isDeveloper: boolean = true;

null

  • Represents an explicitly empty or null value. It is technically a subtype of all other types.

  • Example:

let emptyValue: null = null;

undefined

  • Used when a variable is declared but has not been assigned a value. It’s also a subtype of all types.

  • Example:

let notAssigned: undefined = undefined;

any

  • Allows any kind of value to be assigned, making it the most flexible type. Useful when migrating JavaScript code or when you don’t know the type ahead of time.

  • Example:

let unknownValue: any = "Could be anything";
unknownValue = 42; // also valid

These types provide the foundation for working with TypeScript, helping enforce type safety across variables and function parameters.