Skip to content

Commit

Permalink
Add NonNull
Browse files Browse the repository at this point in the history
  • Loading branch information
xiaoxiangmoe committed Mar 28, 2020
1 parent ba66c89 commit 8dd7b16
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 3 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ We are open for contributions. If you're planning to contribute please make sure
* [`Extract<A, B>`](#extracta-b) _(built-in)_
* [`NonNullable<T>`](#nonnullablea) _(built-in)_
* [`NonUndefined<T>`](#nonundefineda)
* [`NonNull<T>`](#nonnulla)

## Object operators

Expand Down Expand Up @@ -281,6 +282,12 @@ Exclude `undefined` from set `A`

[⇧ back to top](#table-of-contents)

### `NonNull<A>`

Exclude `null` from set `A`

[⇧ back to top](#table-of-contents)

### `Exclude<A, B>`

Exclude subset `B` from set `A`
Expand Down
4 changes: 4 additions & 0 deletions src/__snapshots__/mapped-types.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,10 @@ exports[`MutableKeys testType<MutableKeys<ReadWriteProps>>() (type) should match
exports[`NonFunctionKeys testType<NonFunctionKeys<MixedProps>>() (type) should match snapshot 1`] = `"\\"name\\" | \\"someKeys\\""`;
exports[`NonNull testType<NonNull<null>>() (type) should match snapshot 1`] = `"never"`;
exports[`NonNull testType<NonNull<string | null | undefined>>() (type) should match snapshot 1`] = `"string | undefined"`;
exports[`NonUndefined testType<NonUndefined<string | null | undefined>>() (type) should match snapshot 1`] = `"string | null"`;
exports[`NonUndefined testType<NonUndefined<undefined>>() (type) should match snapshot 1`] = `"never"`;
Expand Down
9 changes: 9 additions & 0 deletions src/mapped-types.spec.snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SymmetricDifference,
FunctionKeys,
NonUndefined,
NonNull,
NonFunctionKeys,
Omit,
PickByValue,
Expand Down Expand Up @@ -104,6 +105,14 @@ type RequiredOptionalProps = {
testType<NonUndefined<undefined>>();
}

// @dts-jest:group NonNull
{
// @dts-jest:pass:snap -> string | undefined
testType<NonNull<string | null | undefined>>();
// @dts-jest:pass:snap -> never
testType<NonNull<null>>();
}

// @dts-jest:group FunctionKeys
{
// @dts-jest:pass:snap -> "setName" | "someFn"
Expand Down
9 changes: 9 additions & 0 deletions src/mapped-types.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
SymmetricDifference,
FunctionKeys,
NonUndefined,
NonNull,
NonFunctionKeys,
Omit,
PickByValue,
Expand Down Expand Up @@ -104,6 +105,14 @@ type RequiredOptionalProps = {
testType<NonUndefined<undefined>>();
}

// @dts-jest:group NonNull
{
// @dts-jest:pass:snap
testType<NonNull<string | null | undefined>>();
// @dts-jest:pass:snap
testType<NonNull<null>>();
}

// @dts-jest:group FunctionKeys
{
// @dts-jest:pass:snap
Expand Down
15 changes: 12 additions & 3 deletions src/mapped-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,25 @@ export type SymmetricDifference<A, B> = SetDifference<A | B, A & B>;
* @desc Exclude undefined from set `A`
* @example
* // Expect: "string | null"
* SymmetricDifference<string | null | undefined>;
* NonUndefined<string | null | undefined>;
*/
export type NonUndefined<A> = A extends undefined ? never : A;
export type NonUndefined<A> = Exclude<A, undefined>;

/**
* NonUndefined
* @desc Exclude undefined from set `A`
* @example
* // Expect: "string | undefined"
* NonNull<string | null | undefined>;
*/
export type NonNull<A> = Exclude<A, null>;

/**
* NonNullable
* @desc Exclude undefined and null from set `A`
* @example
* // Expect: "string"
* SymmetricDifference<string | null | undefined>;
* NonNullable<string | null | undefined>;
*/
// type NonNullable - built-in

Expand Down

0 comments on commit 8dd7b16

Please sign in to comment.