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

Support for RequiredExactlyOne #70

Closed
haejunejung opened this issue Sep 5, 2024 · 0 comments · Fixed by #74
Closed

Support for RequiredExactlyOne #70

haejunejung opened this issue Sep 5, 2024 · 0 comments · Fixed by #74
Labels
enhancement New feature or request

Comments

@haejunejung
Copy link
Owner

haejunejung commented Sep 5, 2024

This introduces support for RequiredExactlyOne.

Sometimes, we want to enforce that only one property from a set of properties can be used at a time.

For example:

type A = { a: string };
type B = { b: string };

function doSomething(input: A | B) {
  // do something
}

In this case, the union type A | B allows us to pass either A or B, but it also allows both properties (a and b) to coexist, which might not be what we want.

To ensure that exactly one property is used, we can use RequiredExactlyOne.

type Example = {
	a: () => string;
	b: () => string;
};

const example: RequiredExactlyOne<Example, 'a' | 'b'> = {
	// Adding both `a` and `b` would cause a compile error.
	b: () => { console.log(1); }
};

RequiredExactlyOne ensures that only one of the specified properties is present, preventing potential conflicts when multiple properties could be passed at the same time.

@haejunejung haejunejung added the enhancement New feature or request label Sep 5, 2024
@haejunejung haejunejung changed the title Support for RequireExactlyOne Support for RequiredExactlyOne Sep 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant