Add Either type for EventGrid output connections #199
Closed
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In PR #178 we faced the problem that there are two exclusive ways to configure an EventGrid output binding: key-based or identity-based.
However, TypeScript does not have a built-in utility type that forces users to implement one interface or another but not both. We left the code in a state where users would be warned if they:
topicEndpointUri
ortopicKeySetting
but not bothBut users were free to combine the
connection
property withtopicEndpointUri
ortopicKeySetting
or both without any warnings or errors thrown by the type checker. Our only defense against this was a comment thatWhen setting the connection property, the topicEndpointUri and topicKeySetting properties should NOT be set.
Ideally, this comment would be unnecessary and our type design would prevent any such misconfigurations.
This PR introduces a custom utility type
Either
. It takes two interfaces A and B and allows two possible implementations: all the properties in A are accepted but none of the properties in B or vice-versa.Now if a user tries to supply a
connection
property along with key-based settings, this usage will be flagged:Type '{ topicEndpointUri: string; topicKeySetting: string; connection: string; }' is not assignable to type '{ topicEndpointUri?: never; topicKeySetting?: never; }'.
Type '{ topicEndpointUri: string; connection: string; }' is not assignable to type '{ topicEndpointUri?: never; topicKeySetting?: never; }'.
Type '{ topicKeySetting: string; connection: string; }' is not assignable to type '{ topicEndpointUri?: never; topicKeySetting?: never; }'.
And the correct implementations of both
EventGridOutputKeyOptions
andEventGridOutputConnectionOptions
still work as expected: