Skip to content

[api] Add type.isClass() predicate guard - #65584

Closed
Andrew Branch (andrewbranch) wants to merge 1 commit into
microsoft:mainfrom
andrewbranch:program-type-apis
Closed

Andrew Branch (andrewbranch) wants to merge 1 commit into
microsoft:mainfrom
andrewbranch:program-type-apis

Conversation

@andrewbranch

@andrewbranch Andrew Branch (andrewbranch) commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Found by #64749

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>

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.

🟢 Approval recommended

No active review issues remain for the async and sync Type.isClass() API additions.

No open issues 🎉

What changed in this PR

Adds Type.isClass() to the TypeScript JavaScript API, allowing callers to identify originating class types and narrow them to InterfaceType.

Changes:

  • Adds the type guard and documentation to both async and sync APIs.
  • Adds tests covering classes, interfaces, generic specializations, constructors, ordinary values, and type narrowing.
File Description
packages/​typescript/​test/​sync/​api.test.ts Tests synchronous class detection and type narrowing.
packages/​typescript/​test/​async/​api.test.ts Tests asynchronous class detection and type narrowing.
packages/​typescript/​src/​api/​sync/​types.ts Declares and documents the sync type guard.
packages/​typescript/​src/​api/​sync/​api.ts Implements sync class detection using object flags.
packages/​typescript/​src/​api/​async/​types.ts Declares and documents the async type guard.
packages/​typescript/​src/​api/​async/​api.ts Implements async class detection using object flags.

Review effort: Balanced

@andrewbranch Andrew Branch (andrewbranch) changed the title [api] Program and Type APIs [api] Add type.isClass() predicate guard Nov 19, 2026
@andrewbranch
Andrew Branch (andrewbranch) marked this pull request as ready for review November 19, 2026 19:50
@andrewbranch

Copy link
Copy Markdown
Member Author

This is an exact copy from Strada, but because class types and interface types are both InterfaceType just with different objectFlags, this has bad narrowing behavior in the false branch:

declare let type: InterfaceType | UnionType;
if (!type.isClass()) {
  type; // UnionType - wrong, could be a non-classy interface
}

I think it's worth fixing that mistake from the old API and just excluding this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Author: Team For Uncommitted Bug PR for untriaged, rejected, closed or missing bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants