How to define a function which cannot return anything in TypeScript

Stephen Paul

I am writing an API which has 2, very similar, functions:

function update(f: () => string) {...}
function updateDeep(f: () => void) {...}

As you can see, I am trying to make sure that the client of my API passes the correct function type depending on which function they call.

The first function, update, works as predicted. This will rightly throw a compilation error:

update(() => console.log('hey'));

The second function, updateDeep, does not throw a compilation error event though it should:

updateDeep(() => 'hey');

How to I declare a function type which does not return anything?

Ryan Cavanaugh

It isn't possible to make this happen. As the recipient of a function, your only ability is to set a lower bound on what kind function is provided.

See also the TypeScript FAQ entry: https://github.com/Microsoft/TypeScript/wiki/FAQ#why-are-functions-returning-non-void-assignable-to-function-returning-void

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to play with a function which return table in lua?

From Dev

How to define in swift an optional variable for a function type, which can be nil?

From Dev

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

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How can I define a Typescript object return value for a function?

From Dev

How can I define a Typescript interface for a function?

From Dev

How to define virtual method with return type which is not void in C#

From Dev

How does this ruby function return anything?

From Dev

How to 'return' outside function definition in typescript

From Dev

How to define a function which requires one of two parameters?

From Dev

Function will not return anything

From Dev

using "return" in function or define global variable by "<<-" which of them? in R

From Dev

How to define a function does not return or return void in scala

From Dev

Typescript: define function, which transforms an object, keeping keys

From Dev

How to test functions which do not return anything using QTestLib

From Dev

How to define function return type of a custom object literal

From Dev

In typescript, how to define type of async function

From Dev

TypeScript Class how to define not prototype class function

From Dev

Is there a way to return comparator which wont do anything?

From Dev

How to define a PowerShell function which requires elevation?

From Dev

Does an impure function return anything?

From Dev

How can I define the return type of a lodash reduce function with Typescript?

From Dev

How to define a matrix valued function in Matlab/Octave -- return value syntax

From Dev

How to define a return type of a function which contains another, no parameter, function in Swift?

From Dev

How to call function overloaded by function which return partail function

From Dev

How to write a function which return a pointer to the stack

From Dev

Function does not return anything

From Dev

Typescript: How to define overloaded function type

From Dev

How to wait for return of a function in TypeScript?

Related Related

  1. 1

    how to play with a function which return table in lua?

  2. 2

    How to define in swift an optional variable for a function type, which can be nil?

  3. 3

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

  4. 4

    How can I define the return type of a lodash reduce function with Typescript?

  5. 5

    How can I define a Typescript object return value for a function?

  6. 6

    How can I define a Typescript interface for a function?

  7. 7

    How to define virtual method with return type which is not void in C#

  8. 8

    How does this ruby function return anything?

  9. 9

    How to 'return' outside function definition in typescript

  10. 10

    How to define a function which requires one of two parameters?

  11. 11

    Function will not return anything

  12. 12

    using "return" in function or define global variable by "<<-" which of them? in R

  13. 13

    How to define a function does not return or return void in scala

  14. 14

    Typescript: define function, which transforms an object, keeping keys

  15. 15

    How to test functions which do not return anything using QTestLib

  16. 16

    How to define function return type of a custom object literal

  17. 17

    In typescript, how to define type of async function

  18. 18

    TypeScript Class how to define not prototype class function

  19. 19

    Is there a way to return comparator which wont do anything?

  20. 20

    How to define a PowerShell function which requires elevation?

  21. 21

    Does an impure function return anything?

  22. 22

    How can I define the return type of a lodash reduce function with Typescript?

  23. 23

    How to define a matrix valued function in Matlab/Octave -- return value syntax

  24. 24

    How to define a return type of a function which contains another, no parameter, function in Swift?

  25. 25

    How to call function overloaded by function which return partail function

  26. 26

    How to write a function which return a pointer to the stack

  27. 27

    Function does not return anything

  28. 28

    Typescript: How to define overloaded function type

  29. 29

    How to wait for return of a function in TypeScript?

HotTag

Archive