Is the return type of a function a type of the function?

Problem :

  1. Regular function

Is the type of this function – "void" ? (so in the case of a regular function – regular function type = return type of the regular function)
Or is type of this function just – "Function" ?

function regularFunction(): void {}
  1. Named function

Is the type of this function – "() => void" ? (so in the case of a named function – named function type = whatever this function holds within itself)
Or is type of this function just – "void" ? Or maybe – "Function" ?

const namedFunction = function (): void {};

P.S : I’m not trolling, that’s a serious question … I’m so confused about types of different functions

I tried to seek for an answer on various resourses but they still don’t answer my question

>Solution :

The type of a function is usually referred to as its signature and it includes both the types of the inputs (argument types) and the type of the output (return type). The type of both of your functions is () => void, it doesn’t matter whether the function is named or anonymous.

In Typescript there exists an interface called Function, and all functions implement it, but it is not the type of a specific function and generally it is not used very often as it’s not terribly useful. Even in generic constraints the preferred alternative is like so

function foo<T extends (...args: any) => any>(f: T): whatever

Here T is guaranteed to be a function.

Leave a Reply