Skip to content

feat(compiler): add experimental $onInfo hook and program.getTypeInfo API - #11489

Draft
timotheeguerin wants to merge 6 commits into
microsoft:mainfrom
timotheeguerin:on-info
Draft

feat(compiler): add experimental $onInfo hook and program.getTypeInfo API#11489
timotheeguerin wants to merge 6 commits into
microsoft:mainfrom
timotheeguerin:on-info

Conversation

@timotheeguerin

@timotheeguerin timotheeguerin commented Jul 30, 2026

Copy link
Copy Markdown
Member

Fixes #1993

Summary

Adds a new experimental $onInfo library hook and program.getTypeInfo(type) API that lets libraries contribute extra, domain-specific information about types. This information is surfaced in IDE hover tooltips and can be queried programmatically by tooling (e.g. AI agents).

Unlike $onValidate, this hook:

  • is never run during compilation — it is invoked lazily and on demand.
  • must not mutate the type graph — it only reads the program to answer questions about a type.

The feature is gated behind a new type-info-hook compiler feature flag.
image

… API

Add a new experimental `$onInfo` library hook (gated behind the `type-info-hook`
compiler feature flag) and `program.getTypeInfo(type)` API allowing libraries to
contribute extra, domain-specific info about types for IDE hover and tooling. Wire it
into LSP hover and add a `@typespec/http` provider surfacing verb + URI template +
response status codes.
@microsoft-github-policy-service microsoft-github-policy-service Bot added compiler:core Issues for @typespec/compiler lib:http meta:website TypeSpec.io updates labels Jul 30, 2026
@pkg-pr-new

pkg-pr-new Bot commented Jul 30, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/bundler@11489
npm i https://pkg.pr.new/@typespec/compiler@11489
npm i https://pkg.pr.new/@typespec/http@11489

commit: bfd1778

@github-actions

github-actions Bot commented Jul 30, 2026

Copy link
Copy Markdown
Contributor

All changed packages have been documented.

  • @typespec/bundler
  • @typespec/compiler
  • @typespec/http
Show changes

@typespec/bundler - fix ✏️

Include a library's own tspconfig.yaml in the generated bundle so per-library opt-ins (such as compiler features) are preserved when the library is loaded in the browser (e.g. in the playground).

@typespec/compiler - feature ✏️

Add a new experimental $onInfo library hook and program.getTypeInfo(type) API allowing libraries to contribute extra, domain-specific information about types. Unlike $onValidate, this hook never runs during compilation and must not mutate the type graph — it is invoked lazily and on demand (e.g. by the language server for hover documentation, or by tooling querying the type).,> ,> The hook is gated by the type-info-hook compiler feature, scoped to the package that declares it: a library opts in via its own tspconfig.yaml and consumers do not need to enable anything.,> ,> ts,> // A library exports a provider (use `defineInfoHook` for typing):,> export const $onInfo = defineInfoHook(({ program, target }) => {,> if (target.kind !== "Operation") return undefined;,> return { content: "extra info about this operation" };,> });,> ,> // Tooling / language server queries it (merges every library's contribution):,> const info = program.getTypeInfo(type);,>

@typespec/http - feature ✏️

Add an $onInfo provider that surfaces the resolved HTTP route (verb and URI template) and response status codes of an operation. This is shown when hovering an operation in the IDE and can be queried programmatically via program.getTypeInfo(operation).,> ,> ts,> const info = program.getTypeInfo(operation);,> // { content: "`HTTP Route`: `GET /pets/{id}`\n\n`Responses`: `204`" },>

@azure-sdk-automation

azure-sdk-automation Bot commented Jul 30, 2026

Copy link
Copy Markdown

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

The `type-info-hook` feature is now checked against the package that declares the
$onInfo hook rather than the consuming project. Libraries opt in via their own
tspconfig.yaml and consumers see the info without enabling anything. Enable it in
@typespec/http and ship library tspconfig.yaml in bundles so it works in the playground.
Comment thread packages/compiler/src/core/program.ts Outdated
Comment thread packages/http/test/info.test.ts Outdated
@@ -0,0 +1,93 @@
---
id: providing-info

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.

can we add experimental badge to sidebar?

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.

Done — the sidebar entry is now a SlugItem with badge: { text: "experimental", variant: "caution" } (matching the variant used for alpha/beta libraries).

- Report a dedicated `on-info-fail` diagnostic when an `$onInfo` provider
  crashes during a design-time build, instead of reusing the misleading
  `on-validate-fail` code.
- Use vitest `expect` in the new info hook tests.
- Badge the "Providing info" docs page as experimental in the sidebar.
The `type-info-hook` opt-in is resolved from the declaring package's own
config, but `tspconfig.yaml` was missing from `package.json#files`, so the
hook was silently dropped for anyone installing @typespec/http from a
registry. It only ever worked inside the monorepo.

Other review feedback:

- `getTypeInfo` no longer reports diagnostics after compilation finished; it
  traces instead, so a crashing provider cannot accumulate duplicates on a
  cached Program or flip `continueToNextStage`. Drops the now unused
  `on-info-fail` diagnostic.
- Fix the `defineInfoHook` JSDoc example, which returned an array and did not
  type check against `OnInfoHook`.
- Add an end-to-end test exercising the real registration path, plus coverage
  for the status code range and `*` branches.
- Fix a broken link to the configuration docs and document that a library must
  ship its `tspconfig.yaml`.
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 lib:http meta:website TypeSpec.io updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mechanism for libraries to add information to IDE documentation/tooltip

1 participant