Restrict function return value to void

Estus Flask

bar is expected to be a function with no return value:

let foo = () => 'foo';
let bar: () => void = foo; // produces no error

Can bar be prevented from being assigned with a function that doesn't match expected () => void signature?

For example, in this case:

declare let jsApiThatShouldNeverAcceptTypeofFoo = (bar: () => void) => void;
jsApiThatShouldNeverAcceptTypeofFoo(bar);
Titian Cernicova-Dragomir

A partial workaround is to ensure that the return type of the function has no keys. This is satisfied by void, but also by {} and never. This might be close enough, as functions retuning never should throw an error and functions returning {} happen infrequently enough:

type EnsureVoid<T> = keyof T extends never ? 
    void 
    : "Should not have a return type, only ()=> void is allowed";

declare let jsApiThatShouldNeverAcceptTypeofFoo: <T extends EnsureVoid<T>>(bar: ()=> T) => void;

let foo = () => 'foo';
jsApiThatShouldNeverAcceptTypeofFoo(foo)  // Error 


let bar = () => { };
jsApiThatShouldNeverAcceptTypeofFoo(bar)


let baz = () => ({ });
jsApiThatShouldNeverAcceptTypeofFoo(baz)

let boo = () => { throw "Error" };
jsApiThatShouldNeverAcceptTypeofFoo(boo)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Return value void * in a function

unexpected non void return value in void function

void function pointer return value in C

Unclear about return value of a void function in C

Should i return a value for a Future<void> Function?

Why does a void function return a value?

How to return a value from Void Function?

Unexpected non-void return value in void function

Unexpected Non-Void Return Value In Void Function (Swift 2.0)

Unexpected non-void return value in void function (Swift 3)

Swift NumberOfRowsInSection Unexpected non-void return value in void function

'Unexpected non-void return value in void function' in swiftui

Restrict return to execute in function

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

My void function returns a value and does not return to the main function.

Why am I able to use the no void or return value on this function

How to call a function, passing the return value (possibly void) of a functor?

How to run a function when return value is void upon setup in Moq

swift Non-void function should return a value in a guard let

QNX Momentics: GoogleMock - Return has value, in function returning void error

Anonymous function converted to a void returning delegate cannot return a value

how void function can be return value? c++

Return from void function

Void Inside of Return function

Void value as return parameter

Return from void function to another void function

What is the return value if we dont return anything from a non void return typed function in c++?[Experimental]

why in this code would I be getting "Unexpected non-void return value in void function"

Retrieve product localised price - Unexpected non-void return value in void function