-
Notifications
You must be signed in to change notification settings - Fork 703
fix(checker): apply discriminant-based contextual typing for object literals (fixes #1727) #1734
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
base: main
Are you sure you want to change the base?
fix(checker): apply discriminant-based contextual typing for object literals (fixes #1727) #1734
Conversation
@microsoft-github-policy-service agree |
There was a problem hiding this 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 discriminant-based contextual typing for object literals in union types. Previously, tsgo
failed to properly type callback parameters when a discriminant property was present, unlike the reference TypeScript compiler.
- Adds reverse inference from literal types to indexed access types for discriminated unions
- Implements type narrowing based on discriminant property values in object literals
- Falls back to existing behavior when discriminants are missing or ambiguous
Co-authored-by: Copilot <[email protected]>
- Add cssTreeTypeInference.ts test case to verify discriminated union type inference - Include baseline files showing expected type inference behavior - Ensures node parameter is correctly inferred as Declaration type in contextual typing scenarios
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
…enceInfoForType - Change from TypeFlagsTypeParameter to TypeFlagsTypeVariable to match the pattern used in getInferenceInfoForType function - This ensures consistency in type flag checking across inference-related functions - TypeFlagsTypeVariable includes both type parameters and indexed access types, which is more comprehensive - Updated comment to reflect the more accurate terminology 'type variable' instead of 'type parameter'
- Move blockedStringType check to beginning of loop for early exit - Avoid expensive getIndexedAccessType() and isTypeIdenticalTo() calls when union member is blocked - Improves performance by skipping unnecessary computations for blocked string types - No functional changes, pure optimization
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Co-authored-by: Copilot <[email protected]>
Co-authored-by: Copilot <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Co-authored-by: Copilot <[email protected]>
Hello, This pull request addresses #1727 by adding discriminant-based contextual typing for
Type inference improvements:
Testing and validation:
Could you please approve the workflows and review this change? @jakebailey — for workflow approval/merge Thank you! |
@arash-mosavi Appreciate your PR, but unfortunately I don't think we want to merge it. See my explanation here: #1727 (comment). |
Summary
This PR fixes issue #1727 where
tsgo
failed to contextually type callback parameters in object literalswhen a discriminant property (e.g.,
visit: 'Declaration'
) was present. Unliketsc
,tsgo
previouslygave the
enter(node)
parameter an implicitany
type.Details
Tests
visit: 'Declaration'
) →enter(node)
is properly typed, no TS7006 error.tsc
).Notes
tsc
5.8.