I think that typescript has many unobvious places, making it not strict and not correct.
I want use undefined as functions return type. Because in reality it is undefined, not void or some other fictional type. But when I write this:
function myFunction(): undefined {
}
it says "A function whose declared type is neither 'void' nor 'any' must return a value".
It must not. And everyone can verify this. I don't want to agree with "void is better, we decided that promise equal undefined" and so on. And don't want to write return undefined
, if it is obvious and redundant.
How to make it work in this example? May be some flag exist or some "miracle comment instruction"?
Here is the use case, explaining why I want explicit undefined:
tsconfig.json
file, by settingnoImplicitReturns
tofalse
. – Benefitreturn undefined
". Is it acceptable to writereturn
? – Randarandalreturn
statement, then why do you want to annotate the return value with anything other thanvoid
? It's never going to change and doesn't seem to depend on what the function does. – Napoleonvoid
perfectly encapsulates "This function doesn't return anything". Saying it returnsundefined
suggests that makes sense. Yet, if it does it because any function always implicitly returns that value and you never rely or influence it, then there really isn't any meaning to that. You're obscuring the fact that the return value shouldn't be relied on. – Napoleonreturn undefined
to allow this "reliance on". I want an exact contract, not "something in some situation". – HallahEverything in my code may rely on this return type
If it always returnsundefined
, what sort of code are you planning to write that will interact with the return value? Like are you going to write code likeif (myFunction() === undefined)
? If you're not going to interact with it, thenvoid
is the correct type. If you are... well, frankly, i don't know what use that is, so i'd still recommendvoid
unless you have an example that demonstrates the point. – Randarandalundefined
is different to something likeArray#find
whereundefined
is a valid value from its domain. You've introduced two different meanings for the value in your code. Whereasvoid
means a single thing. You further lose semantincs for defining your interfaces. – Napoleonvoid
perfectly encapsulates that. Yet if you demand it to returnundefined
you lose the ability to pass something likex => array.push(x)
as a callback and instead have to dox => { array.push() }
due to the dual semantics ofundefined
you've introduced. – Napoleonvoid
is a big pain. It is a big conversation, not for comments. I understand that maybe everyone think another way and nobody want strict return type, and they want all that meanings and advantages ofvoid
. – HallahI can't show all the situations here
One is all i need. In any event, if you truly understand whatvoid
means, and you still have decided thatundefined
is better for your case, then you will need to have an explicit return statement. Leaving out a return statement when you've said you'll return something is a bug in most people's code, which is why typescript enforces it. – Randarandal