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

Salsa type inference should find or types #6663

Closed
egamma opened this issue Jan 27, 2016 · 5 comments
Closed

Salsa type inference should find or types #6663

egamma opened this issue Jan 27, 2016 · 5 comments
Assignees
Labels
Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@egamma
Copy link
Member

egamma commented Jan 27, 2016

From @alexandrudima on January 27, 2016 10:49

Testing #2218

function a1() {
    return 5;
}

function a2() {
    if (1) {
        return 5;
    } else {
        return '5';
    }
}

Hovering over a1() shows its return type is number. Hovering over a2() shows its return type is any. IMHO it should show number|string -- maybe this inference code was written before or types were introduced?

Copied from original issue: microsoft/vscode#2431

@billti
Copy link
Member

billti commented Jan 27, 2016

In TypeScript this is an error of "unknown type". If the types are relatable, we return the type that is the super-type of all return expressions. We explicitly don't try and synthesize a type as this code pattern often indicates a bug.

@billti billti closed this as completed Jan 27, 2016
@billti billti added the By Design Deprecated - use "Working as Intended" or "Design Limitation" instead label Jan 27, 2016
@mhegazy
Copy link
Contributor

mhegazy commented Jan 28, 2016

@billti, we should reconsider this one. the reason we stop this in TS, is we want ppl to go look at their code and make sure this is what they really intend. for JS, we do not have this error, so it seems unjustified to just block inference from the return types.

@RyanCavanaugh
Copy link
Member

We could easily make the checker say that the function returns number|string (instead of unknownType) but still issue an error (in TypeScript). I don't think that would have any meaningful impact to TypeScript. Seems reasonable?

@RyanCavanaugh RyanCavanaugh reopened this Jan 28, 2016
@billti
Copy link
Member

billti commented Jan 28, 2016

I'm wary of adding complexity to the code base and diverging the TypeScript and JavaScript behavior too greatly. If it's simple enough that the benefit outweighs the cost, sounds good to me.

I'd note though that for compatible types we already return the most common super type (which effectively gives you the same completions as a union type). For cases when the types are truly distinct (e.g. the number and string scenario above), is a union type going to be of much value? I suppose if folks are using type guards, or we add better narrowing going forward...

RyanCavanaugh added a commit to RyanCavanaugh/TypeScript that referenced this issue Jan 28, 2016
@RyanCavanaugh RyanCavanaugh added Bug A bug in TypeScript Fixed A PR has been merged for this issue and removed By Design Deprecated - use "Working as Intended" or "Design Limitation" instead labels Jan 28, 2016
@RyanCavanaugh RyanCavanaugh self-assigned this Jan 28, 2016
@mhegazy mhegazy added this to the Salsa 0.9 milestone Jan 28, 2016
@alexdima
Copy link
Member

Thank you! 👍

@mhegazy mhegazy modified the milestones: Salsa 0.9, TypeScript 1.8.2 Feb 2, 2016
billti pushed a commit that referenced this issue Feb 4, 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
Bug A bug in TypeScript Fixed A PR has been merged for this issue
Projects
None yet
Development

No branches or pull requests

5 participants