<?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: Brian Eyster</title>
    <description>The latest articles on DEV Community by Brian Eyster (@brianeyster).</description>
    <link>https://dev.to/brianeyster</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%2F245828%2F5ec84a1d-7be2-4ba0-9a2a-f3caa43477cf.png</url>
      <title>DEV Community: Brian Eyster</title>
      <link>https://dev.to/brianeyster</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/brianeyster"/>
    <language>en</language>
    <item>
      <title>The Value of a Pick&lt;&gt;</title>
      <dc:creator>Brian Eyster</dc:creator>
      <pubDate>Thu, 06 Jan 2022 23:43:56 +0000</pubDate>
      <link>https://dev.to/brianeyster/the-value-of-a-pick-9g8</link>
      <guid>https://dev.to/brianeyster/the-value-of-a-pick-9g8</guid>
      <description>&lt;p&gt;Recently I was writing an API handler that retrieves an object and then returns a partial copy with only the object properties “picked” by the caller.  Pretty standard stuff... until TypeScript intervened.  &lt;/p&gt;

&lt;p&gt;In Typescript, we have the generic utility type &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt;.  It’s super handy.  &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt;  returns a &lt;strong&gt;type&lt;/strong&gt; with only some of the properties (described by the string union &lt;code&gt;K&lt;/code&gt;) of the original object’s type (&lt;code&gt;T&lt;/code&gt;). Since &lt;code&gt;Pick&lt;/code&gt; is a Typescript utility &lt;em&gt;type&lt;/em&gt;, it only acts on the &lt;em&gt;types&lt;/em&gt; (not the &lt;em&gt;values&lt;/em&gt; of the object).  So all Pick’s hard work gets effectively &lt;a href="https://github.com/Microsoft/TypeScript/wiki/FAQ#what-is-type-erasure"&gt;erased at runtime&lt;/a&gt; and doesn’t alter the actual object being returned. 😔    &lt;/p&gt;

&lt;p&gt;How do we code this same Pick-like functionality in the world of runtime &lt;em&gt;values&lt;/em&gt;, while still preserving the type safety of TypeScript?  My investigation into this seemingly simple question, led me to several interesting discoveries and surprises about TypeScript.&lt;/p&gt;

&lt;h2&gt;
  
  
  Our musical example
&lt;/h2&gt;

&lt;p&gt;To illustrate my example, let’s call on one of the most  &lt;a href="https://www.punchbrothers.com/"&gt;inspiring bands in progressive acoustic music&lt;/a&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;PunchBrother&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&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;instrument&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;leadSinger&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;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;mandolinist&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chris Thile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// virtuoso mandolinist&lt;/span&gt;
  &lt;span class="na"&gt;instrument&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;mandolin&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
  &lt;span class="na"&gt;leadSinger&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="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Our aim is to write a function that returns just a few properties of the &lt;code&gt;mandolinist&lt;/code&gt; object:&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;function&lt;/span&gt; &lt;span class="nx"&gt;punchBrotherPick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;musician&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PunchBrother&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;keys&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;PunchBrother&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PunchBrother&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// ... ??? ...&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;partialBrother&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;Note that we define the return type using Typescript’s &lt;code&gt;Partial&amp;lt;T&amp;gt;&lt;/code&gt; utility type since we may only be selecting &lt;em&gt;some&lt;/em&gt; of the properties of the object (and thus omitting others).&lt;/p&gt;

&lt;p&gt;We'll then call our function like:&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;mandolinistName&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;punchBrotherPick&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;mandolinist&lt;/span&gt;&lt;span class="p"&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;name&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]);&lt;/span&gt;

&lt;span class="nx"&gt;mandolinistName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Chris Thile&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true&lt;/span&gt;
&lt;span class="nx"&gt;mandolinistName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instrument&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// true, type is Partial&amp;lt;PunchBrother&amp;gt;&lt;/span&gt;
&lt;span class="nx"&gt;mandolinistName&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;faveCocktail&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// type error, 'faveCocktail' does not exist on Partial&amp;lt;PunchBrother&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🎵  &lt;a href="https://youtu.be/_H2o217hqJU?t=57"&gt;My, oh my.&lt;/a&gt;  What a wonderful day we’re having… 🎵&lt;/p&gt;

&lt;h2&gt;
  
  
  Destructuring a dynamic list of properties
&lt;/h2&gt;

