<?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: oneothebrave</title>
    <description>The latest articles on DEV Community by oneothebrave (@oneothebrave).</description>
    <link>https://dev.to/oneothebrave</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%2F541607%2F114a57de-9a40-434f-861e-6d28f327663a.jpg</url>
      <title>DEV Community: oneothebrave</title>
      <link>https://dev.to/oneothebrave</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/oneothebrave"/>
    <language>en</language>
    <item>
      <title>TS: Type Record&lt;K, T&gt;</title>
      <dc:creator>oneothebrave</dc:creator>
      <pubDate>Tue, 19 Dec 2023 14:38:15 +0000</pubDate>
      <link>https://dev.to/oneothebrave/ts-type-recordk-t-1ii5</link>
      <guid>https://dev.to/oneothebrave/ts-type-recordk-t-1ii5</guid>
      <description>&lt;p&gt;&lt;strong&gt;Record&lt;/strong&gt; type is an advanced type of TS. Let's see its definition first:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Record constructs an object type whose property keys are Keys and whose property values are Type. This utility can be used to map the properties of a type to another type.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Flexiable keys
&lt;/h2&gt;

&lt;p&gt;It is usually used when require a dictionary-like object. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const records: Record&amp;lt;number, string&amp;gt; = {
    1: 'a',
    2: 'b'
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a very simple but useful usage scenario. Its &lt;em&gt;property keys&lt;/em&gt; are number and its &lt;em&gt;property values&lt;/em&gt; are string. In this scenario, records don't have fixed number of member, it's extenable, you can add as much number-string pair as you like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const pair: Record&amp;lt;number, string&amp;gt; = {
    1: 'a',
    2: 'b',
    3: 'c',
    ....
    99: 'X'
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No error would be report , of course.&lt;/p&gt;

&lt;h2&gt;
  
  
  Fixed keys
&lt;/h2&gt;

&lt;p&gt;While the number of keys of &lt;strong&gt;Record&lt;/strong&gt; type can also be strict, let demostrate it by create a type "&lt;strong&gt;gameName&lt;/strong&gt;" and an interface "&lt;strong&gt;comment&lt;/strong&gt;", then combine them to serve a &lt;strong&gt;Record&lt;/strong&gt; type:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type gameName = 'Sekiro' | 'Cyberpunk2077' | 'GTA'
interface comment {
  hours: number,
  review: 'good' | 'bad'
}

const game: Record&amp;lt;name, comment&amp;gt; = {}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this moment, if you code it in a qulified IDE, it will report an error on variable game saying: &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Type '{}' is missing the following properties from type 'Record': Sekiro, Cyberpunk2077, GTA&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Which shows that the keys of &lt;strong&gt;Record&lt;/strong&gt; type game is &lt;strong&gt;&lt;em&gt;No more, no less, no deviation&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's complete the missing property&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const game: Record&amp;lt;gameName, comment&amp;gt; = {
    'Sekiro': {
        hours: 200,
        review: 'good'
    },
    'Cyberpunk2077': {
        hours: 30,
        review: 'bad'
    },
    'GTA': {
        hours: 500,
        review: 'good'
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now everything is perfect, but if we try to add an extra property "Metro":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const game: Record&amp;lt;gameName, comment&amp;gt; = {
    'Sekiro': {
        hours: 200,
        review: 'good'
    },
    'Cyberpunk2077': {
        hours: 30,
        review: 'bad'
    },
    'GTA': {
        hours: 500,
        review: 'good'
    },
    'Metro': {
        hours: 100,
        review: 'good'
    }
  }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Record will immediately yell out:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Object literal may only specify known properties, and ''Metro'' does not exist in type 'Record'.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Source code
&lt;/h2&gt;

&lt;p&gt;Here is the source code of &lt;strong&gt;Record&lt;/strong&gt; type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Construct a type with a set of properties K of type T
 */
type Record&amp;lt;K extends keyof any, T&amp;gt; = {
    [P in K]: T;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is, each attribute in K ([P in K]) is converted to type T. &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
