Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Type guards are a useful construct, especially when dealing with data from an API that could come in a variety of shapes.

However, if you have control over the data shape, in general, it's nicer to take advantage of discriminated unions as you'll get more automatic typesafe compiler support. If you're not familiar with the construct in TypeScript, the docs give a nice example of them[0].

[0]: https://www.typescriptlang.org/docs/handbook/advanced-types....



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: