I have been trying to document and set up a type definition for an API that I have to work with and discovered the ability to set a function as a property in the interface. While researching the correct way to do this, I came across this article by Jeff Butsch that states,
Example 1: Function that does not take arguments and does not return a value: In the example belowonChange is the function declaration. The function returns nothing (void) so we declare the return type as any. This is because void and never are evaluated differently in different contexts (beyond the scope of this article). (Below are some examples of typed return values.)
interface MyClassProps { someProp: string; onChange(): any; } class MyClass extends React.Component<MyClassProps, MyClassState> ...
I have not been able to locate any other references to how Typescript would handle void
in a method declaration in a way that would produce unexpected results and make it worth declaring any
instead. (Searches for things like 'typescript interface method declaration void any never' return a lot of irrelevant results as those keywords appear with each other in a wide variety of contexts.)
Can anyone offer any insight as to the actual difference here? What problems could potentially be encountered from declaring a method in an interface
as returning void
when one legitimately expects it to return void
?