Skip to content

Fix types for path.get("body.${index}")#17829

Merged
nicolo-ribaudo merged 4 commits intobabel:mainfrom
liuxingbaoyu:fix-get-temp
Mar 4, 2026
Merged

Fix types for path.get("body.${index}")#17829
nicolo-ribaudo merged 4 commits intobabel:mainfrom
liuxingbaoyu:fix-get-temp

Conversation

@liuxingbaoyu
Copy link
Copy Markdown
Member

Q                       A
Fixed Issues? Fixes #1, Fixes #2
Patch: Bug Fix?
Major: Breaking Change?
Minor: New Feature?
Tests Added + Pass? Yes
Documentation PR Link
Any Dependency Changes?
License MIT

I discovered this while working on #17828.

@babel-bot
Copy link
Copy Markdown
Collaborator

babel-bot commented Feb 24, 2026

Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61070

it("should support template literal expressions - number", () => {
const path = {} as NodePath<t.BlockStatement>;
const index: number = 0;
const statement = path.get(`body.${index}`);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I really hope that get only returns NodePath and not NodePath[].
Because technically path.get("x.${y}") could also return NodePath[], but that's rare and more complicated, and I'm hesitant to support it.

@JLHwung JLHwung added the PR: Internal 🏠 A type of pull request used for our changelog categories label Feb 24, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new bot commented Feb 24, 2026

Open in StackBlitz

commit: 276667d

Copy link
Copy Markdown
Contributor

@JLHwung JLHwung left a comment

Choose a reason for hiding this comment

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

Marked this PR as internal because it improves the typings. The path.get function is not affected.

@liuxingbaoyu
Copy link
Copy Markdown
Member Author

This is more likely a fix in the next major release, because until then it would return NodePath<null>. :)

@liuxingbaoyu liuxingbaoyu added PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release pkg: traverse and removed PR: Internal 🏠 A type of pull request used for our changelog categories labels Feb 26, 2026
@nicolo-ribaudo nicolo-ribaudo changed the title fix: Support path.get("body.${index}") Fix types for path.get("body.${index}") Feb 27, 2026
: TravD<Node[K] & {}, R>
: never
: null
: `${any}` extends K
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Is this any different from just string extends K?

Copy link
Copy Markdown
Member Author

@liuxingbaoyu liuxingbaoyu Feb 27, 2026

Choose a reason for hiding this comment

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

${any} can allow ${number}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

${any} can allow number

It can allow any number casted to a string, right? Doesn't string also allow anything casted to a string?

I'm finding this very confusing overall: https://www.typescriptlang.org/play/?#code/DYUwLgBAhgXBAGASA3lAdgTwL7wgXggCJDoBnBAMwHsqVSwAnASzQHMcBuAKFEgCM4SVJhz4iJKOSH1mbTj3AQAxoJTpsuAsTIQZLVtyA

What about only supporting the ${number} case here, and users that have string-based template need to type them like

path.get("body.${foo as "body" | "somethingElse"})

to get proper type? That would probably then also give them NodePath[] when needed.

Copy link
Copy Markdown
Member Author

@liuxingbaoyu liuxingbaoyu Feb 27, 2026

Choose a reason for hiding this comment

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

There's a string type test above, and it passed. :)
I don't know why TypeScript is complaining about assignment, but the following code works.
type a = `${any}` extends `${string}` ? true : false;
This code would be false, but we've already split it beforehand.
type c = `${any}` extends `x.${string}` ? true : false;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

There's a string type test above, and it passed. :)

I mean, something like

({} as t.FunctionDeclaration).get(`body.${"" as "body"}`)

that, thanks to the explicit cast, returns NodePath[].

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This seems to work, and I added a test.

it("should support template literal expressions - number", () => {
const path = {} as NodePath<t.BlockStatement>;
const index: number = 0;
const statement = path.get(`body.${index}`);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In this case, what is the result of Split<...>? Is it not ["body", number], same as it would be for Split<"body.0">?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

My bad, they should be the same.

Copy link
Copy Markdown
Member

@nicolo-ribaudo nicolo-ribaudo left a comment

Choose a reason for hiding this comment

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

Thanks, this looks good to me now.

@nicolo-ribaudo nicolo-ribaudo merged commit b6b75bf into babel:main Mar 4, 2026
55 checks passed
@nicolo-ribaudo nicolo-ribaudo added the PR: Bug Fix 🐛 A type of pull request used for our changelog categories label Mar 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pkg: traverse PR: Bug Fix 🐛 A type of pull request used for our changelog categories PR: Bug Fix (next major) 🐛 A type of pull request used for our changelog categories for next major release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants