SoFunction
Updated on 2025-04-10

A detailed explanation of built-in data types in TypeScript

As a type-safe programming language, TypeScript provides a variety of built-in data types to help us better define and manipulate data.

1. Boolean type

First, let's take a look at the boolean type. The Boolean type represents a logical value, with only two possible values: true (true) and false (false). In TypeScript, we can use the boolean keyword to declare variables of Boolean type.

let isDone: boolean = false;

Boolean types are very common in conditional judgments and logical operations. For example, we can use the Boolean type to determine whether a condition is true and perform the corresponding operation based on the result.

let isLogged: boolean = true;
if (isLogged) {
  ("The user is logged in!");
} else {
  ("The user is not logged in!");
}

2. Number type (number)

The next type is the numeric type. Number types are used to represent numeric values, including integers and floating-point numbers. In TypeScript, we can declare variables of numeric types using the number keyword.

let age: number = 27;
let pi: number = 3.14;

Number types support basic mathematical operations such as addition, subtraction, multiplication, and division.

let x: number = 10;
let y: number = 5;
let sum: number = x + y;
let difference: number = x - y;
let product: number = x * y;
let quotient: number = x / y;
(sum);         // Output: 15(difference);  // Output: 5(product);     // Output: 50(quotient);    // Output:2

3. String type (string)

Next, let's discuss string types. String type is used to represent text data. In TypeScript, we can declare variables of string type using the string keyword.

let name: string = "Zhang San";
let message: string = "Welcome to my personal website!";

String types support string stitching, intercepting, and length acquisition.

let greeting: string = "Hello,";
let name: string = "Alice";
let message: string = greeting + " " + name;
let substring: string = (7, 12);
let length: number = ;
(message);    // Output: Hello, Alice(substring);  // Output: Alice(length);     // Output:13

4. Array type (array)

Array types are used to represent a set of data sets of the same type. In TypeScript, we can use type[] or Array<type> to declare variables of array type, where type is the type of elements in the array.

let numbers: number

[] = [1, 2, 3, 4, 5];
let fruits: Array<string> = ["apple", "banana", "orange"];

Array types support accessing elements, adding elements, and getting array length.

let numbers: number[] = [1, 2, 3, 4, 5];
(numbers[0]);       // Output: 1(6);
(numbers);          // Output: [1, 2, 3, 4, 5, 6]let length: number = ;
(length);           // Output:6

5. Tuple type (tuple)

Tuple type is used to represent an array of known elements and types. In TypeScript, we can declare variables of tuple type using a list of type[] and element types.

let person: [string, number] = ["Zhang San", 27];

Tuple types can access elements at specific index locations and retain the element's type information.

let person: [string, number] = ["Zhang San", 27];
(person[0]);    // Output: "Zhang San"(person[1]);    // Output:27

6. Enum type (enum)

Enumeration types are used to represent a set of named constants. In TypeScript, we can declare enum types using the enum keyword.

enum Color {
  Red,
  Green,
  Blue,
}
let backgroundColor: Color = ;

Enumeration types provide a meaningful set of named constants for us to use in our code.

enum Day {
  Monday = 1,
  Tuesday,
  Wednesday,
  Thursday,
  Friday,
  Saturday,
  Sunday,
}
let today: Day = ;
(today);    // Output:3

7. Any type (any)

Any type can be used to represent a value of any type. In TypeScript, we can declare variables of any type using the any keyword.

let value: any = "Hello, World!";
value = 42;
value = true;

Any type can be adapted to different types of values ​​flexibly, but also loses type safety.

let value: any = "Hello, World!";
(7);    // Valid, return: "World!"value = 42;
(7);    // Report an error!Types are not checked during compilation

8. Empty type (void)

The empty type is used to represent a function that does not have any return value. In TypeScript, we can declare empty types using the void keyword.

function greet(): void {
  ("Hello!");
}

The empty type is mainly used to define functions that do not require a return value.

function sayHello(name: string): void {
  ("Hello, " + name + "!");
}

9. Null and Undefined Types

Null and Undefined types represent null and undefined values, respectively. In TypeScript, we can declare the corresponding variables using the null and undefined keywords.

let empty: null = null;
let notDefined: undefined = undefined;

The Null and Undefined types can be used as subtypes of other types in some cases.

let name: string = null;
let age: number = undefined;

10. Never Type

Never type represents a type that never happens. In TypeScript, the Never type is usually used to describe functions that throw an exception or fail to execute to the end point.

function throwError(message: string): never {
  throw new Error(message);
}
function infiniteLoop(): never {
  while (true) {
    // Infinite loop  }
}

The Never type is a subtype of other types, but no type is a subtype of the Never type.

let value: never = throwError("Something went wrong.");

Summarize

The above are some commonly used built-in data types in TypeScript. By using these data types, we can better define and manipulate data, improving the readability and maintainability of our code.

  • Boolean: true or false
  • Number type (number): integer and floating point number
  • String type (string): text data
  • Array type: a set of data sets of the same type
  • Tuple type (tuple): an array of known elements and types
  • Enum type (enum): a named set of constants
  • Any type: any type of value
  • Empty type (void): a function with no return value
  • Null and Undefined types: null and undefined values
  • Never Type: Type that never happens

The sample code is used only to illustrate concepts and may not be in line with best practices. In actual development, please make adjustments according to the specific situation.

This is the end of this article about a detailed explanation of the built-in data types in TypeScript. For more related content on built-in data types in TypeScript, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!