Skip to content

Commit b1d9034

Browse files
committed
Use 'void' for the contextual type of an unused expression
1 parent 9569198 commit b1d9034

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

Diff for: src/compiler/checker.ts

+22-3
Original file line numberDiff line numberDiff line change
@@ -23042,7 +23042,7 @@ namespace ts {
2304223042
if (result) {
2304323043
return result;
2304423044
}
23045-
if (!(contextFlags! & ContextFlags.SkipBindingPatterns) && isBindingPattern(declaration.name)) { // This is less a contextual type and more an implied shape - in some cases, this may be undesirable
23045+
if (!(contextFlags! & ContextFlags.SkipBindingPatternsAndUnusedExpressions) && isBindingPattern(declaration.name)) { // This is less a contextual type and more an implied shape - in some cases, this may be undesirable
2304623046
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
2304723047
}
2304823048
}
@@ -23190,7 +23190,13 @@ namespace ts {
2319023190
getTypeOfExpression(left) : type;
2319123191
case SyntaxKind.AmpersandAmpersandToken:
2319223192
case SyntaxKind.CommaToken:
23193-
return node === right ? getContextualType(binaryExpression, contextFlags) : undefined;
23193+
if (node === right) {
23194+
return getContextualType(binaryExpression, contextFlags);
23195+
}
23196+
else if (!(contextFlags! & ContextFlags.SkipBindingPatternsAndUnusedExpressions)) {
23197+
return voidType;
23198+
}
23199+
return undefined;
2319423200
default:
2319523201
return undefined;
2319623202
}
@@ -23602,6 +23608,19 @@ namespace ts {
2360223608
case SyntaxKind.JsxOpeningElement:
2360323609
case SyntaxKind.JsxSelfClosingElement:
2360423610
return getContextualJsxElementAttributesType(<JsxOpeningLikeElement>parent, contextFlags);
23611+
case SyntaxKind.ExpressionStatement:
23612+
case SyntaxKind.VoidExpression:
23613+
if (!(contextFlags! & ContextFlags.SkipBindingPatternsAndUnusedExpressions)) {
23614+
return voidType;
23615+
}
23616+
break;
23617+
case SyntaxKind.ForStatement: {
23618+
const forStatement = parent as ForStatement;
23619+
if (!(contextFlags! & ContextFlags.SkipBindingPatternsAndUnusedExpressions) && (node === forStatement.initializer || node === forStatement.incrementor)) {
23620+
return voidType;
23621+
}
23622+
break;
23623+
}
2360523624
}
2360623625
return undefined;
2360723626

@@ -25919,7 +25938,7 @@ namespace ts {
2591925938
// 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
2592025939
// return type of 'wrap'.
2592125940
if (node.kind !== SyntaxKind.Decorator) {
25922-
const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
25941+
const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatternsAndUnusedExpressions : ContextFlags.None);
2592325942
if (contextualType) {
2592425943
// We clone the inference context to avoid disturbing a resolution in progress for an
2592525944
// outer call expression. Effectively we just want a snapshot of whatever has been

Diff for: src/compiler/types.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -4139,7 +4139,7 @@ namespace ts {
41394139
Signature = 1 << 0, // Obtaining contextual signature
41404140
NoConstraints = 1 << 1, // Don't obtain type variable constraints
41414141
Completions = 1 << 2, // Ignore inference to current node and parent nodes out to the containing call for completions
4142-
SkipBindingPatterns = 1 << 3, // Ignore contextual types applied by binding patterns
4142+
SkipBindingPatternsAndUnusedExpressions = 1 << 3, // Ignore contextual types applied by binding patterns and do not use `void` as the contextual type for unused expressions
41434143
}
41444144

41454145
// NOTE: If modifying this enum, must modify `TypeFormatFlags` too!

0 commit comments

Comments
 (0)