-
Notifications
You must be signed in to change notification settings - Fork 245
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Helper to construct TypeSpec unions out of existing models #1811
Comments
A library to aid in constructing ephemeral types for emitters would definitely be nice. That said, you can fairly easily do the same code the checker does for creating a union, since createType is exported, I think? |
It doesn't look like |
It is, on the checker instance: typespec/packages/compiler/core/checker.ts Line 362 in 7195605
|
@bterlson I tried this and it didn't work for the reasons that it seems to need a bunch of underlying TypeSpec nodes and so forth:
I get an error on the last line: |
this is covered by the typekits |
Background
PR #1770 required logic that would essentially take the response types of multiple operations with shared routes and emit them as "oneOf" properties in OpenAPI3. That should have been an easy task, especially since the OpenAPI3 emitter already had logic to get the appropriate OpenAPI3 schema for existing TypeSpec types, including Union.
However, this proved infuriatingly difficult because there was no way to simply take two models and say "Please give me a TypeSpec Union of these" that I could pass to that existing logic. If you can't work with the TypeSpec types as output by the compiler, you are screwed and have to invent a bunch of intermediate models to facilitate this. It ended up being way more work than it seems like it should have.
Request
It would be nice to have some kind of helper method that takes an arbitrary number of TypeSpec types and returns a TypeSpec.Union of them, but without all the type graph links that make trying to construct one of these programmatically pretty much impossible (nodes, parents, etc.).
Perhaps there could be a way to generalize this, but essentially constructing TypeSpec types seems to only be possible via the compiler, and it would be useful to be able to create them programmatically as well.
The text was updated successfully, but these errors were encountered: