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 IsUnion type #544

Closed
wants to merge 2 commits into from
Closed

Add IsUnion type #544

wants to merge 2 commits into from

Conversation

trevorade
Copy link
Contributor

@trevorade trevorade commented Jan 14, 2023

Permits detecting if a given type is a singular type or a union of types. Useful when you only want to accept a union of types or a single type.

This commit uses a pattern where, instead of returning true and false, the caller can specify the result in the true and false cases. Effectively, you can write:

IsUnion<T, IfTrue, IfFalse>

instead of:

IsUnion<T> extends true ? IfTrue : IfFalse

Fixes #236

trevorade and others added 2 commits January 13, 2023 17:20
Permits detecting if a given type is a singular type or a union of types. Useful when you only want to accept a union of types or a single type.

This commit uses a pattern where, instead of returning `true` and `false`, the caller can specify the result in the true and false cases. Effectively, you can write:

```ts
IsUnion<T, IfTrue, IfFalse>
```

instead of:

```ts
IsUnion<T> extends true ? IfTrue : IfFalse
```
import type {IsUnion} from 'type-fest';

// If `MaybeSingle` is a union type, resolves to `never`, otherwise MaybeSingle.
type EnsureSingleType<MaybeSingle> = IsUnion<MaybeSingle, never, MaybeSingle>;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like a more real-world example.

@trevorade
Copy link
Contributor Author

Opting to drop this. I do use this in the codebase I work on but I'm fine keeping local there.

@trevorade trevorade closed this Mar 22, 2023
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.

Proposal: IsUnion
2 participants