How and why to use infer in typescript

Hi everyone, how are you today? Hope you all are doing well!

Today I’m going to explain how hell works and why it’s important in conditional structures in typescript. Initially the concept is relatively simple, but the implementation makes us question…


This content originally appeared on DEV Community and was authored by Miguel

Hi everyone, how are you today? Hope you all are doing well!

Today I'm going to explain how hell works and why it's important in conditional structures in typescript. Initially the concept is relatively simple, but the implementation makes us question its real reason for use.

First, it is important to note that the use of hell is directly related to ternary conditions in the typescript. But why?

Infer Theory

If we can infer T, G will be the result, if not, F will be the result

The purpose of inference is to test whether an inference is possible, see the example:

Example 01

It turns out that when we use our custom type we will have never as an answer when trying to infer an object with no return.

Example 02

Take one more example, if S cannot infer in the sentence pattern it will return never.

There is great power when using inference in conjunction with generics and conditional structures in typescript. They are very useful when in recursive conditional structures in typescript.

I tried to show you real use cases from hell and maybe you don't get the theory at first, but if you have any questions just send me a message and I'll be happy to answer.

Naruto

Time is very important, thanks for sharing a little bit of yours with me 😊.

image

image


This content originally appeared on DEV Community and was authored by Miguel


Print Share Comment Cite Upload Translate Updates
APA

Miguel | Sciencx (2021-10-20T09:28:54+00:00) How and why to use infer in typescript. Retrieved from https://www.scien.cx/2021/10/20/how-and-why-to-use-infer-in-typescript/

MLA
" » How and why to use infer in typescript." Miguel | Sciencx - Wednesday October 20, 2021, https://www.scien.cx/2021/10/20/how-and-why-to-use-infer-in-typescript/
HARVARD
Miguel | Sciencx Wednesday October 20, 2021 » How and why to use infer in typescript., viewed ,<https://www.scien.cx/2021/10/20/how-and-why-to-use-infer-in-typescript/>
VANCOUVER
Miguel | Sciencx - » How and why to use infer in typescript. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/10/20/how-and-why-to-use-infer-in-typescript/
CHICAGO
" » How and why to use infer in typescript." Miguel | Sciencx - Accessed . https://www.scien.cx/2021/10/20/how-and-why-to-use-infer-in-typescript/
IEEE
" » How and why to use infer in typescript." Miguel | Sciencx [Online]. Available: https://www.scien.cx/2021/10/20/how-and-why-to-use-infer-in-typescript/. [Accessed: ]
rf:citation
» How and why to use infer in typescript | Miguel | Sciencx | https://www.scien.cx/2021/10/20/how-and-why-to-use-infer-in-typescript/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.