&lt;p&gt;Quick searches on StackOverflow all suggest the elegant approach of object destructuring with rest parameters:&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;key1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;withoutKey1Key2&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;origObj&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ah, yes.  I love that destructuring syntax for its simple clarity. &lt;code&gt;withoutKey1Key2&lt;/code&gt; now contains all properties in &lt;code&gt;origObj&lt;/code&gt; minus &lt;code&gt;key1&lt;/code&gt; and &lt;code&gt;key2&lt;/code&gt;.   &lt;/p&gt;

&lt;p&gt;Note that this one-liner more closely mimics Typescript’s &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; since &lt;code&gt;withoutKey1Key2&lt;/code&gt; now omits &lt;code&gt;key1&lt;/code&gt; and &lt;code&gt;key2&lt;/code&gt;.  But we can quickly spread the &lt;code&gt;key1&lt;/code&gt; and &lt;code&gt;key2&lt;/code&gt; properties back into a new object to get the functionality similar to Pick.&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;key1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;rest&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;origObj&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;onlyKey1Key2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;key1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key2&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Unfortunately, this approach won’t work here.  Destructuring only works when the number of extracted properties is static and known at compile time.  In our more general case of picking an arbitrary, dynamic array of properties (specified by the caller as an array of keys), destructuring isn’t possible  (&lt;a href="https://stackoverflow.com/questions/54749242/delete-a-dynamic-key-from-a-typescript-object"&gt;See this SO article&lt;/a&gt;) .  &lt;/p&gt;

&lt;p&gt;A couple asides:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Note that you &lt;em&gt;can&lt;/em&gt; destructure with a dynamic key &lt;em&gt;name&lt;/em&gt; via &lt;code&gt;{ [keyNameVar]: var, …rest}&lt;/code&gt;.  Very hip!&lt;/li&gt;
&lt;li&gt;The problem here is specifying an arbitrary &lt;em&gt;quantity&lt;/em&gt; of these dynamic keys.  You’d need a meta-programming way of specifying the destructure syntax.  If that’s possible in Javascript I’d love to hear about it!&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Clone then mutate
&lt;/h2&gt;

