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

Add Either type for EventGrid output connections #199

Closed
wants to merge 2 commits into from
Closed

Conversation

castrodd
Copy link
Member

@castrodd castrodd commented Nov 22, 2023

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:

  1. Used topicEndpointUri or topicKeySetting but not both
  2. Did not specify any properties

But users were free to combine the connection property with topicEndpointUri or topicKeySetting or both without any warnings or errors thrown by the type checker. Our only defense against this was a comment that When 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:

image
Type '{ topicEndpointUri: string; topicKeySetting: string; connection: string; }' is not assignable to type '{ topicEndpointUri?: never; topicKeySetting?: never; }'.

image
Type '{ topicEndpointUri: string; connection: string; }' is not assignable to type '{ topicEndpointUri?: never; topicKeySetting?: never; }'.

image
Type '{ topicKeySetting: string; connection: string; }' is not assignable to type '{ topicEndpointUri?: never; topicKeySetting?: never; }'.

And the correct implementations of both EventGridOutputKeyOptions and EventGridOutputConnectionOptions still work as expected:

image

image

types/utils/either.ts Show resolved Hide resolved
types/utils/either.ts Show resolved Hide resolved
@castrodd castrodd closed this Dec 1, 2023
@castrodd castrodd deleted the dc/eitherOr branch December 1, 2023 17:50
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