{"id":420,"date":"2023-02-06T05:00:00","date_gmt":"2023-02-06T13:00:00","guid":{"rendered":"https:\/\/www.angulartraining.com\/daily-newsletter\/?p=420"},"modified":"2023-02-01T15:22:12","modified_gmt":"2023-02-01T23:22:12","slug":"typescript-any-vs-unknown","status":"publish","type":"post","link":"https:\/\/www.angulartraining.com\/daily-newsletter\/typescript-any-vs-unknown\/","title":{"rendered":"TypeScript: any vs. unknown"},"content":{"rendered":"\n<p>TypeScript has some abstract types that can be helpful in Angular applications. Most people have encountered the type <code><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/everyday-types.html#any\" target=\"_blank\" rel=\"noopener\" title=\"\">any<\/a><\/code> at some point, and it has become a typical anti-pattern in several projects where developers decided: <em>&#8220;I don&#8217;t want to bother using proper types for this object, so I&#8217;ll use <code>any<\/code>&#8220;<\/em>.<\/p>\n\n\n\n<p>Here is why <code>any<\/code> is dangerous and not recommended:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"460\" height=\"102\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image.png\" alt=\"\" class=\"wp-image-421\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image.png 460w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-300x67.png 300w\" sizes=\"auto, (max-width: 460px) 100vw, 460px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>Now, if we replace <code>any<\/code> with <code><a href=\"https:\/\/www.typescriptlang.org\/docs\/handbook\/2\/functions.html#unknown\" target=\"_blank\" rel=\"noopener\" title=\"\">unknown<\/a><\/code>, things look different:<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"434\" height=\"96\" src=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-2.png\" alt=\"\" class=\"wp-image-425\" srcset=\"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-2.png 434w, https:\/\/www.angulartraining.com\/daily-newsletter\/wp-content\/uploads\/2023\/02\/image-2-300x66.png 300w\" sizes=\"auto, (max-width: 434px) 100vw, 434px\" \/><\/figure>\n<\/div>\n\n\n<p><\/p>\n\n\n\n<p>As you can see, <code>unknown<\/code> preserves type safety. If we receive an object from a third-party library and need to pass it around to another function, <code>unknown<\/code> is perfect for that. <\/p>\n\n\n\n<p>One way to think about <code>unknown<\/code> is: <strong>We have this object that we don&#8217;t know what&#8217;s inside, so we won&#8217;t allow touching it; we&#8217;ll store it or pass it around<\/strong>.<\/p>\n\n\n\n<p>I can&#8217;t think of good reasons why we would need to use any in Angular code at this point. Using <code>any<\/code> is refusing to use TypeScript properly and falling back to untyped JavaScript. <\/p>\n\n\n\n<p>In the next few days, we&#8217;ll cover different techniques and tools we can use to create accurate type information so we don&#8217;t need <code>any<\/code> or <code>unknown<\/code> anymore.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>TypeScript has some abstract types that can be helpful in Angular applications. Most people have encountered the type any at some point, and it has become a typical anti-pattern in several projects where developers decided: &#8220;I don&#8217;t want to bother using proper types for this object, so I&#8217;ll use any&#8220;. Here is why any is [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-420","post","type-post","status-publish","format-standard","hentry","category-typescript"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/420","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=420"}],"version-history":[{"count":9,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/420\/revisions"}],"predecessor-version":[{"id":432,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/posts\/420\/revisions\/432"}],"wp:attachment":[{"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/media?parent=420"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/categories?post=420"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.angulartraining.com\/daily-newsletter\/wp-json\/wp\/v2\/tags?post=420"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}