List of Main Primitive Types in Typescript ?

TypeScript, being a superset of JavaScript, includes several primitive types that are the basic building blocks for handling data. These are the fundamental types in TypeScript:

1. number

Represents both integers and floating-point numbers.
Example:

let age: number = 25;
let price: number = 19.99;

2. string

Represents sequences of characters.
Example:

let name: string = "Alice";
let greeting: string = `Hello, ${name}`;

3. boolean

Represents a logical value: true or false.
Example:

let isDone: boolean = true;

4. null

Represents an explicitly empty or non-existent value.
Example:

let emptyValue: null = null;

5. undefined

Indicates a variable that has been declared but not initialized.
Example:

let notInitialized: undefined = undefined;

6. symbol

Introduced in ES6, symbol represents a unique and immutable value, often used for object property keys.
Example:

let uniqueKey: symbol = Symbol("key");

7. bigint

Introduced in ES2020, bigint represents large integers beyond the number type’s limit.
Example:

let largeNumber: bigint = 9007199254740991n;

Additional Notes

  • void: Used as a return type for functions that do not return a value.
  • any: Allows any type of value and disables type checking.
  • unknown: A safer alternative to any that requires type checking before usage.
  • never: Represents values that never occur, often used in functions that throw errors or infinite loops.

Summary of Main Primitives

TypeExampleDescription
numberlet x: number = 10Represents numeric values (integers and floats).
stringlet s: string = "Hi"Represents text or character sequences.
booleanlet b: boolean = trueRepresents true or false.
nulllet n: null = nullRepresents an explicitly empty value.
undefinedlet u: undefined = undefinedRepresents an uninitialized variable.
symbollet sym: symbol = Symbol("id")Represents unique identifiers.
bigintlet big: bigint = 123nRepresents large integer values.

These types ensure TypeScript enforces strong typing and helps catch errors early during development.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *