Type assertions

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 initialize sng but then I cannot use it. It does know I am trying to use type ITEM for the shift method, but shift by definition can also return undefined and this definition of sng does not allow that. I get Type ‘ITEM | undefined’ is not assignable to type ITEM:

I would resort to this:

Yesterday I finally found a better solution. Use a type assertion on the shift:

Leave a Reply

Your email address will not be published. Required fields are marked *