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 NonNull #133

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
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