remove null or undefined from properties of a type

TypescriptTypesNullUndefined

Typescript Problem Overview


I need to declare a type such that removes the undefined from its property types.

Suppose we have:

type Type1{
  prop?: number;
}

type Type2{
  prop: number | undefined;
}

type Type3{
  prop: number;
}

I need to define a generic type called NoUndefinedField<T> such that NoUndefinedField<Type1> gives the same type as Type3 and the same type as NoUndefinedField<Type2>.

I tried this

type NoUndefinedField<T> = { [P in keyof T]: Exclude<T[P], null | undefined> };

But it only works for Type2.

Typescript Solutions


Solution 1 - Typescript

There is also now the NonNullable type built in:

type NonNullable<T> = Exclude<T, null | undefined>;  // Remove null and undefined from T

https://www.typescriptlang.org/docs/handbook/utility-types.html#nonnullablet

Solution 2 - Typescript

Thanks to @artem, the solution is:

type NoUndefinedField<T> = { [P in keyof T]-?: NoUndefinedField<NonNullable<T[P]>> };

Notice the -? syntax in [P in keyof T]-? which removes optionality

Solution 3 - Typescript

@DShook's answer is incorrect (or rather incomplete) because the OP is asking to remove null and undefined from the types properties, not from the type itself (a distinct difference).

While @Fartab's answer is correct, I'll add to it, as there is now the built-in Required type, and the solution can be re-written as:

type RequiredProperty<T> = { [P in keyof T]: Required<NonNullable<T[P]>>; };

This will map the types properties (not the type itself), and ensure that each one is neither; null or undefined.

An example of the difference between removing null and undefined from the type, versus removing them from a types properties (using the above RequiredProperty type):

type Props = {
  prop?: number | null;
};

type RequiredType = NonNullable<Props>; // { prop?: number | null }
type RequiredProps = RequiredProperty<Props>; // { prop: Required<number> } = { prop: number }

Solution 4 - Typescript

Something in both @Fartab's and @tim.stasse's answers is messing up a property of type Date for me:

// both:
type NoUndefinedField<T> = {
  [P in keyof T]-?: NoUndefinedField<NonNullable<T[P]>>;
};
type NoUndefinedField<T> = {
  [P in keyof T]-?: NoUndefinedField<Exclude<T[P], null | undefined>>;
};
// throw:
Property '[Symbol.toPrimitive]' is missing in type 'NoUndefinedField<Date>' but required in type 'Date'.ts(2345)
// and
type NoUndefinedField<T> = { [P in keyof T]: Required<NonNullable<T[P]>> };
// throws:
Property '[Symbol.toPrimitive]' is missing in type 'Required<Date>' but required in type 'Date'.ts(2345)

I'm having success with this solution without recursion:

type NoUndefinedField<T> = {
  [P in keyof T]-?: Exclude<T[P], null | undefined>;
};

Solution 5 - Typescript

Some of the answers were not working for me, I ended up with a similar solution based on the top answers:

type RequiredNonNullableObject<T extends object> = { [P in keyof Required<T>]: NonNullable<T[P]>; };

This results in the following:

type ObjectType = {

  startDateExpr?: string | null;
  endDateExpr?: string | null;

  startDate?: Date | null;
  endDate?: Date | null;

}

type Result = RequiredNonNullableObject<ObjectType>; 

With the Result type being equal to:

type Result = {
  startDateExpr: string;
  endDateExpr: string;
  startDate: Date;
  endDate: Date;
}

TypeScript Playground Example

Solution 6 - Typescript

Nowadays you can use Required to do exactly what you need:

Required<Type1>

That will result in all the fields becoming non-optional. More details can be found here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFartabView Question on Stackoverflow
Solution 1 - TypescriptDShookView Answer on Stackoverflow
Solution 2 - TypescriptFartabView Answer on Stackoverflow
Solution 3 - Typescripttim.stasseView Answer on Stackoverflow
Solution 4 - TypescriptStepanView Answer on Stackoverflow
Solution 5 - TypescriptrmolinamirView Answer on Stackoverflow
Solution 6 - TypescriptParsa NasirimehrView Answer on Stackoverflow