&lt;p&gt;Another option is to first clone the object (using your clone method of choice), then selectively remove the properties we don’t need via Javascript’s &lt;code&gt;delete&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;partialThile&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PunchBrother&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;({},&lt;/span&gt; &lt;span class="nx"&gt;mandolinist&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// cloned object&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;partialThile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;instrument&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;delete&lt;/span&gt; &lt;span class="nx"&gt;partialThile&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;leadSinger&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It’s nice to know that &lt;code&gt;delete&lt;/code&gt; is sound with regards to types.  In order for a property to be deleted, Typescript requires that the property must already be optional on the object. Well done, TS! &lt;/p&gt;

&lt;p&gt;But I’m not thrilled with this approach, as it is more analogous in spirit to Typescript’s &lt;code&gt;Omit&lt;/code&gt;.  We have to clone the entire object, then remove the fields that we &lt;strong&gt;don’t&lt;/strong&gt; want to include.  This approaches the idea of &lt;code&gt;Pick&lt;/code&gt; from its inverse.  &lt;/p&gt;

&lt;p&gt;Interestingly, &lt;code&gt;Omit&lt;/code&gt; itself is defined in TS (&lt;a href="https://github.com/microsoft/TypeScript/blob/cec2fda9a53620dc545a2c4d7b0156446ab145b4/lib/lib.es5.d.ts"&gt;/lib/es5.d.ts&lt;/a&gt;) using Pick and Exclude:&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;Omit&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="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="kr"&gt;keyof&lt;/span&gt; &lt;span class="kr"&gt;any&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Pick&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="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;Exclude&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But let’s dig deeper into this approach as there are some other potential problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Iterating over keys of an object
&lt;/h3&gt;

&lt;p&gt;At runtime, all properties of an object are visible, even ones that have been “hidden” from TypeScript via type narrowing. We might iterate over the keys of a PunchBrother object expecting to see just our 3 PunchBrother properties, but actually see additional properties.  Consider 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="c1"&gt;// Punch Brothers bassist&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;paulKowert&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Paul Kowert&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;instrument&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;bass&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;leadSinger&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="na"&gt;otherBands&lt;/span&gt;&lt;span class="p"&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;Hawktail&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;// field not declared on PunchBrothers type&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;punchPaul&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;PunchBrother&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;paulKowert&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// type narrowing&lt;/span&gt;

&lt;span class="nx"&gt;punchPaul&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;otherBands&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Type Error: Property 'otherBands' does not exist on type 'PunchBrother'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As expected, TypeScript errors if we attempt to access &lt;code&gt;punchPaul.otherBands&lt;/code&gt;.  But at runtime, if we attempt to iterate over the keys of &lt;code&gt;punchPaul&lt;/code&gt;, we will see the &lt;code&gt;otherBands&lt;/code&gt; property as well as the 3 PunchBrother properties. Type narrowing like this only happens at compile time; these types are completely erased from the runtime Javascript.&lt;/p&gt;

&lt;p&gt;The TypeScript designers made the decision to type the return value of &lt;code&gt;Object.keys&lt;/code&gt; and &lt;code&gt;for..in&lt;/code&gt; as &lt;code&gt;string&lt;/code&gt; rather than &lt;code&gt;keyof obj&lt;/code&gt; for this reason: the compiler just can’t be certain there aren’t other properties on the object.  (See lots of great info and links on this &lt;a href="https://stackoverflow.com/questions/59233965/typescript-element-implicitly-has-type-any-with-for-in-loops"&gt;StackOverflow post&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;We can get &lt;em&gt;some&lt;/em&gt; type safety by using the &lt;code&gt;for…in&lt;/code&gt; syntax.  If we declare the key variable inside the &lt;code&gt;for..in&lt;/code&gt; the key will be of type string. But we can declare our &lt;code&gt;key&lt;/code&gt; variable prior to the &lt;code&gt;for..in&lt;/code&gt; and include a type annotation:&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;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;PunchBrother&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="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;punchPaul&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;span class="c1"&gt;// type of key is still `keyof PunchBrother`&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Curiously (?), we can annotate our type with a narrower type here (&lt;code&gt;keyof PunchBrother&lt;/code&gt; is narrower than &lt;code&gt;string&lt;/code&gt;) and not receive a TypeScript error when using the variable in the &lt;code&gt;for..in&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This satisfies the TypeScript compiler, but it is not sound.  In our &lt;code&gt;punchPaul&lt;/code&gt; example, the runtime value of &lt;code&gt;key&lt;/code&gt; can still be &lt;code&gt;otherBands&lt;/code&gt; which is not a member of the union &lt;code&gt;keyof PunchBrother&lt;/code&gt;.  &lt;/p&gt;

&lt;p&gt;The use of &lt;code&gt;for..in&lt;/code&gt; this way is fine if we know that our object exactly matches the type and doesn’t possesses any properties beyond those declared in the type. But if our object is narrowed from another type, as in the case above, the type declaration for &lt;code&gt;key&lt;/code&gt; may not be sound. &lt;/p&gt;

&lt;p&gt;Given the potential unsoundness of iterating over object keys, as well as the semantic backwardness of a “clone then mutate” approach, let’s look at a better solution. &lt;/p&gt;

&lt;h2&gt;
  
  
  Selectively copy properties
&lt;/h2&gt;

&lt;p&gt;The more natural approach to our initial issue is to begin with an empty object (&lt;code&gt;{}&lt;/code&gt;) and selectively copy the requested properties from the source object.  (This is the approach used by the Just utility library’s &lt;a href="https://github.com/angus-c/just#just-pick"&gt;just-pick&lt;/a&gt;.)&lt;/p&gt;

&lt;p&gt;Here’s the naive code:&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;thileInstrument&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PunchBrother&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="c1"&gt;// must be Partial&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;PunchBrother&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;instrument&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;key&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="nx"&gt;thileInstrument&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thile&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// Error: Type 'string | boolean' is not assignable to type 'undefined'.&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we reach the most surprising hurdle of this article: copying fields between 2 objects. Our innocent little code: &lt;code&gt;target[key] = src[key]&lt;/code&gt; yields a type error: &lt;code&gt;Type 'string | boolean' is not assignable to type 'undefined'.&lt;/code&gt;  &lt;/p&gt;

&lt;p&gt;Huh?  Isn’t it self-evident that this is type-safe?  The objects are the same type, we’re using the same keys, shouldn’t all the types match?  And equally surprising, why is the type of the left-hand-side (target[key]) 'undefined'?&lt;/p&gt;

&lt;p&gt;Let’s break this down from the perspective of the TypeScript compiler.  For each iteration of the loop, there is a single key.  But at compile time, Typescript doesn’t know &lt;em&gt;which&lt;/em&gt; key. So it also can’t know the type of the property in the object: &lt;code&gt;srcObj[key]&lt;/code&gt;.   &lt;/p&gt;

&lt;p&gt;For clarity, let’s introduce a temporary variable for the right-hand side (RHS) value:&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="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;key&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="nx"&gt;rhs&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;thile&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="c1"&gt;// inferred type is: 'string | boolean'&lt;/span&gt;
  &lt;span class="nx"&gt;thileInstrument&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;rhs&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Error!&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Type of the RHS
&lt;/h3&gt;

&lt;p&gt;The type of the right-hand side in the assignment is the &lt;strong&gt;union&lt;/strong&gt; of all possible property types in the object.   &lt;/p&gt;

&lt;p&gt;To quickly unpack this indexed access type: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The type of &lt;code&gt;key&lt;/code&gt; is &lt;code&gt;’name’ | ‘instrument’ | ‘singer’&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;So the type of &lt;code&gt;rhs&lt;/code&gt; is &lt;code&gt;PunchBrother[’name’ | ‘numInstruments’ | ‘singer’]&lt;/code&gt; &lt;/li&gt;
&lt;li&gt;After distributing out the string union: &lt;code&gt;PunchBrothers[‘name’] | PunchBrothers[‘instrument’] | PunchBrothers[‘singer’]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;This simplifies to: &lt;code&gt;string | boolean&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Type of the LHS
&lt;/h3&gt;

&lt;p&gt;While the type of the RHS feels immediately intuitive (the union of all property types), the type of the left-hand side of the assignment is somewhat surprising.  &lt;/p&gt;

&lt;p&gt;TypeScript resolves the type of a left-hand side of an assignment to be the &lt;strong&gt;intersection&lt;/strong&gt; 🤯 of the types of all properties on the object.  (Let that sink in for a minute...) This is a deliberate (though unfamiliar to me!) decision by the TypeScript designers to make assignments as sound as possible.   For more details see &lt;a href="https://github.com/microsoft/TypeScript/pull/30769"&gt;this TypeScript PR discussion&lt;/a&gt; and this &lt;a href="https://fettblog.eu/typescript-unexpected-intersections/"&gt;excellent post about “unexpected intersections”&lt;/a&gt;).  &lt;/p&gt;

&lt;p&gt;🎵 It’s all  &lt;a href="https://www.youtube.com/watch?v=y64KTlk6N_s"&gt;part of the plan&lt;/a&gt; 🎵.&lt;/p&gt;

&lt;p&gt;The basic intuition is that the type of LHS should resolve to the set of types that can safely be assigned to. This type set is represented by the &lt;em&gt;intersection&lt;/em&gt; of all the property types.   When the intersection of property types is a single concrete type, the type-safety of this assignment is clear.  For example, if the object type was the simpler: &lt;code&gt;Record&amp;lt;K, string&amp;gt;&lt;/code&gt; then the intersection of &lt;code&gt;string &amp;amp; string &amp;amp; string&lt;/code&gt;would be &lt;code&gt;string&lt;/code&gt; and the assignment above would be type-safe.  &lt;/p&gt;

&lt;p&gt;But in our case the type of the LHS is: &lt;code&gt;’string &amp;amp; number &amp;amp; undefined’&lt;/code&gt; (Recall that our LHS is of type &lt;code&gt;Partial&amp;lt;PunchBrother&amp;gt;&lt;/code&gt; so each property may also be &lt;code&gt;undefined&lt;/code&gt;.)  &lt;/p&gt;

&lt;p&gt;As &lt;code&gt;string&lt;/code&gt; and &lt;code&gt;number&lt;/code&gt; do not overlap, this intersection should resolve to &lt;code&gt;never&lt;/code&gt;. Or in our specific case, where our left-hand side object is a &lt;code&gt;Partial&amp;lt;&amp;gt;&lt;/code&gt;, this may actually resolve to  &lt;code&gt;undefined&lt;/code&gt;.  Regardless, the types in the LHS and RHS aren’t compatible.  &lt;/p&gt;

&lt;p&gt;(🎵  &lt;a href="https://youtu.be/iXE_K2Kpoqc?t=633"&gt;I'm a magnet&lt;/a&gt; , And you're a magnet, And we're pushing each other away. 🎵)&lt;/p&gt;

&lt;h3&gt;
  
  
  A TypeScript assignment solution
&lt;/h3&gt;

&lt;p&gt;Given the type incompatibility between the LHS and RHS of the assignment, we need a different approach.  The problem is that TypeScript only knows the type of either side as &lt;code&gt;T[K]&lt;/code&gt;, where &lt;code&gt;K&lt;/code&gt; is the &lt;em&gt;set of all keys&lt;/em&gt;.  So intuitively, the solution is to explicitly &lt;em&gt;freeze&lt;/em&gt; (technically called “bind”) the specific key for the LHS and RHS on each iteration of the loop.  Let’s call a generic helper function for each different key value:&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;function&lt;/span&gt; &lt;span class="nx"&gt;copyField&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&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="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Readonly&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&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;TypeScript is perfectly happy with this assignment.  It now knows the objects are the same type, the key is a property on their type, and we’re accessing the same property in both objects.   &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;In order for TypeScript to fully resolve the specific LHS and RHS type,  you might think we’d need to also make the key type explicit like so: &lt;code&gt;function copyField&amp;lt;T, K extends keyof T&amp;gt;(target: T, src: Readonly&amp;lt;T&amp;gt;, key: K)&lt;/code&gt;. But simply introducing the single &lt;code&gt;T&lt;/code&gt; generic parameter is sufficient for TypeScript to infer the specific type for the LHS and RHS.  Thus, we are able to avoid introducing a second generic parameter which would otherwise violate  &lt;a href="https://effectivetypescript.com/2020/08/12/generics-golden-rule/"&gt;The Golden Rule of Generics&lt;/a&gt; .&lt;br&gt;&lt;br&gt;
🎵 Heaven’s a  &lt;a href="https://www.youtube.com/watch?v=aerar7MjtFM"&gt;julep on the porch&lt;/a&gt; 🎵! &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Adding this utility function to the loop, here’s our full type-safe solution.&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;thileInstrument&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Partial&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;PunchBrother&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Array&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;PunchBrother&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="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;instrument&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;copyField&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;target&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="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nb"&gt;Readonly&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="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;keyof&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;target&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;src&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nx"&gt;fields&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;key&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="nx"&gt;copyField&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;thileInstrument&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;thile&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;key&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// TypeScript success!&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Depending on the situation, it might make sense to inline this 1-line &lt;code&gt;copyField()&lt;/code&gt; function as a quick TypeScript IIFE. But that risks further obfuscating the solution to our seemingly very simple situation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One more aside: My initial TypeScript intuition was to search for a runtime / type-guard solution, along the lines of &lt;code&gt;if typeof target[key] === typeof src[key]&lt;/code&gt;.  That’s often the solution to runtime type issues.  But alas that approach doesn’t generalize for the common case of nested objects, since &lt;code&gt;typeof&lt;/code&gt; a nested object just returns &lt;code&gt;object&lt;/code&gt;. There’s probably a solution involving recursively checking types of nested properties.  But at that point, our 1-line &lt;code&gt;copyField()&lt;/code&gt; utility function is looking pretty nice!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Ok, but is this worth it?
&lt;/h2&gt;

&lt;p&gt;In general, the aim of TypeScript is to provide safety and confidence in parts of our code where we might realistically make a mistake and introduce a bug.  &lt;/p&gt;

&lt;p&gt;Part of TypeScript’s allure lies in the fact that programmers are rarely good at knowing where they’re “realistically” likely to make a mistake — or where future maintainers might introduce a compounding mistake.  In complicated code with function calls spanning many files, this compile-time static validation is invaluable.  But is a simple copying of values between 2 objects of the same type one of those areas?  &lt;/p&gt;

&lt;p&gt;Couldn’t we have just asserted type &lt;code&gt;any&lt;/code&gt; on the right-hand side of the assignment and been done awhile ago?  (or suppress the error via &lt;code&gt;// @ts-ignore&lt;/code&gt;) ? &lt;/p&gt;

&lt;p&gt;Isn’t the added complexity (over-engineering?!) of this code more likely to introduce future confusion than the added type safety of the original assignment?  We’re introducing an additional function (or IIFE) with a TypeScript generic, and we’re ( 😱 eek! 😱) mutating one of our function arguments. Is it worth all that additional complexity?&lt;/p&gt;

&lt;p&gt;It’s up to you and your team.  But this utility function does provide the additional confidence that: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;both the source and the target object are the same type, &lt;/li&gt;
&lt;li&gt;the key is valid on the objects,&lt;/li&gt;
&lt;li&gt;we’re copying the same key (and thus the same type) on both sides of the assignment operator.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Ultimately, I think this falls into the gray area of a static tool like TypeScript.  If your code is self-evident and isolated, then the additional cognitive overhead might not be necessary.  But used with complex objects that might be subtypes, I can see a value in this little one-liner.  &lt;/p&gt;

&lt;p&gt;What do you think?  Was this a worthwhile use of TypeScript generics?  I'd love to hear your thoughts in the comments below.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>todayilearned</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
