<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nicholas Kircher</title>
    <description>The latest articles on DEV Community by Nicholas Kircher (@futuresight).</description>
    <link>https://dev.to/futuresight</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F89813%2F15ed4ec5-4b47-4089-9951-7bc508a7f384.jpeg</url>
      <title>DEV Community: Nicholas Kircher</title>
      <link>https://dev.to/futuresight</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/futuresight"/>
    <language>en</language>
    <item>
      <title>How to code without loops - ever!</title>
      <dc:creator>Nicholas Kircher</dc:creator>
      <pubDate>Sun, 19 Feb 2023 00:55:34 +0000</pubDate>
      <link>https://dev.to/futuresight/how-to-code-without-loops-ever-3ph7</link>
      <guid>https://dev.to/futuresight/how-to-code-without-loops-ever-3ph7</guid>
      <description>&lt;p&gt;OH YEAH!  It's a good day for some knowledge!  It's time to talk about loops --- it's time to talk about loops --- it's time to talk about loops --- and how you can throw them out the window and replace them with something better!  Prepare for the Defenestration of JavaScript ("Defenestration" means "to throw out the window").&lt;/p&gt;

&lt;p&gt;I very often see - in my travels - code that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;third&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fourth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fifth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sixth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foundItem&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;s&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;foundItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So if you're familiar with JavaScript/Typescript, which you are, this code should be fairly straight forward to understand.  We're looking for an item in a list of strings where the first letter is 's', and if we find it, we assign it to &lt;code&gt;foundItem&lt;/code&gt;.  This is a "find" operation.  We're looking for a specific element which matches our predicate condition.  We only need one item, and once we've found it, we don't need to keep looking through the rest of the list.  Therefore, our &lt;code&gt;foundItem&lt;/code&gt; should be the string "second". &lt;br&gt;
Right?&lt;/p&gt;

&lt;p&gt;If you're eagle-eyed, you might notice that we are not stopping at the first matching element.  We keep going.  "Ok so lets add a break statement".  Oh now we're really getting right into command and control here.  What if I told you that all the code in that above example can be written in 2 lines, without simply compressing it, and without any mutation or variable reassignment, &lt;em&gt;and&lt;/em&gt; stops searching after the first match?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;first&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;second&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;third&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fourth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;fifth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;sixth&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;foundItem&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;find&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;startsWith&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;s&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Welcome to the wonderful world of iteration methods!  There is not a single thing that I can do with loops - EXCEPT IN INFINITE GENERATOR FUNCTIONS, DON'T @ ME - that I can't with simple iteration methods that are built-in.&lt;/p&gt;

&lt;p&gt;Ever seen this kind of thing?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;newStruct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{};&lt;/span&gt;

