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:

1let firstName: string = "Amir";

number

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

  • Example:

1let age: number = 30;

boolean

  • Represents true/false values.

  • Example:

1let isDeveloper: boolean = true;
2

null

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

  • Example:

1let emptyValue: null = null;
2

undefined

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

  • Example:

1let notAssigned: undefined = undefined;
2

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:

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

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