Skip to content

Commit f7bc576

Browse files
committed
Add IsUndefined type
Fixes #1212
1 parent 5f830b5 commit f7bc576

File tree

4 files changed

+32
-0
lines changed

4 files changed

+32
-0
lines changed

index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ export type {SharedUnionFields} from './source/shared-union-fields.d.ts';
145145
export type {SharedUnionFieldsDeep} from './source/shared-union-fields-deep.d.ts';
146146
export type {IsNull} from './source/is-null.d.ts';
147147
export type {IfNull} from './source/if-null.d.ts';
148+
export type {IsUndefined} from './source/is-undefined.d.ts';
148149
export type {And} from './source/and.d.ts';
149150
export type {Or} from './source/or.d.ts';
150151
export type {AllExtend} from './source/all-extend.d.ts';

readme.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,7 @@ Click the type names for complete docs.
199199
- [`IsUnknown`](source/is-unknown.d.ts) - Returns a boolean for whether the given type is `unknown`.
200200
- [`IsEmptyObject`](source/empty-object.d.ts) - Returns a boolean for whether the type is strictly equal to an empty plain object, the `{}` value.
201201
- [`IsNull`](source/is-null.d.ts) - Returns a boolean for whether the given type is `null`.
202+
- [`IsUndefined`](source/is-undefined.d.ts) - Returns a boolean for whether the given type is `undefined`.
202203
- [`IsTuple`](source/is-tuple.d.ts) - Returns a boolean for whether the given array is a tuple.
203204
- [`IsUnion`](source/is-union.d.ts) - Returns a boolean for whether the given type is a union.
204205
- [`IsLowercase`](source/is-lowercase.d.ts) - Returns a boolean for whether the given string literal is lowercase.

source/is-undefined.d.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/**
2+
Returns a boolean for whether the given type is `undefined`.
3+
4+
@example
5+
```
6+
import type {IsUndefined} from 'type-fest';
7+
8+
type UndefinedFallback<T, Fallback> = IsUndefined<T> extends true ? Fallback : T;
9+
10+
type Example1 = UndefinedFallback<undefined, string>;
11+
//=> string
12+
13+
type Example2 = UndefinedFallback<number, string>;
14+
//=> number
15+
```
16+
17+
@category Type Guard
18+
@category Utilities
19+
*/
20+
export type IsUndefined<T> = [T] extends [undefined] ? true : false;

test-d/is-undefined.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import {expectType} from 'tsd';
2+
import type {IsUndefined} from '../source/is-undefined.d.ts';
3+
4+
expectType<IsUndefined<undefined>>(true);
5+
expectType<IsUndefined<any>>(true);
6+
expectType<IsUndefined<never>>(true);
7+
expectType<IsUndefined<null>>(false); // Depends on `strictNullChecks`
8+
expectType<IsUndefined<unknown>>(false);
9+
expectType<IsUndefined<void>>(false);
10+
expectType<IsUndefined<{}>>(false);

0 commit comments

Comments
 (0)