<?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: nukeop</title>
    <description>The latest articles on DEV Community by nukeop (@nukeop).</description>
    <link>https://dev.to/nukeop</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%2F321356%2F79d47ff0-6f98-4e6b-a503-16370f5e4c7a.png</url>
      <title>DEV Community: nukeop</title>
      <link>https://dev.to/nukeop</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nukeop"/>
    <language>en</language>
    <item>
      <title>Unleashing the Power of Typescript's Mapped Types</title>
      <dc:creator>nukeop</dc:creator>
      <pubDate>Wed, 22 Feb 2023 01:19:46 +0000</pubDate>
      <link>https://dev.to/nukeop/unleashing-the-power-of-typescripts-mapped-types-5878</link>
      <guid>https://dev.to/nukeop/unleashing-the-power-of-typescripts-mapped-types-5878</guid>
      <description>&lt;p&gt;As developers, we all love the power and flexibility that Typescript brings to our code. But did you know that Typescript's mapped types allow you to create dynamic types that change based on the values of your variables? In this post, I'm going to dive into this lesser-known Typescript feature and show you how it can help you write even more powerful and flexible code.&lt;/p&gt;

&lt;p&gt;Mapped types allow you to create new types by transforming properties of an existing type. In essence, they're like a map function, but for types. For example, say you have an interface 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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;foo&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="nl"&gt;bar&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="nl"&gt;baz&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use a mapped type to create a new type that transforms all the property types to &lt;code&gt;string&lt;/code&gt;:&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;MyStringInterface&lt;/span&gt; &lt;span class="o"&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;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This creates a new type called &lt;code&gt;MyStringInterface&lt;/code&gt; that has the same properties as &lt;code&gt;MyInterface&lt;/code&gt;, but with all the property types set to &lt;code&gt;string&lt;/code&gt;. So if you have a variable of type &lt;code&gt;MyInterface&lt;/code&gt;, you can assign it to a variable of type &lt;code&gt;MyStringInterface&lt;/code&gt;:&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;myInterface&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;bar&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="na"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;myStringInterface&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MyStringInterface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;myInterface&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a powerful feature that can save you a lot of time and effort. But what if you want to create a type that only transforms some of the properties, based on their values? That's where things get really interesting.&lt;/p&gt;

&lt;p&gt;Say you have a list of properties that you want to transform to &lt;code&gt;string&lt;/code&gt;:&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;propertiesToTransform&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="s2"&gt;foo&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="s2"&gt;baz&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="kd"&gt;const&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can use a conditional type to create a new type that transforms only the properties in the list to &lt;code&gt;string&lt;/code&gt;:&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;MyConditionalStringInterface&lt;/span&gt; &lt;span class="o"&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;K&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;MyInterface&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;propertiesToTransform&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="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="nx"&gt;MyInterface&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;K&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 creates a new type called &lt;code&gt;MyConditionalStringInterface&lt;/code&gt; that has the same properties as &lt;code&gt;MyInterface&lt;/code&gt;, but with only the properties in &lt;code&gt;propertiesToTransform&lt;/code&gt; set to &lt;code&gt;string&lt;/code&gt;.&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;myConditionalStringInterface&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;MyConditionalStringInterface&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;hello&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;bar&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="na"&gt;baz&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is just scratching the surface of what you can do with mapped types. You can use them to create types that depend on the values of your variables, types that filter out certain properties, and much more. With a little creativity, you can unleash the full power of Typescript's type system and write even more robust and flexible code.&lt;/p&gt;

&lt;p&gt;So the next time you're working with Typescript, keep mapped types in mind. They might just be the key to unlocking the next level of your code.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>programming</category>
      <category>webdev</category>
    </item>
    <item>
      <title>If Copilot generates postcardware, where should you send the post card?</title>
      <dc:creator>nukeop</dc:creator>
      <pubDate>Sat, 25 Jun 2022 23:32:34 +0000</pubDate>
      <link>https://dev.to/nukeop/if-copilot-generates-postcardware-where-should-you-send-the-post-card-4kkm</link>
      <guid>https://dev.to/nukeop/if-copilot-generates-postcardware-where-should-you-send-the-post-card-4kkm</guid>
      <description>&lt;p&gt;I was recently asked a question that stumped me: where should one send a postcard if an artificial intelligence coding assist tool accidentally generates license stating that the code it made is postcardware?&lt;/p&gt;

&lt;p&gt;I'm not sure what the answer is, but I thought I'd share my thoughts on the matter.&lt;/p&gt;

&lt;p&gt;It seems to me that there are two possible options. First, you could send the postcard to the company that made the tool. After all, they're the ones who released the code into the world, and they should be held responsible for its actions.&lt;/p&gt;

&lt;p&gt;Alternatively, you could send the postcard to the person who wrote the code that the tool generated. They're the ones who actually created the code, so they should be the ones to get the credit (or blame) for it.&lt;/p&gt;

&lt;p&gt;Personally, I lean towards the latter option. It seems only fair that the person who created the code should be the one to receive the postcard. But ultimately, the decision is up to you.&lt;/p&gt;

&lt;p&gt;So, where should you send your postcard? I'm not sure. But I hope this article has given you something to think about.&lt;/p&gt;

</description>
      <category>githubcopilot</category>
      <category>ai</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
