Make NodeHandle generic and generate .Handle members of is-guards for guarding node handles (sync and async) - #4887
Merged
Wesley Wigham (weswigham) merged 4 commits intoAug 14, 2026
Conversation
…es (sync and async)
Wesley Wigham (weswigham)
requested review from
Andrew Branch (andrewbranch) and
Jake Bailey (jakebailey)
and
a balanced review from Copilot
August 12, 2026 18:49
Contributor
There was a problem hiding this comment.
Pull request overview
Adds generic node handles and generated handle-aware AST guards for async and sync APIs.
Changes:
- Makes
NodeHandle<T>resolve typed AST nodes. - Adds
.Handlevariants to generated AST guards. - Adds async and sync narrowing coverage.
Reviewed changes
Copilot reviewed 7 out of 8 changed files in this pull request and generated 5 comments.
Show a summary per file
| File | Description |
|---|---|
_scripts/generate-ts-ast.ts |
Generates handle-aware guards. |
_packages/native-preview/src/ast/is.generated.ts |
Contains generated .Handle guards. |
_packages/native-preview/src/api/async/api.ts |
Adds generic async handles. |
_packages/native-preview/src/api/sync/api.ts |
Adds generated generic sync handles. |
_packages/native-preview/src/api/async/types.ts |
Types index declarations. |
_packages/native-preview/src/api/sync/types.ts |
Mirrors typed index declarations. |
_packages/native-preview/test/async/api.test.ts |
Tests async narrowing. |
_packages/native-preview/test/sync/api.test.ts |
Tests sync narrowing. |
💡 Add a code-review agent skill for context-aware, tailored reviews. Learn more in the docs.
| out.push(`} from "./ast.ts";`); | ||
| out.push(`import type { NodeHandle as AsyncNodeHandle } from "../api/async/api.ts";`); | ||
| out.push(`import type { NodeHandle as SyncNodeHandle } from "../api/sync/api.ts";`); | ||
| out.push(`type NodeHandleLike<out T extends Node> = { kind: SyntaxKind; resolve(...args: any): any; };`); |
Comment on lines
+1459
to
+1461
| out.push(`${g.funcName}.Handle = <T extends NodeHandleLike<Node>>(node: T): node is SpecializeNodeHandle<T, ${g.typeName}> => {`); | ||
| out.push(...functionContent); | ||
| out.push(`}`) |
There was a problem hiding this comment.
I think this is a good suggestion
Member
Author
There was a problem hiding this comment.
Sure, which is why I made it in the OP - only drawback is we lose typechecking of the NodeHandle guard implementations, so it's on us to make sure the implementation is valid for the type.
Andrew Branch (andrewbranch)
approved these changes
Aug 13, 2026
Andrew Branch (andrewbranch)
left a comment
Member
There was a problem hiding this comment.
I think I caused a merge conflict, sorry 🥴
Wesley Wigham (weswigham)
requested a review
from Andrew Branch (andrewbranch)
August 13, 2026 21:00
Wesley Wigham (weswigham)
enabled auto-merge
August 13, 2026 21:00
Andrew Branch (andrewbranch)
approved these changes
Aug 14, 2026
Jake Bailey (jakebailey)
pushed a commit
to jakebailey/TypeScript
that referenced
this pull request
Aug 14, 2026
…or guarding node handles (sync and async) (microsoft/typescript-go#4887)
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.
So you can, for example:
and narrow
sig.declaration.Note that I am duplicating the implementations for the
.Handletype guards, but we could writefunc.Handle = func as anyand it'd work just as well, if we'd prefer the brevity.