&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;newStruct&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;generateSomeRelevantDataFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mutations are supremely dangerous.  Even if you think you know exactly what is going to happen here, that can very easily change.  All someone has to do is pass in an object from somewhere else - like a function argument - as the initial value for &lt;code&gt;newStruct&lt;/code&gt; and now you are mutating a reference shared by who knows how many other things, and it changes for &lt;em&gt;all of them&lt;/em&gt;.  Also, doing things immutably is hip and fashionable, so try this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;newStruct&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;items&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;reduce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;({&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nf"&gt;generateSomeDataFor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}),&lt;/span&gt;
  &lt;span class="p"&gt;{}&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will build up your new object (each version of which is provided as "result") as it iterates over the &lt;code&gt;items&lt;/code&gt; list, and we're doing it immutably.  Spread operators don't directly copy that section of memory into a duplicate block of memory, either.  This is all done by reference, so it's also a very efficient operation, and not memory-wasteful, nor does it require all that much more compute time.  In return, we get the safety and assurance of an immutable operation.  Pretty good deal!&lt;/p&gt;

&lt;p&gt;There are SO MANY MORE powerful iteration methods, all built in, like: &lt;code&gt;filter&lt;/code&gt;, &lt;code&gt;map&lt;/code&gt;, &lt;code&gt;flatMap&lt;/code&gt;, &lt;code&gt;findLast&lt;/code&gt;, and &lt;code&gt;reduceRight&lt;/code&gt;.  It gets even better when you throw in a functional library like Ramda or FP-TS, and add all the category theory stuff to it (maybe that will be the next post?  Who wants to learn some category theory?  :D )&lt;/p&gt;

&lt;p&gt;So I hope this inspires you to never use loop statements again.  Why would you need to?&lt;/p&gt;

</description>
      <category>vue</category>
      <category>webcomponents</category>
      <category>learning</category>
    </item>
    <item>
      <title>How 2 TypeScript: Get the last item type from a tuple of types</title>
      <dc:creator>Nicholas Kircher</dc:creator>
      <pubDate>Sun, 02 Sep 2018 02:58:18 +0000</pubDate>
      <link>https://dev.to/futuresight/how-2-typescript-get-the-last-item-type-from-a-tuple-of-types-3fh3</link>
      <guid>https://dev.to/futuresight/how-2-typescript-get-the-last-item-type-from-a-tuple-of-types-3fh3</guid>
      <description>&lt;p&gt;(&lt;em&gt;Note: The author assumes you're using TypeScript 3.x&lt;/em&gt;)&lt;/p&gt;

&lt;p&gt;Hey you!  Do you want to get the type of the last item from a tuple of types?&lt;/p&gt;

&lt;p&gt;Of course you do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Stuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;OtherStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here I have 2 tuple types, one with 3, and one with 4 items.  The last type in &lt;code&gt;Stuff&lt;/code&gt; is &lt;code&gt;string&lt;/code&gt;, and the last type in &lt;code&gt;OtherStuff&lt;/code&gt; is &lt;code&gt;boolean&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;How do we get those last item types?&lt;/p&gt;

&lt;p&gt;Well, amazingly, one way we can do it - if we know the length of the tuple at time of writing - is to use a numeric literal as an index for looking up the type at a position.  I know, amazing.  Like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LastStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Stuff&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; string&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LastOtherStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;OtherStuff&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; boolean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Kinda like a normal array lookup!&lt;/p&gt;

&lt;p&gt;But what if you &lt;em&gt;don't&lt;/em&gt; know the length of the tuple?  Hmm... how do we get TypeScript to tell us the length and then let us use that length to pick out the last item, all at compile time?&lt;/p&gt;

&lt;p&gt;Borrowing from &lt;a href="https://github.com/pelotom/hkts/blob/master/src/index.ts#L38"&gt;this amazing HKTs library&lt;/a&gt; I was able to get the length of the tuple as a numeric literal:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;GetLength&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;infer&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Stuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;OtherStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LengthStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;GetLength&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Stuff&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 3&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the &lt;code&gt;infer&lt;/code&gt; keyword in this part: &lt;code&gt;original extends { length: infer L }&lt;/code&gt; - I talk more about what the &lt;code&gt;infer&lt;/code&gt; keyword is in &lt;a href="https://dev.to/miracleblue/how-2-typescript-serious-business-with-typescripts-infer-keyword-40i5"&gt;the previous How 2 TypeScript post&lt;/a&gt;, so if you're confused, I hope that sheds a bit of light :)&lt;/p&gt;

&lt;p&gt;But remember, lists are zero-indexed, so if we want the last item, we will need a way to do &lt;code&gt;L - 1&lt;/code&gt;.  We can't just do straight arithmetic at the type level in TS (yet), so this does not work: &lt;code&gt;type LastItem = Stuff[GetLength&amp;lt;Stuff&amp;gt; - 1]&lt;/code&gt; (thats a syntax error for TS).  So we're gonna need a map of some kind.&lt;/p&gt;

&lt;p&gt;The approach I felt would be best was a type mapping from a numeric literal to the previous numeric literal.  I found just such a type from &lt;a href="https://github.com/andnp/SimplyTyped/blob/master/src/types/numbers.ts"&gt;this SimplyTyped library&lt;/a&gt;, which is essentially giving us &lt;code&gt;n - 1&lt;/code&gt; for anything between 0 and 63.  Using this, I can input the length we inferred as &lt;code&gt;L&lt;/code&gt; and get back the last index of that list, and use that to look up the last type.  Like so:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Borrowed from SimplyTyped:&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Prev&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;11&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;15&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;16&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;17&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;21&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;23&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;24&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;25&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;26&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;27&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;28&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;29&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;30&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;31&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;33&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;34&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;36&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;37&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;38&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;39&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;40&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;41&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;42&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;43&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;44&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;45&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;46&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;47&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;48&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;51&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;52&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;53&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;54&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;55&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;56&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;57&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;59&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;61&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;62&lt;/span&gt;&lt;span class="p"&gt;][&lt;/span&gt;&lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Actual, legit sorcery&lt;/span&gt;
&lt;span class="c1"&gt;// Borrowed from pelotom/hkts:&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;GetLength&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;infer&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;L&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;nevertype&lt;/span&gt; &lt;span class="nx"&gt;GetLast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;Prev&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;GetLength&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// Here are our test-subject tuples:&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Stuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;OtherStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;object&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="c1"&gt;// How long is the `Stuff` tuple?&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LengthStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;GetLength&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Stuff&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; 3&lt;/span&gt;

&lt;span class="c1"&gt;// What is the last element of each tuple?&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LastStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;GetLast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Stuff&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; string&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;LastOtherStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;GetLast&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;OtherStuff&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="c1"&gt;// =&amp;gt; boolean&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Boom!  It's not perfect, but this is about as good as I could make it with the latest TypeScript version.&lt;/p&gt;

&lt;h2&gt;
  
  
  After thoughts:
&lt;/h2&gt;

&lt;p&gt;I actually tried many, many different things to make this work.  The first version that I got working used the "TupleTable" from the &lt;a href="https://github.com/pelotom/hkts/blob/master/src/index.ts#L58"&gt;HKTs library&lt;/a&gt; to return to me the inferred type of the last item, using a lookup based on the length of the tuple.  That felt a bit overkill for this task, and was limited to only 10 items.  If I wanted to increase the limit, that tuple table would have to get much, much bigger.&lt;/p&gt;

&lt;p&gt;Instead, I looked around for a way to do a bit of type-level arithmetic.  If we say that any given tuple's length is &lt;code&gt;n&lt;/code&gt;, then the last item of any given tuple is at &lt;code&gt;n - 1&lt;/code&gt;.  We know this from the countless times we've had to lookup the last item in an array in JS.  In TypeScript, we can't (yet) do any arithmetic operations on numeric literal types natively, so if we go down this road, we will need a kind of mapping between numeric literals (so that given &lt;code&gt;5&lt;/code&gt;, we get back &lt;code&gt;4&lt;/code&gt;).  It will also be of a finite length, but hopefully the complexity of the code won't increase significantly if we increase the max length by 1 (for example).&lt;/p&gt;

&lt;p&gt;After searching GitHub, I found exactly what I needed.  The &lt;code&gt;Prev&lt;/code&gt; type &lt;a href="https://github.com/andnp/SimplyTyped/blob/master/src/types/numbers.ts"&gt;from SimplyTyped&lt;/a&gt; will let us pass in a number (anywhere from 0 to 63) and it will give us the number before it.  So, given &lt;code&gt;5&lt;/code&gt;, you get back &lt;code&gt;4&lt;/code&gt;.  Until we get a built-in "successor/predecessor" type in TypeScript, I think this is about as good as it gets.  &lt;/p&gt;

&lt;p&gt;I'm actually quite surprised at the power of TypeScript's type system, compared to what I'm used to in Haskell and PureScript.  There are still many limitations in comparison, and perhaps there always will be, but one thing is certain: TypeScript can hold its own in this cut-throat world of typed languages.&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>webdev</category>
      <category>javascript</category>
      <category>computerscience</category>
    </item>
    <item>
      <title>How 2 TypeScript: Serious business with TypeScript's infer keyword.</title>
      <dc:creator>Nicholas Kircher</dc:creator>
      <pubDate>Fri, 31 Aug 2018 11:53:04 +0000</pubDate>
      <link>https://dev.to/futuresight/how-2-typescript-serious-business-with-typescripts-infer-keyword-40i5</link>
      <guid>https://dev.to/futuresight/how-2-typescript-serious-business-with-typescripts-infer-keyword-40i5</guid>
      <description>&lt;p&gt;Hey you!  Do you want to get the return type of a function and use it, without knowing what it really is, or asking the user of your thing to supply it outright?&lt;/p&gt;

&lt;p&gt;Of course you do.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;GetReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; 
  &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;infer&lt;/span&gt; &lt;span class="nx"&gt;returnType&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;returnType&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Conditional types in typescript allow you to introduce type variables into the expression in a rather dynamic way.  Notice the &lt;code&gt;infer&lt;/code&gt; keyword.  That says to TypeScript: "I want to take whatever TypeScript infers to be at this position and assign it to the name &lt;code&gt;returnType&lt;/code&gt;".  It just so happens that the thing at that position is the return type of a given function, that we have called &lt;code&gt;original&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;What happens when you put it into action?  I AM SO GLAD YOU ASKED!  Take this extremely powerful, useful, and all-round fantasmical Higher Order Function&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;someRandomStuff&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;originalFn&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="na"&gt;result&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;GetReturnType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;fn&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;originalFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;12345&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;result&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The type of &lt;code&gt;result&lt;/code&gt; is going to be whatever the return type of &lt;code&gt;originalFn(12345)&lt;/code&gt; is.  We don't actually know, until...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;innerFn&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;item&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;FYI, TS will infer the output of &lt;code&gt;innerFn&lt;/code&gt; to be a string.  Then...&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;output&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;someRandomStuff&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;innerFn&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ALL SYSTEMS GO&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Would you like to play a game?  What is the type of &lt;code&gt;output&lt;/code&gt;?  The answer is: What is the type of &lt;code&gt;innerFn&lt;/code&gt;?&lt;/p&gt;

&lt;p&gt;It's a string!&lt;/p&gt;

&lt;p&gt;If it weren't for this trick, the type of &lt;code&gt;output&lt;/code&gt; would be &lt;code&gt;any&lt;/code&gt;.  I know, I was surprised too.  But it's true.&lt;/p&gt;

&lt;p&gt;You can do the same sort of thing with anything that TypeScript can infer.  Wanna get the type of arguments?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;NYET&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;never&lt;/span&gt; &lt;span class="c1"&gt;// preparation for unfunny joke&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;GetArgumentType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nb"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt;
  &lt;span class="nx"&gt;original&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;infer&lt;/span&gt; &lt;span class="nx"&gt;argumentsType&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;argumentsType&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;NYET&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's all, see you next time.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>webdev</category>
      <category>functional</category>
      <category>how2ts</category>
    </item>
    <item>
      <title>Overcoming the mental blockade</title>
      <dc:creator>Nicholas Kircher</dc:creator>
      <pubDate>Tue, 07 Aug 2018 05:18:11 +0000</pubDate>
      <link>https://dev.to/futuresight/overcoming-the-mental-blockade-1ahc</link>
      <guid>https://dev.to/futuresight/overcoming-the-mental-blockade-1ahc</guid>
      <description>&lt;p&gt;We all know the feeling.  You've got a new task to do, and you can't think of how to implement it.  Or; you've got an idea for something, and you can't decide where to start.  No matter what it is, you have a problem to solve, but no ideas yet on how to solve it.  No, you're not alone, and no you are NOT a bad developer.&lt;/p&gt;

&lt;p&gt;I had this problem for a very long time, long through my development career, and it wasn't until recently that I found a trick to overcoming it.  Please note, your mileage may vary.  What works for me may not work for you, and it's no silver bullet, but I would recommend giving this a good try :)&lt;/p&gt;

&lt;p&gt;First, lets use our imagination.  Think about a situation where you are asked to build some sort of API layer on top of something.  When I say an API, I just mean a series of functions or methods that perform certain related tasks.  Let’s say, for example, that we needed to build a helper library in JS that generates media query strings.  I built just such a tool for my &lt;a href="https://github.com/bupa-digital/shades"&gt;Shades library&lt;/a&gt; (shameless plug, sorry).  &lt;/p&gt;

&lt;p&gt;The way I approached that task was to work backwards.  Hang on, what?  Let me show you.  We know the end result, since it’s described for us already: we want to generate media query strings.  This wrapper library needs to be able to generate &lt;em&gt;any&lt;/em&gt; valid media query.  So, first step, take a look at a few medium-sized media query examples, ones that combine a broad set of media query features, and at the same time lets work out what all the different individual types of value that can go into a media query.&lt;/p&gt;

&lt;p&gt;Some examples taken from the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/CSS/Media_Queries/Using_media_queries"&gt;MDN docs&lt;/a&gt; are as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;@media print&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@media screen, print&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@media (max-width: 1245px)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@media screen and (min-width: 30em) and (orientation: landscape)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;@media screen, print and (min-width: 30em) and (max-width: 100em) and (orientation: landscape) and (color)&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After looking at these and looking through the rest of the MDN docs on the subject, we find that media queries support the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Media “Types”, of which there are only 4 possible values: &lt;code&gt;all&lt;/code&gt;, &lt;code&gt;print&lt;/code&gt;, &lt;code&gt;screen&lt;/code&gt;, and &lt;code&gt;speech&lt;/code&gt;, and one can have anywhere from 0 to all of these in a single query.&lt;/li&gt;
&lt;li&gt;Media “Features”, most of which can accept a value of their own (such as &lt;code&gt;width&lt;/code&gt; and &lt;code&gt;height&lt;/code&gt; which accept pixel numbers), some which allow only one of a set of possible values (such as &lt;code&gt;orientation&lt;/code&gt;, which can only be either &lt;code&gt;portrait&lt;/code&gt; or &lt;code&gt;landscape&lt;/code&gt;), and others which are implicitly set to true if you include then without specifying a value (such as &lt;code&gt;color&lt;/code&gt;, or &lt;code&gt;monochrome&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Logical Operators (such as &lt;code&gt;not&lt;/code&gt;, &lt;code&gt;and&lt;/code&gt;, etc)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now we understand all the different possible values that can go in a query (or at least we know the different kinds!) and we have varying examples of what media queries can look like.  They can be as small as just one value, of either a media “type”, or media “feature”.  Alternatively, they can be as large as you want to go, including literally every possible “feature” and every valid “type”, including various logical operators and combinators.  So, that’s quite daunting, but we don’t need to start by implementing each of these one by one!  Not at all!  &lt;/p&gt;

&lt;p&gt;Let’s just start by imagining we had the perfect library already built, that we could use right now.  If we needed to write a media query using this library, what would that look like?  Imagine it as being the best JS library in the world for this task, without worrying about the implementation or complexity or anything yet.  Pretend it’s all done for you.  Let’s have a go at writing a media query with our amazing, magical pretend library.  What I ended up writing at the very beginning as my “ideal api” looked something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;media.screen.portrait.width(950).hover&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Which would translate into something like this:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;@media screen and (orientation:portrait) and (width:950px) and (hover:hover)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we have an idea of what we really want to build!  The end result may vary a little bit, but the general goal is very clear.  We can now start writing our first test cases too!  In the end, the actual API I built looks more like:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;media().screen().portrait().width(950).hover()&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Only slight changes, but changes are still ok.  Just because we designed our API first doesn't mean it's set in stone.  At least we now have a target to aim for.&lt;/p&gt;

&lt;p&gt;I hope this method helps you just as much as it has helped me.  Until next time!&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>testing</category>
      <category>devtips</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
