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

XOR based on keys #29

Open
Birkbjo opened this issue Oct 5, 2023 · 1 comment
Open

XOR based on keys #29

Birkbjo opened this issue Oct 5, 2023 · 1 comment
Labels
enhancement New feature or request

Comments

@Birkbjo
Copy link

Birkbjo commented Oct 5, 2023

Hello, thanks for this utility type. However, my usecase is usually to create a type of an object where only certain keys of that same object are mutually exclusive. I wonder if there's any interested to include a type for this? I know it's quite easily possible by creating new types of the object (using omit for instance), but an "API" like this would be a bit easier:

type MutuallyExclusive<T, U extends keyof T> = ...

type BaseButtonProps = {
    primary?: boolean
    destructive?: boolean
    secondary?: boolean
    type: string
}

type ButtonProps = MutuallyExclusive<BaseButtonProps, 'primary' | 'secondary' | 'destructive'>

I created a helper type for this. I've done limited testing, but it seems to work for my use case at least. Would this be something that would be useful to include in this repo?

type MutuallyExlusive<T extends object, U extends keyof T> = Prettify<
    T &
        {
            [K in U]: WithoutObj<Pick<T, U>, Pick<T,K>> & T
        }[U]
>
@Birkbjo
Copy link
Author

Birkbjo commented Oct 5, 2023

Closing, because I found some bugs in my implementation. Might reopen if I manage to solve them

Edit: I managed to simplify the utility type, which seems to have fixed the issues I faced.

@Birkbjo Birkbjo closed this as completed Oct 5, 2023
@Birkbjo Birkbjo reopened this Oct 6, 2023
@maninak maninak added the enhancement New feature or request label Oct 6, 2023
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

No branches or pull requests

2 participants