{"id":1991,"date":"2022-06-22T17:02:00","date_gmt":"2022-06-22T15:02:00","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=1991"},"modified":"2022-06-22T17:02:00","modified_gmt":"2022-06-22T15:02:00","slug":"make-all-properties-optional-in-typescript","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2022\/06\/22\/make-all-properties-optional-in-typescript\/","title":{"rendered":"Make all properties optional in TypeScript"},"content":{"rendered":"\n<p>Let\u00b4s <strong>use the<span class=\"has-inline-color has-vivid-cyan-blue-color\">\u00a0<code>Partial<\/code>\u00a0<\/span>utility type to make all of the properties in a type optional,<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Employee {\r\n  id: number;\r\n  name: string;\r\n  salary: number;\r\n}\r\n\r\nconst emp: <span class=\"has-inline-color has-vivid-cyan-blue-color\">Partial<\/span>&lt;Employee> = {};\r\n\r\nemp.name = 'James';\r\n<\/code><\/pre>\n\n\n\n<p>We used the\u00a0<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/utility-types.html#partialtype\">Partial<\/a>\u00a0utility type to construct a new type with all of the properties of the provided type set to optional.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Employee {\r\n  id: number;\r\n  name: string;\r\n  salary: number;\r\n}\r\n\r\n\/\/ ?? type T = {\r\n\/\/     id?: number | undefined;\r\n\/\/     name?: string | undefined;\r\n\/\/     salary?: number | undefined;\r\n\/\/ }\r\ntype T = Partial&lt;Employee>;\r\n<\/code><\/pre>\n\n\n\n<p>The same approach can be used to get a type that consists of an object&#8217;s properties where all of them are marked as optional.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const obj = {\r\n  id: 1,\r\n  name: 'James',\r\n  salary: 100,\r\n};\r\n\r\n\/\/ ?? type T = {\r\n\/\/     id?: number | undefined;\r\n\/\/     name?: string | undefined;\r\n\/\/     salary?: number | undefined;\r\n\/\/ }\r\ntype T = Partial&lt;typeof obj>;\r\n<\/code><\/pre>\n\n\n\n<p>Notice that we had to use the\u00a0<a rel=\"noreferrer noopener\" target=\"_blank\" href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/typeof-types.html#handbook-content\"><strong>typeof<\/strong> type operator<\/a>, because\u00a0<code>Partial<\/code>\u00a0expects a type.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Let\u00b4s use the\u00a0Partial\u00a0utility type to make all of the properties in a type optional, We used the\u00a0Partial\u00a0utility type to construct a new type with all of the properties of the provided type set to optional. The same approach can be used to get a type that consists of an object&#8217;s properties where all of them [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":1993,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[83],"tags":[566,161,217],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1991"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=1991"}],"version-history":[{"count":2,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1991\/revisions"}],"predecessor-version":[{"id":1997,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/1991\/revisions\/1997"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/1993"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=1991"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=1991"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=1991"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}