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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ export type {HasRequiredKeys} from './source/has-required-keys';
export type {Spread} from './source/spread';
export type {TupleToUnion} from './source/tuple-to-union';
export type {IsEqual} from './source/is-equal';
export type {IsUnion} from './source/is-union';

// Template literal types
export type {CamelCase} from './source/camel-case';
Expand Down
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ Click the type names for complete docs.
- [`HasRequiredKeys`](source/has-required-keys.d.ts) - Create a `true`/`false` type depending on whether the given type has any required fields.
- [`Spread`](source/spread.d.ts) - Mimic the type inferred by TypeScript when merging two objects or two arrays/tuples using the spread syntax.
- [`IsEqual`](source/is-equal.d.ts) - Returns a boolean for whether the two given types are equal.
- [`IsUnion`](source/is-union.d.ts) - Detects whether a given type is a union of types.

### JSON

Expand Down
24 changes: 24 additions & 0 deletions source/is-union.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type {UnionToIntersection} from './union-to-intersection';

/**
Detects whether a given type is a union of types.

While you can default to returning a boolean based on the condition, the resulting typing will be simpler if you instead specify the `True` and `False` type parameters.

Note that `boolean` and `enum` types are unions.

Use-cases:
- You want to ensure a given type is not a union but a singular type.

@example
```
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.

```

@category Utilities
*/
export type IsUnion<MaybeUnion, True = true, False = false> =
[MaybeUnion] extends [UnionToIntersection<MaybeUnion>] ? False : True;
28 changes: 28 additions & 0 deletions test-d/is-union.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {expectType} from 'tsd';
import type {IsUnion} from '../index';

declare function isUnion<T>(): IsUnion<T>;

enum Nums {
ONE,
TWO,
}

// Non-union types.
expectType<false>(isUnion<1>());
expectType<false>(isUnion<number>());
expectType<false>(isUnion<string>());
expectType<false>(isUnion<`#${number}`>());

// Unions of self are not unions.
expectType<false>(isUnion<1 | 1>());

// Union types.
expectType<true>(isUnion<1 | 2>());
expectType<true>(isUnion<number | string>());
expectType<true>(isUnion<'a' | 'b'>());
expectType<true>(isUnion<Nums.ONE | Nums.TWO>());

// Surprising union types.
expectType<true>(isUnion<boolean>());
expectType<true>(isUnion<Nums>());