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

Consider contextually typing parenthesized expressions #920

Closed
RyanCavanaugh opened this issue Oct 20, 2014 · 4 comments
Closed

Consider contextually typing parenthesized expressions #920

RyanCavanaugh opened this issue Oct 20, 2014 · 4 comments
Assignees
Labels
Breaking Change Would introduce errors in existing code Bug A bug in TypeScript Fixed A PR has been merged for this issue

Comments

@RyanCavanaugh
Copy link
Member

We've seen some issues where expressions which are non-obviously parenthesized lose contextual typing and cause issues:

var x: SomeType = (n) => ((m) => q)); // 'm' does not get a contextual parameter type
var y: SomeType = t ? (m => m.length) : undefined; // same here

This trips people up because they don't expect parentheses to change the type system, especially when parens are required to make an expression parse.

With the improvements in the type system with union types, we may have crossed the line where we can more aggressively apply contextual types. The 'new' workaround in cases (if there are any) where a contextual type needs to be avoided would be to use a type assertion to any or some other type which lacks a contextual impact.

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Oct 20, 2014
@danquirk
Copy link
Member

Should note that this would be a breaking change. Likely the breaks are bugs but still, we'll have any typed things going to something more specific which is bound to break someone here and there.

@NoelAbrahams
Copy link

This trips people up because they don't expect parentheses to change the type system,

Certainly has tripped me up - twice

#862
#1149

@RyanCavanaugh
Copy link
Member Author

This was discussed in passing at the design meeting today and it seemed like there was broad agreement that we should do it.

@DanielRosenwasser
Copy link
Member

We now perform the appropriate contextual typing.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Breaking Change Would introduce errors in existing code 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