Skip to content
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

Different return types for Ternary/Conditional Operator #1462

Closed
AbubakerB opened this issue Dec 12, 2014 · 4 comments
Closed

Different return types for Ternary/Conditional Operator #1462

AbubakerB opened this issue Dec 12, 2014 · 4 comments
Labels
Fixed A PR has been merged for this issue

Comments

@AbubakerB
Copy link
Contributor

I have a few questions regarding the return type and validity of the conditional operator.
Consider the following:

var aye = () => 0;
var bee = () => console.log("hive");
true ? aye() : bee(); // error

This will throw an error stating "No best common type exists between 'number' and 'void'.".

I was wondering why it would thrown an error if all we're doing is evaluating the {Expressions} (in this case; calling those functions) while not caring about the return type.

I could understand the following throwing an error:

var result = true ? aye() : bee(); // Error: type of result could not be determined

but why should it throw an error if we don't care about the return type? I'm curious about the decision of this validation/error.
(note: i understand using any will solve this)

In addition, now with the introduction of Union Types in 1.4 (#805), i assume the following will be valid?

var result = true ? aye() : bee(); // type of result: number | void;


On a related note. The indentation for conditional operators is as so:

true
? "a"
: "b";

instead of tabbing the '?' and ':' (as C# does) - is this by design?

@CyrusNajmabadi
Copy link
Contributor

@AbubakerB Great feedback. Several points.

  1. We probably could relax the rule about the conditional in the case where the result of the conditional value is never used. That said, it really depends if we think this is a useful or common construct that people will use in practice. In my personal opinion, having a standalone conditional used for flow control is pretty esoteric, so i would hesitate to add support for this.

  2. The decision about where to use union types is still ongoing.

  3. The formatting for conditional expressions has just been fixed.

@AbubakerB
Copy link
Contributor Author

Thanks for the response.

As for 1), i understand your point. I guess the reason why i brought this up, is because i see this as valid javascript and i didn't see it going against any of the specs (or maybe i didn't look good enough).
If the work it takes to overcome this validation is not worth the result then, yea, i guess its fine. We can always just do an if..else instead! 😄

for 2) cool, i'll be looking forward to it!

and 3) Thanks!

@ahejlsberg
Copy link
Member

Regarding union types and the conditional operator, in (the upcoming) version 1.4 this does indeed produce a union type result:

var result = true ? aye() : bee(); // type of result is number | void

The changes are already in the master branch and the spec.

https://github.com/Microsoft/TypeScript/blob/master/doc/spec.md#4.16

@danquirk
Copy link
Member

Sounds like all these issues have been addressed.

@danquirk danquirk added the Fixed A PR has been merged for this issue label Dec 12, 2014
@microsoft microsoft locked and limited conversation to collaborators Jun 18, 2018
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

4 participants