[feature request] Support type predicate getters for control flow narrowing#4409
Open
js2me wants to merge 2 commits into
Open
[feature request] Support type predicate getters for control flow narrowing#4409js2me wants to merge 2 commits into
js2me wants to merge 2 commits into
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds support for control-flow narrowing based on type predicate return types declared on getters (e.g. get hasUser(): this is ...), aligning getter truthiness checks with existing type-guard method behavior.
Changes:
- Extends control flow narrowing to resolve and apply type predicates from property/element access when the accessed member is a getter.
- Allows type predicate nodes in get-accessor return type positions during checking.
- Adds a new compiler test and reference baselines validating narrowing behavior for getter predicates.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| testdata/tests/cases/compiler/typeGuardGetterTypePredicate.ts | New regression test covering getter-based type predicate narrowing (and parity with method predicates). |
| testdata/baselines/reference/compiler/typeGuardGetterTypePredicate.types | New .types baseline validating the narrowed receiver type in truthiness checks. |
| testdata/baselines/reference/compiler/typeGuardGetterTypePredicate.symbols | New .symbols baseline for the added test. |
| internal/checker/flow.go | Implements predicate lookup for getter property access and integrates it into truthiness-based narrowing. |
| internal/checker/checker.go | Permits type predicate nodes in get-accessor return type positions. |
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
get hasUser(): this is { user: string }if (session.hasUser) { ... }, matching the behavior of type guard methods likehasUser(): this is TMotivation
Type guard methods already support
this ispredicates when called (if (session.hasUser())), but the getter form (if (session.hasUser)) was rejected or did not narrow types. This PR adds parity for the getter syntax.Example