<?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: Florian Reuschel</title>
    <description>The latest articles on DEV Community by Florian Reuschel (@loilo).</description>
    <link>https://dev.to/loilo</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%2F1791%2Fc7edf4ff-17ea-4c59-98ba-c8062a1afba2.jpg</url>
      <title>DEV Community: Florian Reuschel</title>
      <link>https://dev.to/loilo</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/loilo"/>
    <language>en</language>
    <item>
      <title>TypeScript's Secret Parallel Universe</title>
      <dc:creator>Florian Reuschel</dc:creator>
      <pubDate>Mon, 20 Jan 2020 23:20:24 +0000</pubDate>
      <link>https://dev.to/loilo/typescript-s-secret-parallel-universe-54i6</link>
      <guid>https://dev.to/loilo/typescript-s-secret-parallel-universe-54i6</guid>
      <description>&lt;p&gt;Almost four years ago, I was a new TypeScript user, amazed by the possibilities that this freshly learned JavaScript dialect opened up to me. But just like every TypeScript developer, I soon ran into some hard-to-debug problems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f9aovsuypypb0hz6tfj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0f9aovsuypypb0hz6tfj.gif" alt="Animation showing a set of virtual switches labelled with " width="246" height="480"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In TypeScript land, those problems usually stem from the programmer's lack of understanding about the language itself.&lt;/p&gt;




