Skip to content

Discard types that reduce to never before discriminating by discriminable items #62275

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Andarist
Copy link
Contributor

fixes #62256

@Copilot Copilot AI review requested due to automatic review settings August 11, 2025 22:20
@github-project-automation github-project-automation bot moved this to Not started in PR Backlog Aug 11, 2025
@typescript-bot typescript-bot added the For Uncommitted Bug PR for untriaged, rejected, closed or missing bug label Aug 11, 2025
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 fixes an issue where TypeScript's type discrimination was not properly handling union types that reduce to never. The fix ensures that types reducing to never are discarded before performing discrimination by discriminable items.

  • Adds a compiler test case demonstrating the fix for contextual typing by discriminable unions
  • Updates the type checker to filter out types that reduce to never during discrimination
  • Includes baseline test files showing the expected type behavior

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/cases/compiler/contextuallyTypedByDiscriminableUnion2.ts New test case demonstrating the fix for discriminable union typing with never-reducing types
tests/baselines/reference/contextuallyTypedByDiscriminableUnion2.types Expected type output baseline for the new test case
tests/baselines/reference/contextuallyTypedByDiscriminableUnion2.symbols Expected symbol output baseline for the new test case
src/compiler/checker.ts Core fix that excludes never-reducing types during discrimination

interface EnableA {
readonly enableA: true;
// this introduces a conflicting property with some of the other members of MyComponentProps
// making relevant final union members reduced nevers
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

The comment should be more specific about which members have conflicting properties and how this leads to never types. Consider explaining the specific conflict between EnableA requiring enableB: true and DisableB requiring enableB: false.

Suggested change
// making relevant final union members reduced nevers
// This introduces a conflicting property: EnableA requires enableB: true,
// but DisableB requires enableB: false. As a result, any union member combining
// EnableA and DisableB is reduced to never, since enableB cannot be both true and false.

Copilot uses AI. Check for mistakes.

interface EnableA {
readonly enableA: true;
// this introduces a conflicting property with some of the other members of MyComponentProps
// making relevant final union members reduced nevers
Copy link
Preview

Copilot AI Aug 11, 2025

Choose a reason for hiding this comment

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

This comment contains unclear language. 'reduced nevers' should be 'reduce to never' for grammatical correctness and clarity.

Suggested change
// making relevant final union members reduced nevers
// making relevant final union members reduce to never

Copilot uses AI. Check for mistakes.

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.

Typescript 5.9 can no longer derive this type, becomes 'any' instead
2 participants