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

Regarding a comment below the video I posted 2 days ago. My case #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

zarichniuk
Copy link

Didn't have time to dive deep into the question of why it doesn't work. But I promise I'll try despite lack of that deep knowledge :)

Unless you solve it first ;)

Didn't have time to dive deep into the question of why it doesn't work. But I promise I'll try despite lack of that deep knowledge :)

Unless you solve it first ;)
@kommade
Copy link

kommade commented Nov 30, 2024

For OneOf to work, there cannot be conflicting types in the array.

This is because of the type OnlyFirst<F, S> = F & {[Key in keyof Omit<S, keyof F>]?: never};

Your example:
interface AuthenticatedUser {
user: User;
isAuthenticated: true;
isLoading: false;
}

interface UnauthenticatedUser {
user: null;
isAuthenticated: false;
isLoading: false;
}

Then F = AuthenticatedUser and S = UnauthenticatedUser. Onlyfirst returns the keys in F only.

Because user cannot be both null and User, it becomes never.

Something like this would work:

type User = {
someValue: unknown;
};

type BaseUser = {
user: User | null,
};

type AuthenticatedUser = BaseUser & {
user: User;
};

type UnauthenticatedUser = BaseUser & {
isAuthenticated: false;
};

type PendingUser = BaseUser & {
isPending: true
};

type AuthenticatedUserState = OneOf<[AuthenticatedUser, UnauthenticatedUser, PendingUser]>;

const user: AuthenticatedUserState = {
user: null,
isAuthenticated: false,
}

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.

3 participants