Skip navigation
⚛️ How to use TypeScript type predicates in React to create a more robust, type-safe, and…
0:00 / 1:07

⚛️ How to use TypeScript type predicates in React to create a more robust, type-safe, and…

George Moller

@_georgemoller119K followers

Visit on X
509 likesDownloadOriginal post

29,524 views

⚛️ How to use TypeScript type predicates in React to create a more robust, type-safe, and maintainable component. (this pattern is particularly useful when dealing with discriminated unions or when you need to narrow types based on runtime checks)

5 Comments

Comments from the original post on X (Twitter)

  • George Moller's profile picture

    @_georgemoller

    I've been working professionally with React for more than 8 years and I compiled all my knowledge into 100+ infographics and 70+ video tutorials just like this one. Check them out ↓

    7 likes

  • Andre de Waard's profile picture

    @andre_dw

    We don’t need this since TS 5.5 right?

    3 likes

  • Prasenjit's profile picture

    @Star_Knight12

    Thank you for the check, it's going to be useful.

    2 likes

  • Pavel Romanov's profile picture

    @pavl_ro

    Strange, I've just tested it in TS playground and stackblitz React playground and it narrows the type correctly (with the latest TS version). Maybe it is a problem of some older TS version or less strict TS complier config?

    1 like

  • Davide Briano's profile picture

    @buondevid

    This is automatically narrowed with the latest TS version now 👌🏻