&lt;p&gt;I'd like to introduce you to one of these early problems I had, mostly because it is related to one of the (in my opinion) most underreported topics in the TypeScript tutorial world: the type scope. It's kind of obvious once you realize it exists, but for me it was a frequent source of confusion when I didn't know about it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;My problem was actually very simple: I was building a library with a bunch of classes distributed over various folders.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ea1ykc8q3z4tlthuinp.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2ea1ykc8q3z4tlthuinp.jpg" alt="The Visual Studio Code file tree showing three TypeScript files (Console.ts, File.ts and Stream.ts) in a folder named " width="228" height="185"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;For the library's public API, I wanted those classes to be exposed as a single nested object (e.g. the &lt;code&gt;Console&lt;/code&gt; class from &lt;code&gt;Output/Console.ts&lt;/code&gt; being available as &lt;code&gt;API.Output.Console&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;So I defined some &lt;a href="https://www.typescriptlang.org/docs/handbook/namespaces.html#namespaced-validators" rel="noopener noreferrer"&gt;namespaces&lt;/a&gt;, imported the classes — and then I struggled. I was just not able to re-export the classes from inside the namespaces. 🤨&lt;/p&gt;




&lt;p&gt;First attempt, turned out to be invalid TypeScript:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;Console&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Output/Console&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;Console&lt;/span&gt;
  &lt;span class="c1"&gt;// TS Error 1128: Declaration or statement expected.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Maybe I need to import it under a different name. Okay, second attempt:&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Output/Console&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="nx"&gt;Console&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt;
  &lt;span class="c1"&gt;// TS Error 2304: Cannot find name 'Console'.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Third attempt — maybe doing it the ES Modules way cuts it. (Spoiler: It didn't.)&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Output/Console&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;Console&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="c1"&gt;//TS Error 1194: Export declarations are not permitted in a namespace.&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Fourth attempt using &lt;code&gt;export const&lt;/code&gt;. This actually compiled. 🎉&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Output/Console&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Console&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But unfortunately, whenever I wanted to type hint something with that &lt;code&gt;API&lt;/code&gt; object, I got the following error:&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="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;API&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./API&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;API&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Console&lt;/span&gt;
&lt;span class="c1"&gt;// TS Error 2694: Namespace 'API.Output' has no exported member 'Console'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;...but I exported that member! Why is it not there!?&lt;/p&gt;

&lt;p&gt;Hum. Maybe the &lt;code&gt;export const&lt;/code&gt; is the problem. I should sprinkle some magic TypeScript keywords over the problem and try &lt;code&gt;export type&lt;/code&gt; instead.&lt;/p&gt;

&lt;p&gt;So without further ado: Fifth attempt. It compiles! 🥳&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="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./Output/Console&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="nx"&gt;Output&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Console&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ConsoleAlias&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Okay, reality check: Does the type hint work? It does! 🕺&lt;/p&gt;

&lt;p&gt;...but that joy was short-lived as well. When I tried to create a new &lt;code&gt;Console&lt;/code&gt; instance, TypeScript errors were all over me again:&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="k"&gt;import&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;API&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;./API&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;API&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Output&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Console&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="c1"&gt;// TS Error 2708: Cannot use namespace 'API' as a value.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Oh come on.&lt;/p&gt;

&lt;p&gt;Needless to say that I was pretty fed up with TypeScript at that point.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3exdf7uzpm7zkfjxlzyb.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3exdf7uzpm7zkfjxlzyb.gif" alt="Video of a man throwing his computer into the dumpster" width="480" height="266"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;However, I did not want to believe that there was no solution to my problem, so I went to &lt;a href="https://stackoverflow.com/questions/38655711/export-a-typescript-class-from-another-file-than-it-has-been-defined-in" rel="noopener noreferrer"&gt;StackOverflow&lt;/a&gt; and, after a couple of days with no answer, I created &lt;a href="https://github.com/microsoft/TypeScript/issues/10058" rel="noopener noreferrer"&gt;an issue&lt;/a&gt; directly in TypeScript's GitHub repository.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Solution
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://twitter.com/SeaRyanC" rel="noopener noreferrer"&gt;Ryan&lt;/a&gt; from the TypeScript team was kind enough to answer my question just within a couple of minutes. To me, the solution seemed pretty obvious and pretty obscure at the same time:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ewtf60gp9rdd64x3sup.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ewtf60gp9rdd64x3sup.jpg" alt="Screenshot of Ryan Cavanaugh's answer to my question on GitHub, stating that I should be using both " width="800" height="393"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I applied that approach — and it worked like a charm.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why It Works
&lt;/h2&gt;

&lt;p&gt;Back then, I just accepted that answer and used it in my code. It sounded kind of plausible to me — both, &lt;code&gt;const&lt;/code&gt; and &lt;code&gt;type&lt;/code&gt;, worked in some way, so I just need to combine them to make both use cases working. But there was a sense of unease in it. Why could I export two things under the same name without producing a big fat compiler error?&lt;/p&gt;

&lt;p&gt;It took me some more months (maybe even years) of TypeScript experience to fully understand why this works, but I think that insight might be valuable for others as well, so  I'll share it here:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;TypeScript has a secret &lt;a href="https://developer.mozilla.org/en-US/docs/Glossary/Scope" rel="noopener noreferrer"&gt;scope&lt;/a&gt;.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;TypeScript basically maintains a type scope which is completely independent of the variable scope of JavaScript. This means that you may declare a variable &lt;code&gt;foo&lt;/code&gt; and a type &lt;code&gt;foo&lt;/code&gt; &lt;em&gt;in the same file&lt;/em&gt;. 🤯 They don't even need to be compatible:&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;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bar&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;

&lt;span class="c1"&gt;// ✅ This is absolutely fine for TypeScript&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now classes in TypeScript are a little bit special. What happens if you define a class &lt;code&gt;Foo&lt;/code&gt; is that TypeScript not only creates a variable &lt;code&gt;Foo&lt;/code&gt; (containing the class object itself) it &lt;em&gt;also declares a type &lt;code&gt;Foo&lt;/code&gt;&lt;/em&gt;, representing an instance of the &lt;code&gt;Foo&lt;/code&gt; class.&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;class&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="p"&gt;{}&lt;/span&gt;

&lt;span class="c1"&gt;// We can use Foo as a type&lt;/span&gt;
&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Foo&lt;/span&gt;

&lt;span class="c1"&gt;// We can use Foo as a constructor (i.e. a value)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bar&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Similarly, when importing a name from another file (like we do with &lt;code&gt;ConsoleAlias&lt;/code&gt; in the second code sample), both — the &lt;code&gt;ConsoleAlias&lt;/code&gt; class object and the &lt;code&gt;ConsoleAlias&lt;/code&gt; type — are imported.&lt;/p&gt;

&lt;p&gt;In other words, that single name — the imported &lt;code&gt;ConsoleAlias&lt;/code&gt; — holds both the class object and the type declared in &lt;code&gt;Output/Console.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So if we re-export &lt;code&gt;Console&lt;/code&gt; from inside the &lt;code&gt;Output&lt;/code&gt; namespace by writing &lt;code&gt;export const Console = ConsoleAlias&lt;/code&gt;, only the class &lt;em&gt;object&lt;/em&gt; is exported (because a &lt;code&gt;const&lt;/code&gt; only ever holds a value, not a type). Similarly, if we'd do &lt;code&gt;export type Console = ConsoleAlias&lt;/code&gt;, only the class &lt;em&gt;type&lt;/em&gt; would be exported.&lt;/p&gt;

&lt;p&gt;In a nutshell: Because of the independent scopes, it's valid to export a value as well as a type under the same name. And in some cases (like the one above), this is not only valid but necessary.&lt;/p&gt;




&lt;p&gt;I hope this helped refine your mental model of TypeScript. 🤓&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>variables</category>
      <category>types</category>
      <category>scope</category>
    </item>
    <item>
      <title>(An Approach to) Vue.js Template Variables</title>
      <dc:creator>Florian Reuschel</dc:creator>
      <pubDate>Wed, 05 Dec 2018 00:34:08 +0000</pubDate>
      <link>https://dev.to/loilo/an-approach-to-vuejs-template-variables-5aik</link>
      <guid>https://dev.to/loilo/an-approach-to-vuejs-template-variables-5aik</guid>
      <description>&lt;h2&gt;
  
  
  The Problem
&lt;/h2&gt;

&lt;p&gt;From time to time, I have the need to temporarily store the results of a method call in Vue templates. This is particularly common inside loops, where we cannot easily use computed properties.&lt;/p&gt;

&lt;p&gt;Basically what we'd want to avoid is this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- List.vue --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"id in users"&lt;/span&gt; &lt;span class="na"&gt;:key=&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;:src=&lt;/span&gt;&lt;span class="s"&gt;"getUserData(id).avatar"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
    🏷️ {{ getUserData(id).name }}&lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
    🔗 {{ getUserData(id).homepage }}
  &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Common Solutions
&lt;/h2&gt;

&lt;p&gt;We could describe this problem as "computed properties with arguments", and it already has some established solutions out there:&lt;/p&gt;

&lt;h3&gt;
  
  
  Outsource Components
&lt;/h3&gt;

&lt;p&gt;The pretty much canonical way is done through refactoring: We could outsource the &lt;code&gt;&amp;lt;li&amp;gt;&lt;/code&gt; items into their own &lt;code&gt;&amp;lt;ListItem&amp;gt;&lt;/code&gt; component.&lt;/p&gt;

&lt;p&gt;That component would receive the &lt;code&gt;id&lt;/code&gt; as a prop and store the according metadata in a computed property which is then cached by Vue until it needs to be re-evaluated.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- List.vue --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;ListItem&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"id in users"&lt;/span&gt; &lt;span class="na"&gt;:key=&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt; &lt;span class="na"&gt;:id=&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- ListItem.vue --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;:src=&lt;/span&gt;&lt;span class="s"&gt;"metadata.avatar"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  🏷️ {{ metadata.name }}&lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
  🔗 {{ metadata.homepage }}
&lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, this approach can be pretty tedious to write and maintain: all pieces of data we need inside each list item have to be passed down to the &lt;code&gt;&amp;lt;ListItem&amp;gt;&lt;/code&gt; as props.&lt;/p&gt;

&lt;p&gt;It can also be hard to follow as a reader — particularly if the &lt;code&gt;&amp;lt;ListItem&amp;gt;&lt;/code&gt; component is very small. Then it may easily contain four lines of template code followed by 25 lines of props definition boilerplate.&lt;/p&gt;

&lt;h3&gt;
  
  
  Memoize Method Results
&lt;/h3&gt;

&lt;p&gt;We could also memoize the results of &lt;code&gt;getUserData()&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;However this can be tedious to implement as well, it usually only works with serializable input data — and of all approaches, adding another layer of memoization on top of Vue feels like suiting the Vue way™ the least.&lt;/p&gt;

&lt;h2&gt;
  
  
  My Approach
&lt;/h2&gt;

&lt;p&gt;For my projects, I like to use another (less obvious, and AFAICT less common) approach: I create a helper component I call &lt;code&gt;&amp;lt;Pass&amp;gt;&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It's really, really tiny:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;Pass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$scopedSlots&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;default&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;$attrs&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;Basically this is a placeholder component which does not render a DOM element itself but passes down all props it receives to its child.&lt;/p&gt;

&lt;p&gt;So, let's rewrite our list with the &lt;code&gt;&amp;lt;Pass&amp;gt;&lt;/code&gt; helper:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight vue"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- List.vue --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;ul&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;Pass&lt;/span&gt; &lt;span class="na"&gt;v-for=&lt;/span&gt;&lt;span class="s"&gt;"id in users"&lt;/span&gt; &lt;span class="na"&gt;:key=&lt;/span&gt;&lt;span class="s"&gt;"id"&lt;/span&gt; &lt;span class="na"&gt;:metadata=&lt;/span&gt;&lt;span class="s"&gt;"getUserData(id)"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;li&lt;/span&gt; &lt;span class="na"&gt;slot-scope=&lt;/span&gt;&lt;span class="s"&gt;"{ metadata }"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt; &lt;span class="na"&gt;:src=&lt;/span&gt;&lt;span class="s"&gt;"metadata.avatar"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;br&amp;gt;&lt;/span&gt;
      🏷️ {{ metadata.name }}&lt;span class="nt"&gt;&amp;lt;br&amp;gt;&lt;/span&gt;
      🔗 {{ metadata.homepage }}
    &lt;span class="nt"&gt;&amp;lt;/li&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/Pass&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/ul&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will only evaluate &lt;code&gt;getUserData()&lt;/code&gt; once: when &lt;code&gt;&amp;lt;Pass&amp;gt;&lt;/code&gt; is rendered. Nice and clean, isn't it?&lt;/p&gt;

&lt;p&gt;Also, here's a CodeSandbox where you can fiddle around with the example I described:&lt;/p&gt;

&lt;p&gt;&lt;iframe src="https://codesandbox.io/embed/1v50nxwx77"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h3&gt;
  
  
  Caveats
&lt;/h3&gt;

&lt;p&gt;To be fully honest, there are a few drawbacks to this approach:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The helper component utilizes a &lt;a href="https://vuejs.org/v2/guide/components-slots.html#Scoped-Slots" rel="noopener noreferrer"&gt;scoped slot&lt;/a&gt; for passing data. This means, &lt;code&gt;&amp;lt;Pass&amp;gt;&lt;/code&gt; can only have exactly one child component.&lt;/li&gt;
&lt;li&gt;Another limitation to this approach is that the markup injected into the slot &lt;em&gt;must&lt;/em&gt; render a real DOM node. We can't just set the &lt;code&gt;slot-scope&lt;/code&gt; on something like a &lt;code&gt;&amp;lt;template&amp;gt;&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's it. I hope this helps simplify your Vue templates!&lt;/p&gt;

</description>
      <category>vue</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
