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

IfType type guards #788

Open
ZachHaber opened this issue Jan 2, 2024 · 8 comments · May be fixed by #961
Open

IfType type guards #788

ZachHaber opened this issue Jan 2, 2024 · 8 comments · May be fixed by #961
Labels
good first issue Good for newcomers help wanted Extra attention is needed type addition

Comments

@ZachHaber
Copy link

ZachHaber commented Jan 2, 2024

The readme mentions that there should be a corresponding IfType guard for every IsType check.

For every IsT type (e.g. IsAny), there is an associated IfT type that can help simplify conditional types.

I'm only seeing a few IfType guards in the package (IfNever, IfAny, and IfUnknown).

Potential fixes (that I can think of at the moment):

  1. Add the missing IfType guards
  2. Update the readme to specifically call out those three guards

Upvote & Fund

  • We're using Polar.sh so you can upvote and help fund this issue.
  • The funding will be given to active contributors.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar
@sindresorhus
Copy link
Owner

PR welcome for types starting with Is that are missing an If type.

@sindresorhus sindresorhus added help wanted Extra attention is needed good first issue Good for newcomers type addition labels Jan 2, 2024
harsh-srivastava44 pushed a commit to harsh-srivastava44/type-fest that referenced this issue Jan 7, 2024
harsh-srivastava44 pushed a commit to harsh-srivastava44/type-fest that referenced this issue Jan 7, 2024
@sindresorhus sindresorhus pinned this issue Mar 8, 2024
@ikelax ikelax linked a pull request Sep 29, 2024 that will close this issue
@sindresorhus
Copy link
Owner

I'm starting to question whether it was the correct choice to add If equivalents. It seemed nice at first, but there is now so many extra types and all of them require extra docs and maintenance. Maybe we should instead have supported the extra parameters on Is* or just referred people to a If type. Thoughts?

// @ikelax

@sindresorhus
Copy link
Owner

// @Emiyaaaaa @voxpelli

@ikelax
Copy link

ikelax commented Oct 14, 2024

Sorry for the late response.

I also think that the If types create a lot of extra work. For me, it would be fine if we support the extra parameters on Is* or just refer people to a If type. However, I would prefer later because the name helps a lot.

@ikelax
Copy link

ikelax commented Oct 14, 2024

// @sindresorhus

@voxpelli
Copy link
Collaborator

voxpelli commented Oct 14, 2024

I'm starting to question whether it was the correct choice to add If equivalents. It seemed nice at first, but there is now so many extra types and all of them require extra docs and maintenance. Maybe we should instead have supported the extra parameters on Is* or just referred people to a If type. Thoughts?

I'm 👍 on this, and eg. the IfAny already works as IsAny if no second or third value are given:

type IfAny<T, TypeIfAny = true, TypeIfNotAny = false>

Though internally it uses IsAny:

export type IfAny<T, TypeIfAny = true, TypeIfNotAny = false> = (
IsAny<T> extends true ? TypeIfAny : TypeIfNotAny
);

I would deprecate the Is* and refer people to the If*

@sindresorhus
Copy link
Owner

I would deprecate the Is* and refer people to the If*

Why not the other way around? Is instead of If?

@voxpelli
Copy link
Collaborator

No strong opinion, but IfAny<T, Foo, Bar> is more self-explanatory than IsAny<T, Foo, Bar> I think – the If* indicates that there is some kind of if-statement and as such that the two other arguments are probably depending on the first one, whereas Is* doesn't really give any indication about that, IsAny<T, Foo, Bar> sounds like the Foo and the Bar are part of the comparison

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed type addition
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants