Skip to content

[feature request] Support type predicate getters for control flow narrowing#4409

Open
js2me wants to merge 2 commits into
microsoft:mainfrom
js2me:main
Open

[feature request] Support type predicate getters for control flow narrowing#4409
js2me wants to merge 2 commits into
microsoft:mainfrom
js2me:main

Conversation

@js2me

@js2me js2me commented Jun 23, 2026

Copy link
Copy Markdown

Summary

  • Allow type predicates in getter return types, e.g. get hasUser(): this is { user: string }
  • Narrow the receiver in truthiness checks, e.g. if (session.hasUser) { ... }, matching the behavior of type guard methods like hasUser(): this is T
  • Extend control flow analysis to resolve type predicates from property access to getters

Motivation

Type guard methods already support this is predicates 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

class Session {
    user: string | null = null;

    get hasUser(): this is { user: string } {
        return this.user !== null;
    }
}

const session = new Session();

if (session.hasUser) {
    session.user.toUpperCase(); // ok: `user` is `string`
}

Copilot AI review requested due to automatic review settings June 23, 2026 12:01

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.

Comment thread internal/checker/flow.go
Comment thread internal/checker/flow.go
@js2me

js2me commented Jun 23, 2026

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

@RyanCavanaugh RyanCavanaugh added this to the Possible Improvement milestone Jun 23, 2026
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.

3 participants