what is returntype in typescript

In TypeScript, return type specifies the type of the value that a function returns. A function return type can be defined using a type annotation in the function signature.

Here's an example of declaring a function with a return type:

index.ts
function addNumbers(a: number, b: number): number {
  return a + b;
}
70 chars
4 lines

In the above example, the return type is defined as number using a type annotation after the function parameter list.

If a function does not have a return type annotation, TypeScript will infer the return type as any.

index.ts
function addNumbers(a: number, b: number) {
  return a + b;
}
62 chars
4 lines

In the above example, TypeScript infers the return type as any.

It is always recommended to specify return type in function signature to avoid any runtime errors.

gistlibby LogSnag