Basic TypeScript Types to learn - Complete Types in TypeScript

TypeScript provides several basic types that can be used to define variables and function parameters. Here are the most commonly used basic types in TypeScript:

  1. Boolean - represents a boolean value (true or false)
  2. Number - represents a numeric value (integer or floating-point)
  3. String - represents a string value (a sequence of characters)
  4. Array - represents an array of values of a specific type
  5. Tuple - represents an array with a fixed number of elements of specific types
  6. Enum - represents a set of named constants
  7. Any - represents any value, and can be used to disable type checking
  8. Void - represents the absence of a value, typically used as the return type for functions that do not return a value
  9. Null - represents a null value
  10. Undefined - represents an undefined value

These basic types can be used to define variables, function parameters, and return types. TypeScript also provides advanced types such as union types, intersection types, and type aliases that can be used to create more complex type definitions.

Start Testing Basic typescript types

Let’s create a file called basic-type.ts

Sure! Here are some examples of each of the basic types in TypeScript:

Boolean:

let isCompleted: boolean = true;
let hasErrors: boolean = false;

// Later catch compile time errors
isCompleted = 'false'; // ❌
isCompleted = null; // ❌
isCompleted = '12'; // ❌
isCompleted = '0'; // ❌
isCompleted = '1'; // ❌
isCompleted = true; // ✅
isCompleted = false; // ✅

Number:

let age: number = 30;
let price: number = 9.99;

// Later catch compile time errors
number = '20'; // ❌
number = 'true'; // ❌
number = true; // ❌
number = null; // ❌
number = 50.44; // ✅
number = 100; // ✅

String:

let name: string = "John";
let message: string = `Hello ${name}!`;

// Later catch compile time errors
name = 20; // ❌
name = 10.1; // ❌
name = true; // ❌
name = null; // ❌
name = "Jhon Doe"; // ✅
name = 'Jhon Doe'; // ✅
name = `Jhon Doe`; // ✅
name = `${firstName} ${lastName}`; // ✅

Array:

let numbers: number[] = [1, 2, 3, 4];
let names: string[] = ["John", "Jane", "Bob"];

// Later catch compile time errors
numbers = 'false'; // ❌
names = [1, "Jhon"]; // ❌
names = [null]; // ❌
number = [1, 100, 200.1]; // ✅
names = ["Akash", "Jhon"]; // ✅

Tuple:

let person: [string, number] = ["John", 30];
let coordinate: [number, number, number] = [10, 20, 30];

Enum:

enum Color {
  Red,
  Green,
  Blue,
}

let favoriteColor: Color = Color.Blue;

Void:

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

Null:

let nullValue: null = null;

// Later catch compile time errors
nullValue = 'false'; // ❌
nullValue = [1, "Jhon"]; // ❌
nullValue = null; // ✅

Undefined:

let undefinedValue: undefined = undefined;

// Later catch compile time errors
undefinedValue = 'false'; // ❌
undefinedValue = [1, "Jhon"]; // ❌
undefinedValue = undefined; // ✅

Any:

Anything could be assigned to this. This is not a recommended to use this type. If we use this type, means we don’t know the types and it’s bad practice in Typescript.

let value: any = "hello";
value = 123;
value = ["a", "b", "c"];

In each of these examples, we define a variable and assign it a value of a specific basic type. These basic types can be used to create more complex type definitions, and to ensure that our code is more reliable and less prone to errors.

Previous
Installing TypeScript Step by step guide for beginner
Next
TypeScript Variable, Annotation and Inference Learning