francisrstokes / construct-js

🛠️A library for creating byte level data structures.
MIT License
1.37k stars 29 forks source link

Use TS assertion signature to avoid type casting #30

Closed kubk closed 2 years ago

kubk commented 2 years ago

Starting from TS 3.7 it's possible to use so-called "assertion signatures" to narrow TS types after guard statements. It's described here: https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-7.html#assertion-functions

It allows to avoid type casting. For some reason assertion signatures are available only for function keyword, that's why I had replace const with function

francisrstokes commented 2 years ago

Definitely an improvement, thanks @kubk! :tada: