[Typescript] Make your optional fields required in TypeScript

In this post, let's see how to make all the optional fields to be required with the help of Required.

type User = {
  name: string;
  age?: number;
  gender?: string;
};

const user: Required<User> = {
  name: "John Doe",
  age: 23,
  gender: "male"
};

console.log(user);

 

posted @ 2020-02-16 22:53  Zhentiw  阅读(167)  评论(0编辑  收藏  举报