{"id":450,"date":"2023-02-08T05:00:00","date_gmt":"2023-02-08T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=450"},"modified":"2023-02-07T15:34:17","modified_gmt":"2023-02-07T23:34:17","slug":"how-to-generate-type-definitions-for-typescript","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/how-to-generate-type-definitions-for-typescript\/","title":{"rendered":"How to generate type definitions for TypeScript?"},"content":{"rendered":"\n<p>This week, we have been looking at <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/union-types-in-typescript\/\" target=\"_blank\" rel=\"noopener\" title=\"\">union type<\/a>s and the downsides of using <a href=\"https:\/\/www.angulartraining.com\/daily-newsletter\/typescript-any-vs-unknown\/\" target=\"_blank\" rel=\"noopener\" title=\"\"><code>any<\/code> or <code>unknown<\/code><\/a> when we don&#8217;t have any accurate type information.<\/p>\n\n\n\n<p>Today, let&#8217;s look at how to generate type information out of any JSON data. The first tool I want to mention is <strong><a href=\"http:\/\/json2ts.com\/\" target=\"_blank\" rel=\"noopener\" title=\"\">json2ts.com<\/a><\/strong>, a website where you can copy-paste any chunk of JSON syntax and get a fully-typed output of interfaces with inferred types and everything.<\/p>\n\n\n\n<p>For instance, if I copy-paste the following JSON into json2ts:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"234\" height=\"241\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-11.png\" alt=\"\" class=\"wp-image-453\"\/><\/figure>\n<\/div>\n\n\n<p>We get the following set of interfaces ready to be used in our code:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"267\" height=\"333\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-12.png\" alt=\"\" class=\"wp-image-454\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-12.png 267w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-12-241x300.png 241w\" sizes=\"auto, (max-width: 267px) 100vw, 267px\" \/><\/figure>\n<\/div>\n\n\n<p>The more data you give to <strong>json2ts<\/strong>, the more precise it is. For instance, if you give it an array of similar objects, <strong>json2ts <\/strong>can identify if some properties are optional or support multiple different types.<\/p>\n\n\n\n<p>Also, it&#8217;s worth mentioning that interfaces are <strong>better than classes<\/strong> to describe type information because they do not get compiled into anything at all (like union types), thus bringing type safety to your code without making your production code bigger.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This week, we have been looking at union types and the downsides of using any or unknown when we don&#8217;t have any accurate type information. Today, let&#8217;s look at how to generate type information out of any JSON data. The first tool I want to mention is json2ts.com, a website where you can copy-paste any [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17,3],"tags":[],"class_list":["post-450","post","type-post","status-publish","format-standard","hentry","category-tools","category-typescript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/450","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/comments?post=450"}],"version-history":[{"count":3,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/450\/revisions"}],"predecessor-version":[{"id":460,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/450\/revisions\/460"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=450"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=450"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=450"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}