View accompanying code on Github
While I was building out my portfolio, I discovered a funny quirk with Typescript generics.
For the uninitiated, a generic is simply a component (function, class, variable,
etc) that spans types
. You'll commonly see them in polymorphic functions. The
contrived example we'll be using is pulled directly from the TS docs:
I've always read this from left-to-right, which lead me to believe that Type
needs to be declared if arg
is going to be casted correctly.
In my mind, there were 2 exceptions, which it turns out, are only partially true. The exceptions were:
extends
keyword
=
operator:
So there I was click-clacking along, when I absent-mindedly forgot to include an
argument to the generic and yet, the correct type was returned. This wild
phenomenon is known as type argument interference
. Let me explain:
What's happening is TS is correctly inferring the type of the functional
argument, which it assigns to Type
. So in reality, the following return the
same types
Even cooler is that it respects the type differences between var
, let
and
const
. As you know, the const
declarator means a variable immutable and
therefore, its type is typically its value:
This same inference is applied to generics:
If you want to see this in action, you can checkout the source code to the site you're on right now:
Comments