By utilizing TypeScript 2.1, we have the ability to generate a partial type from a strict type as demonstrated below:
type Partial<T> = {
[P in keyof T]?: T[P];
};
type Person = { name: string, age: number }
type PersonPartial = Partial<Person>; // === { name?: string, age?: number }
However, the question arises: is it feasible to derive a strict type from a partial type?
type Strict<T> = { ??? };
type Person = { name: string; age?: number; }
type PersonStrict = Strict<Person>; // === { name: string, age: number }
Desired Outcome
I am in need of the following two types, without the need for redundant declarations.
type Person = { name: string, age?: number, /* and other props */ }
type PersonStrict = { name: string, age: number, /* and other props */ }
While a verbose solution exists, I am curious if there is a more efficient approach available.
type RequiredProps = { name: string, /* and other required props */ };
type OptionalProps = { age: number, /* and other optional props */ };
type Person = RequiredProps & Partial<OptionalProps>;
type PersonStrict = RequiredProps & OptionalProps;