200.Land

strict-type.ts

The TypeScript code defines an interface INiceAndLoose and a utility type Strict which transforms the properties of a type to be non-nullable and required. An instance of the Strict type, derived from INiceAndLoose, is created, showcasing that the properties must now be non-nullable and defined.

interface INiceAndLoose {
  foo: string | null;
  bar?: number;
}

type Strict<T> = {
  [K in keyof Required<T>]: NonNullable<T[K]>;
};

const tight: Strict<INiceAndLoose> = { foo: "ll", bar: 2 };