Skip to content

[compiler] Identifier interpolation#9791

Draft
witemple-msft wants to merge 5 commits intomicrosoft:mainfrom
witemple-msft:witemple-msft/interpolation
Draft

[compiler] Identifier interpolation#9791
witemple-msft wants to merge 5 commits intomicrosoft:mainfrom
witemple-msft:witemple-msft/interpolation

Conversation

@witemple-msft
Copy link
Member

WIP

This PR enables interpolation of string values into identifiers in many cases, reusing the existing syntax for keyword escapes.

  • Type declaration identifiers: model, union, enum, interface, etc. (with the limitation that these are not possible to reference by identifier, but they can still be declared. But NOT namespaces as this is just too fraught.
  • Member identifiers: interface operations, model properties, enum members.

⚠️ This was 100% generated by the copilot CLI as an experiment.

@microsoft-github-policy-service microsoft-github-policy-service bot added the compiler:core Issues for @typespec/compiler label Feb 24, 2026
@pkg-pr-new
Copy link

pkg-pr-new bot commented Feb 24, 2026

Open in StackBlitz

npm i https://pkg.pr.new/microsoft/typespec/@typespec/compiler@9791

commit: 4cf581d

@github-actions
Copy link
Contributor

All changed packages have been documented.

  • @typespec/compiler
Show changes

@typespec/compiler - feature ✏️

Added the ability to interpolate identifiers in most positions (model, property, enum, union, scalar, operation names, etc.). To compute an identifier, use backticks as if creating a templated string type (e.g. const v = "Model"; model \My${v} { ... }).

@azure-sdk
Copy link
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

// Temporary solution to allow reserved keywords as identifiers in certain contexts. This should get expanded to a more general solution per keyword category.
allowReservedIdentifier?: boolean;
}): IdentifierNode {
if (token() === Token.StringTemplateHead) {
Copy link
Member Author

Choose a reason for hiding this comment

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

I think I don't like this way of implementing this. It seems too fragile to allow this anywhere we call parseIdentifier... I think it would be better to have a parseIdentifierOrInterpolated and only call that from syntactic positions where it should be allowed.

Copy link
Member Author

Choose a reason for hiding this comment

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

On the other hand diagnostic behavior is probably better this way, since we get a clean "interpolation not allowed here" error instead of "unexpected token"...

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

Labels

compiler:core Issues for @typespec/compiler

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants