mortal-cultivation-biography / type-notes

0 stars 0 forks source link

Required<Type> #3

Open nmsn opened 1 year ago

nmsn commented 1 year ago

Constructs a type consisting of all properties of Type set to required. The opposite of Partial.

interface Props {
  a?: number;
  b?: string;
}

const obj: Props = { a: 5 };

const obj2: Required<Props> = { a: 5 };
// Property 'b' is missing in type '{ a: number; }' but required in type 'Required<Props>'.
nmsn commented 1 year ago

https://github1s.com/microsoft/TypeScript/blob/HEAD/lib/lib.es5.d.ts#L1556

/**
 * Make all properties in T required
 */
type Required<T> = {
    [P in keyof T]-?: T[P];
};