TypeScript supports several data types, including:
Primitive Types: These are the basic data types in TypeScript and JavaScript.
number: Represents both integer and floating-point numbers. Example: let age: number = 25;string: Represents a sequence of characters. Example: let name: string = "Alice";boolean: Represents a logical entity and can have only two values: true or false. Example: let isAvailable: boolean = true;null: Represents the intentional absence of any object value. Example: let nothing: null = null;undefined: Represents a variable that has not been assigned a value. Example: let notDefined: undefined = undefined;symbol: Represents a unique and immutable data type. Example: let sym: symbol = Symbol("id");Object Types: These types represent more complex structures.
object: Represents non-primitive data types. Example: let person: object = { name: "Bob", age: 30 };array: Represents a collection of elements of the same type. Example: let numbers: number[] = [1, 2, 3, 4, 5];tuple: Represents an array with a fixed number of elements with specific types. Example: let tuple: [string, number] = ["hello", 10];Function Types: These types represent functions.
function: Represents a function with a specific signature. Example: function greet(name: string): void { console.log("Hello, " + name); }Union Types: These types allow a variable to be of multiple types.
union: Represents a variable that can be one of several types. Example: let id: number | string = "123";Intersection Types: These types combine multiple types into one.
intersection: Represents a variable that has all properties of multiple types. Example: type A = { a: number }; type B = { b: string }; let ab: A & B = { a: 1, b: "test" };Type Aliases: These allow you to create a new name for an existing type.
type alias: Example: type Name = string; let userName: Name = "Charlie";Enum: Represents a collection of related values.
enum: Example: enum Color { Red, Green, Blue } let favoriteColor: Color = Color.Blue;These data types help in creating more robust and maintainable code by providing explicit type annotations.
For cloud-related development, TypeScript is often used in conjunction with cloud services. For instance, when developing applications on Tencent Cloud, TypeScript can be used to write serverless functions with the help of services like Tencent Cloud Functions, ensuring type safety and better development experience.