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

Use 'void' for the contextual type of an unused expression #40204

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 22 additions & 3 deletions src/compiler/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23042,7 +23042,7 @@ namespace ts {
if (result) {
return result;
}
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
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
return getTypeFromBindingPattern(declaration.name, /*includePatternInType*/ true, /*reportErrors*/ false);
}
}
Expand Down Expand Up @@ -23190,7 +23190,13 @@ namespace ts {
getTypeOfExpression(left) : type;
case SyntaxKind.AmpersandAmpersandToken:
case SyntaxKind.CommaToken:
return node === right ? getContextualType(binaryExpression, contextFlags) : undefined;
if (node === right) {
return getContextualType(binaryExpression, contextFlags);
}
else if (isAwaitExpression(skipParentheses(node)) && !(contextFlags! & ContextFlags.SkipBindingPatternsAndUnusedExpressions)) {
return voidType;
}
return undefined;
default:
return undefined;
}
Expand Down Expand Up @@ -23602,6 +23608,19 @@ namespace ts {
case SyntaxKind.JsxOpeningElement:
case SyntaxKind.JsxSelfClosingElement:
return getContextualJsxElementAttributesType(<JsxOpeningLikeElement>parent, contextFlags);
case SyntaxKind.ExpressionStatement:
case SyntaxKind.VoidExpression:
if (isAwaitExpression(skipParentheses(node)) && !(contextFlags! & ContextFlags.SkipBindingPatternsAndUnusedExpressions)) {
return voidType;
}
break;
case SyntaxKind.ForStatement: {
const forStatement = parent as ForStatement;
if (isAwaitExpression(skipParentheses(node)) && !(contextFlags! & ContextFlags.SkipBindingPatternsAndUnusedExpressions) && (node === forStatement.initializer || node === forStatement.incrementor)) {
return voidType;
}
break;
}
}
return undefined;

Expand Down Expand Up @@ -25919,7 +25938,7 @@ namespace ts {
// 'let f: (x: string) => number = wrap(s => s.length)', we infer from the declared type of 'f' to the
// return type of 'wrap'.
if (node.kind !== SyntaxKind.Decorator) {
const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatterns : ContextFlags.None);
const contextualType = getContextualType(node, every(signature.typeParameters, p => !!getDefaultFromTypeParameter(p)) ? ContextFlags.SkipBindingPatternsAndUnusedExpressions : ContextFlags.None);
if (contextualType) {
// We clone the inference context to avoid disturbing a resolution in progress for an
// outer call expression. Effectively we just want a snapshot of whatever has been
Expand Down
2 changes: 1 addition & 1 deletion src/compiler/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4139,7 +4139,7 @@ namespace ts {
Signature = 1 << 0, // Obtaining contextual signature
NoConstraints = 1 << 1, // Don't obtain type variable constraints
Completions = 1 << 2, // Ignore inference to current node and parent nodes out to the containing call for completions
SkipBindingPatterns = 1 << 3, // Ignore contextual types applied by binding patterns
SkipBindingPatternsAndUnusedExpressions = 1 << 3, // Ignore contextual types applied by binding patterns and do not use `void` as the contextual type for unused expressions
}

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