{"id":2650,"date":"2023-04-28T14:18:03","date_gmt":"2023-04-28T12:18:03","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=2650"},"modified":"2023-04-28T14:18:03","modified_gmt":"2023-04-28T12:18:03","slug":"how-to-set-up-a-typescript-interface-default-value","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2023\/04\/28\/how-to-set-up-a-typescript-interface-default-value\/","title":{"rendered":"How To Set Up A TypeScript Interface Default Value?"},"content":{"rendered":"\n<p>In TypeScript, interfaces are used to define the structure of objects, but they do not provide default values for properties.<\/p>\n\n\n\n<p>However, you can define optional properties using the <strong><code>?<\/code> <\/strong>operator, and you can also set default values for object properties when you define the object.<\/p>\n\n\n\n<p>Here&#8217;s an example of how we can define an interface with optional properties and set default values for the properties:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface Person {\r\n  name: string;\r\n  age?: number;\r\n  gender?: string;\r\n}\r\n\r<\/code><\/pre>\n\n\n\n<p>In this example, the <code>Person<\/code> interface defines three properties: <code><span class=\"has-inline-color has-vivid-cyan-blue-color\">name<\/span><\/code>, <code><span class=\"has-inline-color has-vivid-cyan-blue-color\">age<\/span><\/code>, and <code><span class=\"has-inline-color has-vivid-cyan-blue-color\">gender<\/span><\/code>. The <code>age<\/code> and <code>gender<\/code> properties are optional, as indicated by the <strong><code>?<\/code> operator<\/strong>.<\/p>\n\n\n\n<p>If we want to set default values for required properties, we can use a type assertion to cast the value to the expected type:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>export interface Person {\r\n  name: string;\r\n  age: number;\r\n  gender: string;\r\n}\r\n\r\nexport const defaultsPerson = {\r\n  name: 'John',\r\n  age: 30,\r\n  gender: 'male',\r\n} as Person;\r\n<\/code><\/pre>\n\n\n\n<p>In this example, the &#8220;<code>defaultsPerson<\/code>&#8221; object has the same properties as the <code>Person<\/code> interface, but we&#8217;re using a type assertion to tell TypeScript that the object should be treated as a <code>Person<\/code> object.<\/p>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h4>Alternative: <\/h4>\n\n\n\n<p>To set up a default value with an\u00a0interface, we can also use the Typescript <strong>Pick <\/strong>Utility type\u00a0like so:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>interface IPerson {\r\n    name: string;\r\n    role: string;\r\n}\r\n\r\nconst defaults: Pick&lt;IPerson, 'role'> = {\r\n    role: 'user'\r\n}\r\n\r\nconst person: IPerson = {\r\n    ...defaults,\r\n    name: 'Tim'\r\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In TypeScript, interfaces are used to define the structure of objects, but they do not provide default values for properties. However, you can define optional properties using the ? operator, and you can also set default values for object properties when you define the object. Here&#8217;s an example of how we can define an interface [&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":[680,372,463,681,615],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2650"}],"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=2650"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2650\/revisions"}],"predecessor-version":[{"id":2651,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/2650\/revisions\/2651"}],"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=2650"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=2650"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=2650"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}