Skip to content

Add inline named models support#9694

Closed
johanste wants to merge 11 commits intomicrosoft:mainfrom
johanste:copilot/add-inline-named-models-support
Closed

Add inline named models support#9694
johanste wants to merge 11 commits intomicrosoft:mainfrom
johanste:copilot/add-inline-named-models-support

Conversation

@johanste
Copy link
Copy Markdown
Contributor

Asked copilot to enable support for inline model names:

model Outer {
   inner: model Inner {
      size: int32;
   }
}

is equivalent to:

model Outer {
   inner: Inner;
}

model Inner {
  size: int32;
}

@bterlson , @witemple-msft , I'm very curious to see what ya think...

Copilot AI and others added 11 commits February 10, 2026 02:21
Add optional `id` field to `ModelExpressionNode` to support inline
named models in property type positions. Example:

```typespec
model Parent {
    child: model Child {
        age: int32;
    }
}
```

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
…rator and nesting support with tests

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
…st, response, and template parameter

Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
Co-authored-by: johanste <15110018+johanste@users.noreply.github.com>
@microsoft-github-policy-service microsoft-github-policy-service Bot added the compiler:core Issues for @typespec/compiler label Feb 11, 2026
@github-actions
Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/compiler
Show changes

@typespec/compiler - feature ✏️

Add support for inline named model expressions in property type positions

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented Feb 11, 2026

Open in StackBlitz

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

commit: 4df0e57

@azure-sdk
Copy link
Copy Markdown
Collaborator

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

Copy link
Copy Markdown
Member

@witemple-msft witemple-msft left a comment

Choose a reason for hiding this comment

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

I think this is worth doing. Not just for model expressions, but for all keyword declarations.

Comment on lines +390 to +398
// When the model expression has a name, declare it in the enclosing namespace/script scope
// so it can be referenced by name (e.g. from augment decorators).
const prevScope = scope;
scope = getEnclosingDeclarationScope();
declareSymbol(node as unknown as Declaration, SymbolFlags.Model | SymbolFlags.Declaration);
scope = prevScope;
} else {
bindSymbol(node, SymbolFlags.Model);
}
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.

I'm not sure we really want to do this for expressions. It's going to make some weird lexical behavior:

https://cadlplayground.z22.web.core.windows.net/prs/9694/?e=%40typespec%2Fopenapi3&c=bW9kZWwgWCB7CiAgeTogxg9ZxQ8gIHo6IHN0cmluZzsKICB9Owp9CgphbGlhcyBBID0gWTs%3D&options=%7B%7D&vs=%7B%7D

It's not clear to me that in this example Y should escape the scope of X.y.

function parseInlineNamedModelExpression(): ModelExpressionNode {
const pos = tokenPos();
parseExpected(Token.ModelKeyword);
const id = parseIdentifier();
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.

I'd propose making the identifier optional. Not so much because of models since we already have bare {} model literals, but for other keyword-expression forms, like enum { ... } or union { ... }. I want to give all such things expression forms, and for symmetry I think it would be nice to allow the identifier to be optional. That does pose some challenges, as I think some of our Type variants have mandatory name.

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.

yeah +1 for optional identifier so it allows the other PR to work better too

};
}

function parseInlineNamedModelExpression(): ModelExpressionNode {
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.

I feel like we also need to allow this for all the types(enum, union at least)

@timotheeguerin
Copy link
Copy Markdown
Member

From design discussion

  1. Handle enum, union, scalar?
  2. Should(n't) expose the declaration to the upper scope?
  • what happens if same name is used in 2 location(delegate to emitter to dedup?)
  1. Name should be optional model {foo: string}, enum {a, b}, etc.

@microsoft-github-policy-service microsoft-github-policy-service Bot added the stale Mark a PR that hasn't been recently updated and will be closed. label Mar 28, 2026
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

Hi @@johanste. Your PR has had no update for 30 days and it is marked as a stale PR. If it is not updated within 30 days, the PR will automatically be closed. If you want to refresh the PR, please remove the stale label.

@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

Hi @@johanste. The PR will be closed since the PR has no update for 60 days. If this is still relevant please reopen.

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 stale Mark a PR that hasn't been recently updated and will be closed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants