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 incorrect program doesn't give error (subtyping) #3651

Closed
hesselink opened this issue Jun 26, 2015 · 7 comments
Closed

Type incorrect program doesn't give error (subtyping) #3651

hesselink opened this issue Jun 26, 2015 · 7 comments
Labels
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead

Comments

@hesselink
Copy link

The following program crashes, but doesn't give a type error:

function g (f: (x: {}) => void): void { f({}) }

function f (x: { foo : { bar }}) { alert(x.foo.bar); }

// Doesn't typecheck
// f({ })

// Typechecks?!
g(f)

I would expect this to give a type error because f expects something with a foo, but g is only going to pass it an empty object.

@DanielRosenwasser
Copy link
Member

This is a symptom of bivariance when relating function arguments, which was an intentional design choice taken to support certain JavaScript patterns.

@DanielRosenwasser DanielRosenwasser added By Design Deprecated - use "Working as Intended" or "Design Limitation" instead Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design labels Jun 26, 2015
@hesselink
Copy link
Author

Ah, that's too bad. Perhaps this could be added to the specification, then? I looked there but couldn't find it.

Are there any plans to make this more strict at some point, perhaps with a flag. While I can see it enables some code to work, in our case it failed to catch bugs that we expected to be caught. We'd rather have to occasional cast than the occasional bug.

@DanielRosenwasser
Copy link
Member

Totally understandable. Actually, there is issue #274 which covers just that (i.e. making things more strict).

@hesselink
Copy link
Author

Thanks, I'll follow that issue.

The example on the wiki looks almost like the overloading on string values that typescript already allows. Perhaps overloading on enum values could be added as well to support that use case, removing a need for this bivariance?

@DanielRosenwasser
Copy link
Member

@RyanCavanaugh recently suggested overloading on enums. I think he might also be able to weigh in here on that question.

@RyanCavanaugh RyanCavanaugh removed the Canonical This issue contains a lengthy and complete description of a particular problem, solution, or design label Jun 26, 2015
@RyanCavanaugh
Copy link
Member

Overload on enum is compelling; I think we have an issue for it somewhere? We would probably tackle that after the "string literals as types" proposal as that will set a good foundation for it.

@DanielRosenwasser
Copy link
Member

@RyanCavanaugh I can create the issue for it in the mean time.

@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
By Design Deprecated - use "Working as Intended" or "Design Limitation" instead
Projects
None yet
Development

No branches or pull requests

3 participants