Fundamental Types in TypeScript: Primitives, Objects, Arrays, Tuples, Enums, and Unions

TypeScript is a statically typed programming language developed by Microsoft that is a superset of JavaScript and enables type checking at compile time. TypeScript's powerful type system makes it easier for developers to write maintainable and extensible code. This article will introduce the basic types in TypeScript in detail, including primitive types, object types, array types, tuple types, enumeration types, and union types.

primitive type

In TypeScript, there are the following primitive types:

number type

Numeric types are used to represent integer or floating point numbers. numberNumeric variables can be declared using the keyword.

For example:

let num: number = 123;

string type

The string type is used to represent text data. stringString variables can be declared using the keyword.

For example:

let str: string = "Hello";

Boolean type

The Boolean type is used to represent logical values, ie trueor false. booleanBoolean variables can be declared using the keyword.

For example:

let isTrue: boolean = true;

Null values ​​and undefined types

The null type ( void) is used to represent functions that return no value. The undefined type ( undefined) is used to represent variables that have not been assigned a value. You can use voidthe and undefinedkeywords to declare the corresponding variables.

For example:

let result: void = undefined;  // 空值类型
let undef: undefined = undefined;  // 未定义类型

Null and untyped types

The null type ( void) is used to represent functions that return no value. nullTypes are used to represent null values ​​or object references that are null. You can use voidthe and nullkeywords to declare the corresponding variables.

For example:

let nothing: null = null;  // 空值类型
let nul: null = null;  // 空值类型

object type

Object types are used to represent non-primitive data types, including objects, arrays, functions, and so on. Object types can be objectdeclared using the keyword.

object type

The object type is used to represent an object, which contains multiple key-value pairs. Object types can be declared using {}the or keywords. objectThe object type can specify the type of attribute name and attribute value.

For example:

let person: {
    
     name: string; age: number } = {
    
    
  name: "John",
  age: 25,
};

array type

The array type is used to represent an ordered collection of elements of the same type. Array types can be declared using the 类型[]or syntax.Array<类型>

For example:

let numbers: number[] = [1, 2, 3, 4, 5]; // 数字数组
let names: string[] = ["Alice", "Bob", "Charlie"]; // 字符串数组

tuple type

The tuple type is used to represent an array of fixed length and type. You can use [类型1, 类型2, ...]the syntax to declare tuple types.

For example:

let user: [string, number] = ["John", 25]; // 元组类型

function type

A function type is used to represent a function. Function types can be (参数类型) => 返回值类型declared using the syntax.

For example:

let add: (x: number, y: number) => number = function (x: number, y: number): number {
    
    
  return x + y;
};

enumerated type

An enumerated type is used to represent a set of named constants. enumEnumeration types can be declared using the keyword.

For example:

enum Color {
    
    
  Red,
  Green,
  Blue,
}

let color: Color = Color.Green;

In an enumeration type, each enumeration member has a numeric value associated with it, starting from 0 by default. You can also manually specify values ​​for enumeration members.

union type

Union types are used to indicate that a variable can be one of several types. 类型1 | 类型2 | ...Union types can be declared using the syntax.

For example:

let value: string | number = "Hello"; // 联合类型
value = 123; // 合法

Union types can provide greater flexibility, allowing us to handle values ​​of many different types.

Type inference and type assertion

TypeScript has powerful type inference capabilities, which can automatically infer the type of variables based on the context. For example, TypeScript can infer the variable's type if we assign a value directly when we define it.

let num = 123; // 类型推断为 number
let str = "Hello"; // 类型推断为 string

Additionally, we can use type assertions to tell the compiler the type of a value. Type assertions have two syntactic forms, <类型>值and 值 as 类型.

let someValue: unknown = "Hello";
let strLength1: number = (<string>someValue).length;
let strLength2: number = (someValue as string).length;

Type assertions can provide type information in some cases where type inference cannot be done, but should be used sparingly to avoid type errors.

Summarize

This article introduces TypeScript's basic types in detail, including primitive types, object types, array types, tuple types, enumeration types, and union types. TypeScript's powerful type system enables developers to perform type checking at compile time, reducing the probability of type errors at runtime.

Guess you like

Origin blog.csdn.net/weixin_43025343/article/details/131836984