Interview question-TS (2): How to define variable and function types in TypeScript?

Interview question-TS (2): How to define variable and function types in TypeScript?

1. Definition of variable type

In TypeScript, we can use a colon (:) to specify the type of a variable. Here are some common variable types:

  1. Boolean type (boolean): represents the value of true or false.
let isDone: boolean = false;
  1. Number type (number): represents a numeric value.
let age: number = 25;
  1. String type (string): represents a text value.
let name: string = "John";
  1. Array type (array): Represents a set of values ​​of the same type.
let numbers: number[] = [1, 2, 3, 4, 5];
  1. Tuple type (tuple): Represents a fixed-length array in which each element can have a different type.
let person: [string, number] = ["John", 25];
  1. Enumeration type (enum): represents a set of named constant values.
enum Color {
    
    
  Red,
  Green,
  Blue,
}

let myColor: Color = Color.Blue;
  1. Any type (any): Indicates any type of value, which is equivalent to relaxing the type check.
let data: any = "Hello, TypeScript!";
  1. Empty type (void): Indicates a function that does not return a value.
function greet(): void {
    
    
  console.log("Hello, TypeScript!");
}

Above are some common variable types.

2. Definition of function type

In TypeScript, we can define the input parameter types and return value types of functions. Here are some common ways to define function types:

  1. The complete function type definition:
function add(x: number, y: number): number {
    
    
  return x + y;
}
  1. Optional parameters and default parameters:
function greet(name: string, age?: number = 25): void {
    
    
  console.log(`Hello, ${
      
      name}! You are ${
      
      age} years old.`);
}
  1. Remaining parameters:
function sum(...numbers: number[]): number {
    
    
  return numbers.reduce((total, num) => total + num, 0);
}
  1. Function type and callback function:
type MathOperation = (x: number, y: number) => number;

function calculate(x: number, y: number, operation: MathOperation): number {
    
    
  return operation(x, y);
}

By defining the type of a function, we can more clearly express the intent and expected input and output of the function, improving the readability and maintainability of the code.

3. Type inference and type assertion

In TypeScript, if the type of the variable is not explicitly specified, the compiler will perform type inference based on the initial value of the variable. For example:

let message = "Hello, TypeScript!"; // 推断为string类型

In addition, sometimes we may need to tell the compiler the specific type of a variable, which is a type assertion. By using angle brackets or the as keyword after a variable, we can make type assertions. For example:

let data: any = "Hello, TypeScript!";
let length: number = (data as string).length;

Type inference and type assertion can work together to help us better deal with type derivation and control.

Guess you like

Origin blog.csdn.net/weixin_42560424/article/details/131912739