Why can't I annotate a function expression as void despite that it returns nothing in TypeScript?

Grateful

I have a function expression that returns nothing. However, the following doesn't work.

let printHello: void = (): void => console.log('Hello!');

However, if I change the type to object it works!

let printHello: object = () => console.log('Hello!');

I think I have the actual function under control. However, I can't seem to annotate the variable printHello correctly...

jonrsharpe

A TS variable declaration always takes the same form:

let <name>[: <type>][ = <value>];

In your case:

let printHello: void = (): void => console.log('Hello!');
//     name   | type | value

(The function also has the : void return type, but that's part of the value so not relevant here.)

However, the actual type of the value, which can be inferred if we remove the explicit type from the declaration:

let printHello = (): void => console.log('Hello!');

is not void, it's () => void - the value itself isn't undefined, it's a function returning undefined. So if you do want to explicitly include the type and not rely on inference (which "may add unnecessary verbosity"), it would have to be:

let printHello: () => void = (): void => console.log('Hello!');
//     name   |    type    |    value

object works not because your function returns an object (it doesn't), but because a function is an object.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

useQuery function returns void and I don't know why

void function returns nothing - C

Why can't I reassign a function in TypeScript?

Why can't I make a void arrow function?

Why can't I cast a function pointer to (void *)?

Why can't I convert [](auto&&...){} into a std::function<void()>?

Why I can't declare void * p in the end of longestCommonPrefix function?

TypeScript: why function can return value despite that its type doesn't allow for it?

Non-void function returns nothing dart

Why XMPP function returns nothing?

Why can't I add an extra parameter to a function in TypeScript?

How can I see if a function pointer passed to a function returns void?

Why I am getting this "This function must return a value of type View" error despite the interface function is returning void?

Why can't I see the favicon despite setting the manifest?

Why can't the name of a function expression be reassigned?

Why can't a yield expression be a function argument?

Why can't a function with return value be assigned to a pointer function with void?

How can I define a return type of void for a function in a Typescript interface?

Why can't I use a starred expression?

Why can't I configure a CMake target with CLion (Nothing to run on)?

Why can't I assign Nothing to a variable of type Unit?

Typescript why can't return header in function?

if function in javascript returns nothing..why?

Why can I assign a `Function` to an `Interface` in Typescript?

Nothing is static, though apparently I can't access a non-static void

Why can't I use Data.Function.(&) in the same expression as Control.Arrow.(>>>)?

Why can I not cast a lambda to void* then to a function pointer?

Why can't I assign a value to the class member variable in a bool function that returns const? C++

Why can't I use a key function that returns a reference when sorting a vector with sort_by_key?