-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Consider using union types for function return expressions #921
Comments
Thoughts on this:
There's also a big danger for functions which intend to return a boolean value. Consider something like this, where you'd never actually use the type in a way that reveals that it's become a union type: function isUndefinedOrEmpty(s: string) {
if(s === undefined) {
return true;
} else {
return s.length = 0;
}
}
// Later, this isn't an error
if(isUndefinedOrEmpty(n)) { ... }
// This isn't an error either
if(isUndefinedOrEmpty(n) === true) { } |
Everyone seems to like the current behavior. |
Perhaps we should reconsider this in the context of Our three options are:
|
Via #919
Even with union types, we still require that a best common type can be selected from the types of the return expressions in a function:
If this is the desired behavior, the user must write
This is inconsistent with how we're treating other cases, e.g.
var x = [1, 'foo'];
produces anArray<number|string>
.Is the desired behavior here that we infer a more complex union type return type for a function, or that we enforce a consistent set of return expression types when no other information is given?
The text was updated successfully, but these errors were encountered: