Understanding the infer keyword
Saturday 6 January 2024
ELI5 - Explaining infer
take this type
----
we first check if the type passed T
is a type of Promise<infer R>
(assume that R
is any value)
if yes then we can say return the value in R
(infer will essentially capture the value)
if not then we make the next check (for the .then()
) and we do the same thing
if the two fail then we return never
----
btw a fun and actually very useful thing about infer is that you can add extends to make sure that you only catch specific types
i.e if you do
it'll only capture First
if First
happens to be a string this means the extends will fail and you'll get never