'typescript exclude optional fields from type

There is the Utility type NonNullable which will remove undefined and null values from a union type. But I was wondering if there was a way to remove optional fields from a type.

Basically if I have a type like this:

type MyType = {
  thingOne: number,
  thingTwo?: number
};

I want to be able to create a type out of the required fields only

type MyRequireds = NonOptional<MyType>;
// which is a type only containing "thingOne"

Is there some utility class that would satisfy the made up "NonOptional" utility class?



Solution 1:[1]

A tricky solution:

type RequiredKeys<T> = {
  [K in keyof T]: ({} extends { [P in K]: T[K] } ? never : K)
}[keyof T];

type NonOptional<T> = Pick<T, RequiredKeys<T>>;

type MyType = {
  thingOne: number,
  thingTwo?: number
};

type MyRequireds = NonOptional<MyType>;

Playground

The trick is that {} extends {thingTwo?: number} but doesn't extend {thingOne: number}. I originally found this solution here.

Solution 2:[2]

You can compress this into one type using the bellow.

type OmitOptional<T> = { 
  [P in keyof Required<T> as Pick<T, P> extends Required<Pick<T, P>> ? P : never]: T[P] 
}

Playground

For nested types @lonewarrior556

export type OmitOptionalNested<T> = { [P in keyof Required<T> as Pick<T, P> extends Required<Pick<T, P>> ? P : never]: 
        T[P] extends (infer U)[] ? OmitOptionalNested<U>[] :
        T[P] extends object ? OmitOptionalNested<T[P]> :
        T[P] 
}

Playgound

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Valeriy Katkov
Solution 2