Skip to content

Consider contextually typing parenthesized expressions #920

Closed
@RyanCavanaugh

Description

@RyanCavanaugh

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.

Metadata

Metadata

Labels

Breaking ChangeWould introduce errors in existing codeBugA bug in TypeScriptFixedA PR has been merged for this issue

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions