Skip to content

Conversation

@ahejlsberg
Copy link
Member

Fixes #944.

@ahejlsberg ahejlsberg requested review from Copilot and jakebailey May 28, 2025 01:21
Copy link
Contributor

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 adds a new exhaustive‐narrowing switch test for discriminant properties and corrects the logic in the type‐narrowing flow to properly handle missing discriminant types.

  • Introduce switchExhaustiveNarrowing.ts to verify exhaustive checks including undefined
  • Update baselines for .types and .symbols to reflect the new test
  • Change narrowTypeByDiscriminant in flow.go to replace a nil check with an explicit fallback to unknownType

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
testdata/tests/cases/compiler/switchExhaustiveNarrowing.ts Add a test covering exhaustive switch on source?.type
testdata/baselines/reference/compiler/switchExhaustiveNarrowing.types Update expected type printing for the new test
testdata/baselines/reference/compiler/switchExhaustiveNarrowing.symbols Update symbol output for the new test
internal/checker/flow.go Fix logic in narrowTypeByDiscriminant to handle nil via unknownType
Comments suppressed due to low confidence (1)

internal/checker/flow.go:720

  • [nitpick] Add a comment explaining why we use unknownType as a fallback for missing discriminants, to clarify the intended behavior when the property does not exist.
return c.filterType(t, func(t *Type) bool {

@ahejlsberg ahejlsberg added this pull request to the merge queue May 28, 2025
Merged via the queue into main with commit 8202302 May 28, 2025
23 checks passed
@jakebailey jakebailey deleted the fix-944 branch June 2, 2025 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Does not completely narrow type through null coalesce operator in switch

3 participants