Skip to content

Discriminant overlap in inference: distribute source, use comparability, optional fast path - #5

Merged
Andarist merged 2 commits into
fix-go/contextual-inference-discriminantsfrom
experiment/discriminant-overlap-rework
Oct 22, 2026
Merged

Andarist merged 2 commits into
fix-go/contextual-inference-discriminantsfrom
experiment/discriminant-overlap-rework

Conversation

@Andarist

Copy link
Copy Markdown
Owner

Stacked on fix-go/contextual-inference-discriminants (microsoft#64695). Four commits, meant to be read in order:

  1. Tests. The optional/indexed discriminant tests, five return-type inference cases (broadContext, partialTagContext, unionTagContext, indexedContext, eitherContext), and classExtendedDiscriminatedUnionInference.ts ported from During inference discriminate the target type before falling back to close-matching types microsoft/TypeScript#43853. Baselines reflect the PR as it stands, so the regressions against main are visible in this commit.
  2. Distribute the source discriminant. The source-union filter was relating the whole source constituent property type to single units of the target property type, so any union-typed source discriminant (tag?: "a", ok: true | undefined, index signatures) never matched. Swapping which side gets distributed fixes everything except broadContext.
  3. Comparability. The inference filter runs in both directions (argument inference checks source into target, return-type inference checks target into source), so one assignability direction is wrong for one of them. broadContext (kind: string contextual constituent) is the case assignability cannot get right. Comparability is the complement relation; relation checking and contextual typing keep assignability since their direction is fixed.
  4. Optional fast path. Two optional discriminants always overlap under strictNullChecks, so skip the property type resolution and the comparisons. This commit moves no baselines.

Full local compiler suite passes on the tip. Compare against the PR branch: fix-go/contextual-inference-discriminants...experiment/discriminant-overlap-rework

@Andarist
Andarist force-pushed the experiment/discriminant-overlap-rework branch from f2f1fbe to e045b7a Compare October 22, 2026 07:22
@Andarist
Andarist merged commit e045b7a into fix-go/contextual-inference-discriminants Oct 22, 2026
2 checks passed
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.

1 participant