-
Notifications
You must be signed in to change notification settings - Fork 12.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Function return value constraints #228
Comments
This is an issue we have discussed a lot. The reasoning behind the current behavior is that a function with an explicitly declared non-void return type and no return statements is clearly suspicious (and therefore should be an error), but otherwise the function might simply be relying on the fact that JavaScript returns undefined when a function doesn't explicitly return a value. We would turn a lot of existing (and technically correct) JavaScript code into errors if we tightened this, so it would definitely have to be an opt-in similar to noImplicitAny. |
Thank you for the response Anders. |
Also related is #162, which would also be a breaking change if amended. |
Linking this to #274. I'd like to see some set of nailed-down rules. Specifically:
|
This check is very important when working with promises, because people sometimes forget to return a promise, thus breaking a promise chain. Consider this sample:
If you execute it, the result is:
Now, lets assume, a programmer makes a mistake and forgets to return the result of f2:
The result is
This mistake is very unfortunate. It is very easy to be made, especially when coding under pressure; is very easy to be missed on code reviews; tslint and friends does not complain; and its asynchronous nature allows it to slip through the manual and automated testing. The code even mostly works in production, except when it mysteriously does not. Considering how important promises and async/await become, I think it is worthy implementing this check behind an opt-in flag. |
👍 for @teobugslayer 's suggestion |
your wish is my command declare function foo(s: (x: boolean) => number);
foo((x): number => {
if (x) return 1;
}) compiling with current master bits
|
@vladima, I assume this will be released in typescript 1.8, is this correct? |
Yes |
Great news, thanks you! |
@vladima Great! |
It will be updated before the release |
I've tested with this code
and it was successfully compiled without errors using this command line
I am using typescript by installing Is this expected? |
currently it is by design, |
I see, thanks for the explanation. |
I am very glad. Great work. |
Should this issue be closed now? |
thanks @alexeagle. closing. |
Coming in "a bit" late to the party, thanks a lot for adding this option. As someone who is slowly but steadily learning TypeScript but with a strong background in the "implicit return" world of Ruby and CoffeeScript, this seems to be a bug I often make: forgetting to type out the So, great work @vladima and the rest of you, thanks a lot. A question: If I am declaring a method or a lambda type which must return a value, but it can return "any kind of value", is the correct way then to say that the return type is function sql(callback: (h: Handle) => Object): Object {
// ...
} Or is there some other, more correct way to do it? (As previously alluded, I willingly admit: I am not a Javascript nor TypeScript expert, yet. 😄) |
In the example above, suppose I want to support an
What if I'm creating an interface and want to indicate that a function can return a value of a certain type or not at all (
|
TypeScript compiler allows to define a function marked explicitly with a return type, with code branches which lead to an inconclusive return value!
I know It is valid in JavaScript function not to return a value on all code path, however in TypeScript being a javascript typed superset, I can imagine having a poor written function [without return value on all code path] could potentially break other codes and I'm talking null-references, type mismatch,
NaN
, ...I believe when using TypeScript code in an explicitly typed expression, no-one needs all the ugly plumbings, input or type validation,
undefined
checking, ... One needs to be sure to write a code that couldn't break the way a dynamic language like javascript could!The following is a valid JavaScript code and it could compile within TypeScript:
When you explicitly mention that the method has a return value, the compiler throw an error:
However it is not the case when the function does not return in all code path, and I don't understand if this is by design why bother preventing above code from compiling in the first place!
The followings are all wrong in a typed context yet the compiler ignores them as a valid code:
The text was updated successfully, but these errors were encountered: