ReasonJun

Typescript : additional provided types (void, never) 본문

Frontend/Typescript

Typescript : additional provided types (void, never)

ReasonJun 2023. 6. 17. 15:33
728x90

The void type is used to represent functions that do not return a value. This is typically used for functions that have side effects, such as logging or printing to the console. For example:

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

const result = logMessage("Hello, world!"); // The result of this function is void

The never type is used to represent functions that never return a value. This is typically used for functions that always throw an error or enter an infinite loop. For example:

function throwError(): never {
  throw new Error("This function always throws an error");
}

const result = throwError(); // The result of this function is never

The main difference between void and never is that void allows functions to return undefined, while never does not. This is because undefined is a valid value in JavaScript, while never is not.

 

Here is a table that summarizes the key differences between void and never:

 

728x90
Comments