Skip to content
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

feat(compiler): Bring back the Map<K, V> type #5573

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

siketyan
Copy link

@siketyan siketyan commented Jan 12, 2025

Related Issue: #2842

I was wondering how to emit propertyNames keyword of a object schema from TypeSpec. Then I found #2842. After looked up #2842 (comment), #571, and #1537, I noticed the previously there was the Map<K, V> type. (I couldn't catch exactly why it has been removed)

Because of lacking the key type of Record<T>, we cannot inject some custom types as the keys of the object. As @timotheeguerin suggested, bringing back the Map type should be the start point for adding propertyNames support.

As the current main branch of TypeSpec doesn't support OpenAPI 3.1 yet, I postponed to add emitting support of propertyNames keyword. Once this pull request got merged, I will prepare it, targeting the openapi3.1 branch.

@@ -280,7 +280,7 @@ export type NeverIndexer = {
};

export type ModelIndexer = {
readonly key: Scalar;
readonly key: Enum | Scalar | Union;
Copy link
Member

Choose a reason for hiding this comment

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

does this mean the key here could be an enum, or scalar or union of enums and/or scalars?
How should we understand the Union here?
would a union of models be OK here?
Also I did not see any test cases in this PR when key is an enum or union, therefore I am really curious what the union here stands for

Copy link
Author

Choose a reason for hiding this comment

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

I added Union here for inline enum-like types, such as "dog" | "cat". AFAIK "dog" will be a Scalar type but "dog" | "cat" sill be a Union type.

Union of models won't be ok here, at least the key type must be assignable to string for Maps, by the extends keyword. However the ModelIndexer type cannot be constrained to such a limitation.

Please let me know if there is a better approach! 🙏 I will add missing test cases for enums though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants