Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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" ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading