Content-Length: 387715 | pFad | http://github.com/microsoft/TypeScript/pull/62350

F3 Control flow analysis for destructured rests by Andarist · Pull Request #62350 · microsoft/TypeScript · GitHub
Skip to content

Conversation

Andarist
Copy link
Contributor

fixes #46680
fixes #53947

@Copilot Copilot AI review requested due to automatic review settings August 28, 2025 17:30
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Aug 28, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Aug 28, 2025
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #46680. If you can get it accepted, this PR will have a better chance of being reviewed.

1 similar comment
@typescript-bot
Copy link
Collaborator

The TypeScript team hasn't accepted the linked issue #46680. If you can get it accepted, this PR will have a better chance of being reviewed.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR implements control flow analysis for destructured rest variables, addressing two specific TypeScript issues (#46680 and #53947). The changes enable the TypeScript compiler to properly narrow types in destructuring assignments that include rest elements, improving type inference when discriminated unions are involved.

Key Changes

  • Enhanced control flow analysis to handle destructured rest variables by tracking narrowed types through rest patterns
  • Modified the constraint substitution logic to be more selective, improving type narrowing for binding elements
  • Extended support for both object and array destructuring with rest elements

Reviewed Changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/cases/conformance/controlFlow/dependentDestructuredRestVariables1.tsx New test cases demonstrating control flow analysis for destructured rest variables in various scenarios
tests/baselines/reference/dependentDestructuredRestVariables1.symbols Generated symbol baseline for the new test cases
tests/baselines/reference/arrayDestructuringInSwitch1.types Updated type baseline showing improved type inference for array destructuring
tests/baselines/reference/arrayDestructuringInSwitch1.symbols Updated symbol baseline with refined symbol references
src/compiler/checker.ts Core implementation changes to enable control flow analysis for rest variables
Comments suppressed due to low confidence (1)

src/compiler/checker.ts:1

  • [nitpick] The long conditional expression on line 30851 reduces readability. Consider extracting the check mode determination into a variable: const checkMode = shouldSubstituteConstraints(type, location) ? CheckMode.Normal : CheckMode.SkipConstraintsSubstitution;
import {

const parent = declaration.parent.parent;
const rootDeclaration = getRootDeclaration(parent);
if (rootDeclaration.kind === SyntaxKind.VariableDeclaration && getCombinedNodeFlagsCached(rootDeclaration) & NodeFlags.Constant || rootDeclaration.kind === SyntaxKind.Parameter) {
const links = getNodeLinks(parent);
if (!(links.flags & NodeCheckFlags.InCheckIdentifier)) {
links.flags |= NodeCheckFlags.InCheckIdentifier;
const parentType = getTypeForBindingElementParent(parent, CheckMode.Normal);
const parentTypeConstraint = parentType && mapType(parentType, getBaseConstraintOrType);
const parentType = getTypeForBindingElementParent(parent, shouldSubstituteConstraints(type, location) ? CheckMode.Normal : CheckMode.SkipConstraintsSubstitution);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When obtaining the parent type we don't want to get its constraints substituted based on its contextual type or it being in a constraint position or something. This would lead to returning its constraint and then using that for getBindingElementTypeFromParentType even when the current location isn't at the constraint position.

@Andarist
Copy link
Contributor Author

Huh, I'll have to investigate what's happening with the self-check

@Andarist Andarist marked this pull request as draft August 28, 2025 17:42
@controversial
Copy link

wowwww ❤️

@Andarist Andarist marked this pull request as ready for review August 29, 2025 10:16
Comment on lines +3 to +7
=== Performance Stats ===
Assignability cache: 2,500
Type Count: 10,000
Instantiation count: 100,000
Symbol count: 50,000
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fwiw, those numbers are the same on main

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
For Uncommitted Bug PR for untriaged, rejected, closed or missing bug
Projects
Status: Not started
Development

Successfully merging this pull request may close these issues.

Control flow analysis for destructured rest element of discriminated union Keep context after using spread
3 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/microsoft/TypeScript/pull/62350

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy