-
Notifications
You must be signed in to change notification settings - Fork 230
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Naoki Ikeguchi <me@s6n.jp>
Signed-off-by: Naoki Ikeguchi <me@s6n.jp>
Signed-off-by: Naoki Ikeguchi <me@s6n.jp>
Signed-off-by: Naoki Ikeguchi <me@s6n.jp>
@@ -280,7 +280,7 @@ export type NeverIndexer = { | |||
}; | |||
|
|||
export type ModelIndexer = { | |||
readonly key: Scalar; | |||
readonly key: Enum | Scalar | Union; |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so the union here actually means union of literals of enums and scalars.
if the compiler could prevent all those unexpected usage scenarios, this is actually fine
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. The compiler can check the key type for Map
types, but IIRC the compiler currently doesn't check argument types for @indexer
decorator as it's an intrinstics decorator without any declarations.
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 theMap<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 addingpropertyNames
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 theopenapi3.1
branch.