Skip to main content
seanmcp.com

Make all properties required in TypeScript


I have a type interface for shapes that looks something like this:

interface Shape {
  height?: number;
  width?: number;
}

Now I want to extend my interface for rectangles, but I want both height and width to be required. You can do that in TypeScript with a Required utility type.

Required takes a type and returns one with all required properties. You can use it by declaring a new type and passing the Shape interface:

type Rectangle = Required<Shape>;

Now when I try to create a new rectangle, I can use the Rectangle type to ensure that I provided all the required properties:

const rect: Rectangle = {
  height: 2,
  width: 4,
};

You can play around with these types in this TypeScript playground, and read more about Utility types on the TypeScript website.

Happy requiring!