{"id":446,"date":"2025-08-10T22:12:40","date_gmt":"2025-08-10T22:12:40","guid":{"rendered":"https:\/\/www.bullcrane.com\/angular\/?p=446"},"modified":"2025-10-13T21:43:44","modified_gmt":"2025-10-13T21:43:44","slug":"type-assertions","status":"publish","type":"post","link":"http:\/\/www.bullcrane.com\/angular\/2025\/08\/10\/type-assertions\/","title":{"rendered":"Type assertions"},"content":{"rendered":"\n<p>We are supposed to put type definitions on our variables in TypeScript.  But I often had to resort to the frowned-upon type <em>any<\/em>.  Here I declare a type for <strong>sng <\/strong>but then I cannot use <strong>sng<\/strong> for a bog standard <strong>shift<\/strong>.  The language&#8217;s <strong>shift<\/strong> method can return undefined.  My definition of ITEM (elsewhere) does not allow that because (duh, of course) it is a type definition.  I get <em>Type &#8216;ITEM | undefined&#8217; is not assignable to type ITEM<\/em>:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"306\" height=\"83\" src=\"https:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-8.png\" alt=\"\" class=\"wp-image-448\" srcset=\"http:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-8.png 306w, http:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-8-300x81.png 300w\" sizes=\"(max-width: 306px) 100vw, 306px\" \/><\/figure>\n\n\n\n<p>I would resort to this:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"372\" height=\"88\" src=\"https:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-11.png\" alt=\"\" class=\"wp-image-451\" srcset=\"http:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-11.png 372w, http:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-11-300x71.png 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/figure>\n\n\n\n<p>Yesterday I finally found a better solution.  Use a type assertion on the shift:<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" loading=\"lazy\" width=\"372\" height=\"88\" src=\"https:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-9.png\" alt=\"\" class=\"wp-image-449\" srcset=\"http:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-9.png 372w, http:\/\/www.bullcrane.com\/angular\/wp-content\/uploads\/2025\/08\/image-9-300x71.png 300w\" sizes=\"(max-width: 372px) 100vw, 372px\" \/><\/figure>\n\n\n\n<p>If a Type Assertion is incorrect it can lead to runtime errors that Typescript would normally prevent.  But in this case I am confident that this line of code will never return an undefined value.<\/p>\n\n\n\n<p><strong>sng<\/strong> might be null.  It is immediately after it is declared in the first line.  That does not matter to the type assertion because 1. the type assertion is only for its own line of code, and 2. I am promising the compiler that this <strong>shift<\/strong> will never return something that is <em>undefined<\/em>.  I am not promising it will never return a <em>null<\/em>.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are supposed to put type definitions on our variables in TypeScript. But I often had to resort to the frowned-upon type any. Here I declare a type for sng but then I cannot use sng for a bog standard shift. The language&#8217;s shift method can return undefined. My definition of ITEM (elsewhere) does not &hellip; <\/p>\n<p class=\"link-more\"><a href=\"http:\/\/www.bullcrane.com\/angular\/2025\/08\/10\/type-assertions\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Type assertions&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts\/446"}],"collection":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/comments?post=446"}],"version-history":[{"count":8,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":465,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/posts\/446\/revisions\/465"}],"wp:attachment":[{"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/media?parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/categories?post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.bullcrane.com\/angular\/wp-json\/wp\/v2\/tags?post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}