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

Type inference on ternary-selected promises fails #7105

Closed
calebegg opened this issue Feb 17, 2016 · 1 comment
Closed

Type inference on ternary-selected promises fails #7105

calebegg opened this issue Feb 17, 2016 · 1 comment
Labels
Question An issue which isn't directly actionable in code

Comments

@calebegg
Copy link

Example:

let x = {then(cb: (x: string)=>any) { cb('x'); }};
let y = {then(cb: (x: Object)=>any) { cb({}); }};

(Math.random() < .5 ? x : y).then((z) => z); // z is string
(Math.random() < .5 ? y : x).then((z) => z); // z is Object

z should probably be string | Object in both cases.

Might be related, but if I have x: number in the second function above, the last lines fail with "Cannot invoke an expression whose type lacks a call signature", which is weird.

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Feb 17, 2016
@RyanCavanaugh
Copy link
Member

In general you should never declare things as Object, it rarely does what you want (for example, string is assignable to Object). See #1809.

The primary issue is that Object is a supertype of string, and function parameters are bivariant (see the entry in the FAQ), so the type of x is assignable to y and the type of y is assignable to x, so we're basically just picking the first one here when resolving the type of expr ? x: y.

The related issue is because call signatures don't appear on union types unless the signatures are identical. See #5640

@mhegazy mhegazy closed this as completed Feb 17, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

3 participants