<?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: Vitaly Obolensky</title>
    <description>The latest articles on DEV Community by Vitaly Obolensky (@vitalyobolensky).</description>
    <link>https://dev.to/vitalyobolensky</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F895634%2F851ebc03-8a9b-44cb-b818-5510bb5bae46.png</url>
      <title>DEV Community: Vitaly Obolensky</title>
      <link>https://dev.to/vitalyobolensky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vitalyobolensky"/>
    <language>en</language>
    <item>
      <title>12 Tricky TypeScript Questions That Separate Good Developers From Great Ones</title>
      <dc:creator>Vitaly Obolensky</dc:creator>
      <pubDate>Tue, 09 Jun 2026 14:40:53 +0000</pubDate>
      <link>https://dev.to/vitalyobolensky/12-tricky-typescript-questions-that-separate-good-developers-from-great-ones-318e</link>
      <guid>https://dev.to/vitalyobolensky/12-tricky-typescript-questions-that-separate-good-developers-from-great-ones-318e</guid>
      <description>&lt;h1&gt;
  
  
  12 Tricky TypeScript Questions That Separate Good Developers From Great Ones
&lt;/h1&gt;

&lt;p&gt;Short, sharp, and deliberately uncomfortable — &lt;strong&gt;12 senior-level TypeScript questions&lt;/strong&gt; for developers who think they know TypeScript. Type-level edge cases, compiler quirks, and the gap between "I use TypeScript" and "I understand how the compiler thinks."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics covered:&lt;/strong&gt; Type System, Generics, Functions, Type Guards, Interfaces and Types, Modules.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Ambient declaration vs implementation in global scope
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Variables and Declarations · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;You need a globally accessible constant in a &lt;code&gt;.d.ts&lt;/code&gt;-style setup where the same file may be included multiple times. What is the correct TypeScript declaration pattern — and why?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;declare const&lt;/code&gt; introduces an &lt;strong&gt;ambient declaration&lt;/strong&gt;: it asserts that the symbol exists at runtime without emitting JavaScript. That avoids duplicate-definition errors when the file is processed more than once (for example with legacy &lt;code&gt;--outFile&lt;/code&gt; or script concatenation).&lt;/p&gt;

&lt;p&gt;A plain &lt;code&gt;const&lt;/code&gt; would emit code and can cause redeclaration errors. &lt;code&gt;export const&lt;/code&gt; creates a &lt;strong&gt;module-scoped&lt;/strong&gt; binding, not a global. Assigning to &lt;code&gt;globalThis&lt;/code&gt; is runtime-only and gives you no compile-time type safety or declaration merging.&lt;/p&gt;

&lt;p&gt;Ambient declarations are the standard pattern for global constants in declaration files and multi-entrypoint setups.&lt;/p&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/declaration-files/introduction.html" rel="noopener noreferrer"&gt;Ambient Declarations (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Impact of &lt;code&gt;as const&lt;/code&gt; on literal types
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Type System · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What is the type of &lt;code&gt;config&lt;/code&gt; after the following declaration?&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;config&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&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;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;as const&lt;/code&gt; converts mutable literal types into their narrowest possible readonly literal types — &lt;code&gt;apiUrl&lt;/code&gt; becomes the specific string literal &lt;code&gt;'https://api.example.com'&lt;/code&gt;, and &lt;code&gt;timeout&lt;/code&gt; becomes the numeric literal &lt;code&gt;5000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Result type:&lt;/strong&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;apiUrl&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;https://api.example.com&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;timeout&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;5000&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 enables exhaustive type checking and prevents unintended mutation. Without &lt;code&gt;as const&lt;/code&gt;, TypeScript would widen to &lt;code&gt;{ apiUrl: string; timeout: number }&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-4.html#const-assertions" rel="noopener noreferrer"&gt;Const Assertions&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Rest parameter in overloaded function
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Functions · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;Consider a function with two overloads: one accepting &lt;code&gt;(a: string)&lt;/code&gt; and another &lt;code&gt;(a: string, ...rest: number[])&lt;/code&gt;. What must be true about the implementation signature's rest parameter?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The implementation signature must be callable with &lt;em&gt;all&lt;/em&gt; overload call patterns. Since the first overload passes only one argument, &lt;code&gt;rest&lt;/code&gt; must be optional — but TypeScript requires rest parameters in implementations to match the &lt;em&gt;most permissive&lt;/em&gt; overload.&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;...rest: number[]&lt;/code&gt; is valid because it's omitted in calls matching the first overload (treated as &lt;code&gt;[]&lt;/code&gt;). The parameter &lt;code&gt;a&lt;/code&gt; remains required; &lt;code&gt;rest&lt;/code&gt; is inherently optional when omitted.&lt;/p&gt;

&lt;p&gt;Using &lt;code&gt;any[]&lt;/code&gt; or &lt;code&gt;unknown[]&lt;/code&gt; violates strict typing and isn't required.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Type predicates and user-defined type guards
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Type Guards · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What's the difference between these two functions, and why does only one act as a proper type guard?&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="nf"&gt;isString1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&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="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;typeof&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isString2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kr"&gt;string&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;typeof&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;string&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&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="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isString1&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&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;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;isString2&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toUpperCase&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt; &lt;span class="c1"&gt;// ✅ OK&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;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The key difference is the &lt;strong&gt;return type&lt;/strong&gt;: &lt;code&gt;boolean&lt;/code&gt; vs &lt;code&gt;val is string&lt;/code&gt; (type predicate).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;isString1&lt;/code&gt; returns &lt;code&gt;boolean&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Function works correctly at runtime&lt;/li&gt;
&lt;li&gt;But TypeScript doesn't narrow the type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;value&lt;/code&gt; remains &lt;code&gt;string | number&lt;/code&gt; after the check&lt;/li&gt;
&lt;li&gt;Can't call string methods without assertion&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;isString2&lt;/code&gt; returns &lt;code&gt;val is string&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is a &lt;strong&gt;type predicate&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Tells TypeScript: "if this returns true, then &lt;code&gt;val&lt;/code&gt; is definitely &lt;code&gt;string&lt;/code&gt;"&lt;/li&gt;
&lt;li&gt;TypeScript narrows &lt;code&gt;value&lt;/code&gt; to &lt;code&gt;string&lt;/code&gt; inside the if-block&lt;/li&gt;
&lt;li&gt;Full type safety enabled&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Rules for type predicates:&lt;/strong&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="c1"&gt;// ✅ Valid: parameter name matches&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;User&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;typeof&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;object&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&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="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// ❌ Invalid: parameter name doesn't match&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isUser&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;unknown&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// ❌ Invalid: return type must be assignable to parameter type&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isString&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;val&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;val&lt;/span&gt; &lt;span class="k"&gt;is&lt;/span&gt; &lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// ...&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Type predicates enable custom type guards&lt;/li&gt;
&lt;li&gt;Must use &lt;code&gt;parameterName is Type&lt;/code&gt; syntax&lt;/li&gt;
&lt;li&gt;Parameter name must match function parameter&lt;/li&gt;
&lt;li&gt;Essential for narrowing complex types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/2/narrowing.html#using-type-predicates" rel="noopener noreferrer"&gt;Using Type Predicates (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  5. Conditional types with infer keyword
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Generics · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What does this conditional type do, and how does the &lt;code&gt;infer&lt;/code&gt; keyword work?&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;UnpackPromise&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;infer&lt;/span&gt; &lt;span class="nx"&gt;U&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;U&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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&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;// string&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This conditional type &lt;strong&gt;extracts the resolved type from a Promise&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;T extends Promise&amp;lt;infer U&amp;gt;&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Checks if &lt;code&gt;T&lt;/code&gt; is assignable to &lt;code&gt;Promise&amp;lt;something&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;infer U&lt;/code&gt; tells TypeScript to &lt;strong&gt;infer&lt;/strong&gt; the type parameter and bind it to &lt;code&gt;U&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;? U : T&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;T&lt;/code&gt; is a Promise, return the inferred type &lt;code&gt;U&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Otherwise, return &lt;code&gt;T&lt;/code&gt; unchanged&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Examples:&lt;/strong&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;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&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;// string&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&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;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// { id: number }&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;C&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// number (not a Promise)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;More examples with &lt;code&gt;infer&lt;/code&gt;:&lt;/strong&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="c1"&gt;// Extract return type of a function&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nb"&gt;ReturnType&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;extends &lt;/span&gt;&lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&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;R&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;R&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="c1"&gt;// Extract element type of an array&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ElementType&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="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;extends &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;U&lt;/span&gt;&lt;span class="p"&gt;)[]&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;U&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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Num&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;ElementType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// number&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;infer&lt;/code&gt; can only be used in &lt;code&gt;extends&lt;/code&gt; clauses of conditional types&lt;/li&gt;
&lt;li&gt;It creates a type variable that TypeScript infers from the pattern&lt;/li&gt;
&lt;li&gt;Essential for advanced type-level programming&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/2/conditional-types.html" rel="noopener noreferrer"&gt;Conditional Types (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Never type and control flow analysis
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Type System · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What is the inferred return type of &lt;code&gt;fail()&lt;/code&gt; — and why does that matter for code after the call?&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="nf"&gt;fail&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="o"&gt;??&lt;/span&gt;&lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;fail()&lt;/code&gt; &lt;strong&gt;unconditionally throws&lt;/strong&gt; and has no reachable &lt;code&gt;return&lt;/code&gt;, TypeScript infers its return type as &lt;strong&gt;&lt;code&gt;never&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;That enables control flow analysis: anything after &lt;code&gt;fail()&lt;/code&gt; is treated as unreachable, so the compiler won't expect a return value on code paths that call it.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;void&lt;/code&gt; would mean the function could return &lt;code&gt;undefined&lt;/code&gt;, which is false here. &lt;code&gt;unknown&lt;/code&gt; and &lt;code&gt;Error&lt;/code&gt; are unrelated to the return type of a throwing function.&lt;/p&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/2/functions.html#never" rel="noopener noreferrer"&gt;The &lt;code&gt;never&lt;/code&gt; type (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  7. TypeScript's &lt;code&gt;--isolatedModules&lt;/code&gt; flag implication
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Modules · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What constraint does TypeScript's &lt;code&gt;--isolatedModules&lt;/code&gt; compiler option enforce on individual files?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;--isolatedModules&lt;/code&gt; requires each file to be independently valid as a module — meaning it cannot rely on global ambient context or non-module syntax that assumes shared scope (e.g., &lt;code&gt;declare module&lt;/code&gt; in non-module files). This is critical for tools like Babel or SWC that transpile files individually without full TS program analysis.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Key constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;All files must be modules (have &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;export&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Cannot use &lt;code&gt;const enum&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Cannot re-export types without &lt;code&gt;export type&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/tsconfig#isolatedModules" rel="noopener noreferrer"&gt;TypeScript Docs: isolatedModules&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  8. Mapped types with key remapping
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Generics · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What does this mapped type do, and how does the &lt;code&gt;as&lt;/code&gt; clause work?&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;Getters&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="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;T&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="s2"&gt;`get&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Capitalize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;]:&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;T&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;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Person&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&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;age&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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;PersonGetters&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;Getters&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Person&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;// { getName: () =&amp;gt; string; getAge: () =&amp;gt; number }&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This mapped type &lt;strong&gt;creates getter methods with transformed property names&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;[K in keyof T]&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Iterates over all keys of &lt;code&gt;T&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;K&lt;/code&gt; is each key in turn&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;as get${Capitalize&amp;lt;string &amp;amp; K&amp;gt;}&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Key remapping&lt;/strong&gt; (TypeScript 4.1+)&lt;/li&gt;
&lt;li&gt;Transforms each key name&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Capitalize&lt;/code&gt; is a built-in template literal type&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;string &amp;amp; K&lt;/code&gt; ensures &lt;code&gt;K&lt;/code&gt; is a string (not &lt;code&gt;number&lt;/code&gt; or &lt;code&gt;symbol&lt;/code&gt;)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;: () =&amp;gt; T[K]&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each property becomes a function returning the original type&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Step-by-step for &lt;code&gt;Person&lt;/code&gt;:&lt;/strong&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;A&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kr"&gt;string&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;// string&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;B&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;id&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;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// { id: number }&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;C&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;UnpackPromise&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&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="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// number (not a Promise)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Other key remapping examples:&lt;/strong&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="c1"&gt;// Remove optional modifier&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nb"&gt;Required&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="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;T&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;-&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;K&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="c1"&gt;// Filter keys by condition&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;RemoveReadonly&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="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;T&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="nx"&gt;K&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="s2"&gt;`readonly_&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="s2"&gt;`&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nx"&gt;never&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="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="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;&lt;strong&gt;Key points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Key remapping enables powerful type transformations&lt;/li&gt;
&lt;li&gt;Works with template literal types&lt;/li&gt;
&lt;li&gt;Can filter, rename, or compute new keys&lt;/li&gt;
&lt;li&gt;Essential for advanced utility types&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/2/mapped-types.html#key-remapping-via-as" rel="noopener noreferrer"&gt;Key Remapping in Mapped Types (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  9. Template literal types
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Type System · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What is the type of &lt;code&gt;event&lt;/code&gt; in this code, and how do template literal types work?&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;EventType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;focus&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;blur&lt;/span&gt;&lt;span class="dl"&gt;'&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;Handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s2"&gt;`on&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nb"&gt;Capitalize&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;EventType&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="k"&gt;void&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;handler&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Handler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;event&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;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The type of &lt;code&gt;event&lt;/code&gt; is &lt;strong&gt;&lt;code&gt;'onClick' | 'onFocus' | 'onBlur'&lt;/code&gt;&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How template literal types work:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;`on${Capitalize&amp;lt;EventType&amp;gt;}`&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Takes each member of the union &lt;code&gt;EventType&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Applies &lt;code&gt;Capitalize&lt;/code&gt; to each&lt;/li&gt;
&lt;li&gt;Concatenates with &lt;code&gt;'on'&lt;/code&gt; prefix&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Result:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;'click'&lt;/code&gt; → &lt;code&gt;Capitalize&amp;lt;'click'&amp;gt;&lt;/code&gt; → &lt;code&gt;'Click'&lt;/code&gt; → &lt;code&gt;'onClick'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'focus'&lt;/code&gt; → &lt;code&gt;'onFocus'&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;'blur'&lt;/code&gt; → &lt;code&gt;'onBlur'&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;More examples:&lt;/strong&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="c1"&gt;// CSS properties&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;CSSProperty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`margin&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Top&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Right&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Bottom&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Left&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// 'marginTop' | 'marginRight' | 'marginBottom' | 'marginLeft'&lt;/span&gt;

&lt;span class="c1"&gt;// Event handlers&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ReactEventHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`on&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Click&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Change&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;Submit&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;Capture`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// 'onClickCapture' | 'onChangeCapture' | 'onSubmitCapture'&lt;/span&gt;

&lt;span class="c1"&gt;// URL paths&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;APIRoute&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`/api/&lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;users&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;posts&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;comments&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="c1"&gt;// '/api/users' | '/api/posts' | '/api/comments'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Template literal types create string literal types from unions&lt;/li&gt;
&lt;li&gt;Work with built-in string manipulation types (&lt;code&gt;Capitalize&lt;/code&gt;, &lt;code&gt;Uppercase&lt;/code&gt;, etc.)&lt;/li&gt;
&lt;li&gt;Enable type-safe string patterns&lt;/li&gt;
&lt;li&gt;Essential for type-safe APIs and configuration&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/2/template-literal-types.html" rel="noopener noreferrer"&gt;Template Literal Types (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Recursive conditional types
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Generics · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What does this recursive type do, and why is the conditional check necessary?&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;DeepReadonly&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&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;T&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;K&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="kd"&gt;extends&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;DeepReadonly&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="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;T&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;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Config&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;url&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;timeout&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;features&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;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;ReadonlyConfig&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;DeepReadonly&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Config&lt;/span&gt;&lt;span class="o"&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;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This recursive type &lt;strong&gt;makes all properties deeply readonly&lt;/strong&gt; — including nested objects and arrays.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;readonly [K in keyof T]&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Makes each property readonly&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;T[K] extends object ? DeepReadonly&amp;lt;T[K]&amp;gt; : T[K]&lt;/code&gt;:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;If the property value is an object, recursively apply &lt;code&gt;DeepReadonly&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Otherwise, keep the primitive type as-is&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Result for &lt;code&gt;Config&lt;/code&gt;:&lt;/strong&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="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;api&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;url&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="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;timeout&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="k"&gt;readonly&lt;/span&gt; &lt;span class="nx"&gt;features&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;readonly&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;&lt;strong&gt;Why the conditional check is necessary:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without &lt;code&gt;extends object&lt;/code&gt;, you'd try to recurse into primitives:&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;// ❌ Bad: tries to make string readonly&lt;/span&gt;
&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;BadDeepReadonly&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="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;readonly&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;T&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt; &lt;span class="nx"&gt;DeepReadonly&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="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="c1"&gt;// Would try: DeepReadonly&amp;lt;string&amp;gt; → { readonly [K in keyof string]: ... }&lt;/span&gt;
&lt;span class="c1"&gt;// This creates infinite recursion or weird results&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Limitations:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Doesn't handle &lt;code&gt;Date&lt;/code&gt;, &lt;code&gt;RegExp&lt;/code&gt;, &lt;code&gt;Map&lt;/code&gt;, &lt;code&gt;Set&lt;/code&gt; correctly&lt;/li&gt;
&lt;li&gt;Can hit recursion limits with very deep structures&lt;/li&gt;
&lt;li&gt;Arrays become &lt;code&gt;readonly T[]&lt;/code&gt; (which is correct)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/2/conditional-types.html" rel="noopener noreferrer"&gt;Recursive Types (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  11. Declaration merging with interfaces
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Interfaces and Types · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What happens when you declare the same interface multiple times, and why doesn't this work with type aliases?&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;Box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&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;interface&lt;/span&gt; &lt;span class="nx"&gt;Box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;box&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Box&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;width&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="na"&gt;height&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="c1"&gt;// ✅ OK&lt;/span&gt;

&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;volume&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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;weight&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="c1"&gt;// ❌ Error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This is &lt;strong&gt;declaration merging&lt;/strong&gt; — a feature unique to interfaces in TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;How it works:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;When you declare the same interface multiple times, TypeScript &lt;strong&gt;merges&lt;/strong&gt; all declarations into a single interface:&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;// These two declarations:&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&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;interface&lt;/span&gt; &lt;span class="nx"&gt;Box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;height&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="c1"&gt;// Merge into:&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Box&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;width&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;height&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What can be merged:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Properties (must have unique names or compatible types)&lt;/li&gt;
&lt;li&gt;Methods (create overloads)&lt;/li&gt;
&lt;li&gt;Nested interfaces&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example with methods:&lt;/strong&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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;level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;info&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&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="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Merged result:&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Logger&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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;level&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;info&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;error&lt;/span&gt;&lt;span class="dl"&gt;'&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Why type aliases don't merge:&lt;/strong&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;Container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;volume&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="kd"&gt;type&lt;/span&gt; &lt;span class="nx"&gt;Container&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;weight&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="c1"&gt;// ❌ Duplicate identifier&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Type aliases create a &lt;strong&gt;single, immutable binding&lt;/strong&gt;. You can't redeclare them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When declaration merging is useful:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Extending third-party interfaces (e.g., adding properties to &lt;code&gt;Window&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Module augmentation&lt;/li&gt;
&lt;li&gt;Progressive interface building&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example — extending &lt;code&gt;Window&lt;/code&gt;:&lt;/strong&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Window&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;myCustomProperty&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;span class="c1"&gt;// Now window.myCustomProperty is typed&lt;/span&gt;
&lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;myCustomProperty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;hello&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;&lt;strong&gt;Key points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Only interfaces support declaration merging&lt;/li&gt;
&lt;li&gt;Type aliases cannot be merged&lt;/li&gt;
&lt;li&gt;Useful for extending existing types&lt;/li&gt;
&lt;li&gt;Common in library type definitions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/declaration-merging.html" rel="noopener noreferrer"&gt;Declaration Merging (TypeScript Handbook)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  12. Variance annotations in TypeScript 4.7+
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Topic:&lt;/strong&gt; Generics · &lt;strong&gt;Difficulty:&lt;/strong&gt; ⭐⭐⭐ Hard&lt;/p&gt;

&lt;p&gt;What do the &lt;code&gt;in&lt;/code&gt; and &lt;code&gt;out&lt;/code&gt; modifiers do in this interface, and why are they needed?&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;Producer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;out&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="nf"&gt;get&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="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Consumer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;in&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="nf"&gt;set&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;value&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="k"&gt;void&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Processor&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;out&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="nf"&gt;process&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="na"&gt;value&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;T&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;&lt;strong&gt;Answer:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;These are &lt;strong&gt;variance annotations&lt;/strong&gt; (TypeScript 4.7+) that control how generic types relate to each other in subtyping.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Variance explained:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;out T&lt;/code&gt; (covariant):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;T&lt;/code&gt; only appears in output positions (return types)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Producer&amp;lt;Dog&amp;gt;&lt;/code&gt; is assignable to &lt;code&gt;Producer&amp;lt;Animal&amp;gt;&lt;/code&gt; if &lt;code&gt;Dog extends Animal&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Safe because you only get &lt;code&gt;T&lt;/code&gt; out&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;in T&lt;/code&gt; (contravariant):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;T&lt;/code&gt; only appears in input positions (parameter types)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Consumer&amp;lt;Animal&amp;gt;&lt;/code&gt; is assignable to &lt;code&gt;Consumer&amp;lt;Dog&amp;gt;&lt;/code&gt; if &lt;code&gt;Dog extends Animal&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Safe because you only put &lt;code&gt;T&lt;/code&gt; in&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;in out T&lt;/code&gt; (invariant):&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;T&lt;/code&gt; appears in both input and output&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Processor&amp;lt;Dog&amp;gt;&lt;/code&gt; is NOT assignable to &lt;code&gt;Processor&amp;lt;Animal&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Neither direction is safe&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Why they're needed:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Without annotations, TypeScript uses structural typing and might allow unsafe assignments:&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;// Without variance annotations&lt;/span&gt;
&lt;span class="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;Box&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="na"&gt;value&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;animalBox&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Animal&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="na"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Animal&lt;/span&gt;&lt;span class="p"&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;dogBox&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;Box&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Dog&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;animalBox&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ Unsafe but allowed!&lt;/span&gt;
&lt;span class="nx"&gt;dogBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;bark&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 💥 Runtime error if value is actually Animal&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;With variance annotations:&lt;/strong&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="kr"&gt;interface&lt;/span&gt; &lt;span class="nx"&gt;ReadOnlyBox&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;out&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="nf"&gt;getValue&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="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;animalBox&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadOnlyBox&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Animal&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;getAnimalBox&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;dogBox&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ReadOnlyBox&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Dog&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;animalBox&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ✅ Safe — covariant&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Key points:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;out&lt;/code&gt; = covariant (output only)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;in&lt;/code&gt; = contravariant (input only)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;in out&lt;/code&gt; = invariant (both)&lt;/li&gt;
&lt;li&gt;Catches unsafe type relationships at compile time&lt;/li&gt;
&lt;li&gt;Essential for library authors&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;📖 &lt;a href="https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#variance-annotations-for-type-parameters" rel="noopener noreferrer"&gt;Variance Annotations (TypeScript Release Notes)&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  🎯 Ready to test yourself?
&lt;/h2&gt;

&lt;p&gt;Reading answers is not the same as retrieving them under pressure.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Want to test your knowledge interactively?&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;📚 &lt;strong&gt;&lt;a href="https://github.com/Skillhacker-io/typescript-interview-questions" rel="noopener noreferrer"&gt;Full TypeScript Question Bank on GitHub&lt;/a&gt;&lt;/strong&gt; — 30+ questions with detailed explanations, perfect for offline study&lt;/li&gt;
&lt;li&gt;🚀 &lt;strong&gt;&lt;a href="https://skillhacker.io" rel="noopener noreferrer"&gt;Take the Interactive TypeScript Assessment&lt;/a&gt;&lt;/strong&gt; — timed mode, instant feedback, compare your results with other developers&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>typescript</category>
      <category>javascript</category>
      <category>webdev</category>
      <category>codenewbie</category>
    </item>
    <item>
      <title>6 Advanced JavaScript Questions That Separate Seniors from Mid-Levels</title>
      <dc:creator>Vitaly Obolensky</dc:creator>
      <pubDate>Sat, 30 May 2026 18:42:14 +0000</pubDate>
      <link>https://dev.to/vitalyobolensky/6-advanced-javascript-questions-that-separate-seniors-from-mid-levels-6cm</link>
      <guid>https://dev.to/vitalyobolensky/6-advanced-javascript-questions-that-separate-seniors-from-mid-levels-6cm</guid>
      <description>&lt;h2&gt;
  
  
  1. Stale closure &amp;amp; primitive capture
&lt;/h2&gt;

&lt;p&gt;What is the output of the following code?&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;function&lt;/span&gt; &lt;span class="nf"&gt;createIncrement&lt;/span&gt;&lt;span class="p"&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;count&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&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;message&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s2"&gt;`Count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;count&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;message&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;log&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="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;createIncrement&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;increment&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Test your understanding of closures, lexical scope, and primitive value capture.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  ✅ Output
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Count is 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  🧠 Explanation
&lt;/h3&gt;

&lt;p&gt;This is a classic stale closure trap — but not in the way most developers expect.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step-by-step execution:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;createIncrement()&lt;/code&gt; is invoked → new lexical environment created:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;count = 0&lt;/code&gt; (mutable binding)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;message = "Count is 0"&lt;/code&gt; (primitive string, interpolated immediately at assignment)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;Inner functions &lt;code&gt;increment&lt;/code&gt; and &lt;code&gt;log&lt;/code&gt; are defined. Both close over the same lexical environment.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;increment()&lt;/code&gt; is called twice:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;count&lt;/code&gt; mutates: &lt;code&gt;0 → 1 → 2&lt;/code&gt; ✓&lt;/li&gt;
&lt;li&gt;This works as expected.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;log()&lt;/code&gt; is called:

&lt;ul&gt;
&lt;li&gt;It references the variable &lt;code&gt;message&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;message&lt;/code&gt; still holds the original string value &lt;code&gt;"Count is 0"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;The template literal was evaluated once, at the moment of assignment — not re-evaluated when &lt;code&gt;log()&lt;/code&gt; runs.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;
&lt;h3&gt;
  
  
  🔑 Core Concept
&lt;/h3&gt;

&lt;p&gt;&amp;gt; Closures capture &lt;strong&gt;variables&lt;/strong&gt;, not &lt;strong&gt;expressions&lt;/strong&gt;.&lt;br&gt;
&amp;gt; But if a variable holds a primitive value (string, number, boolean), that value is fixed at assignment time.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;message&lt;/code&gt; is not a live reference to &lt;code&gt;count&lt;/code&gt;. It is a &lt;strong&gt;snapshot&lt;/strong&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  🛠 How to fix it (if dynamic output is desired)
&lt;/h3&gt;

&lt;p&gt;Re-evaluate the template literal inside &lt;code&gt;log()&lt;/code&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;function&lt;/span&gt; &lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Count is &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;count&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;h3&gt;
  
  
  🎯 What this question tests
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Template literal evaluation timing&lt;/td&gt;
&lt;td&gt;They run at assignment, not at access&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Primitive vs reference types&lt;/td&gt;
&lt;td&gt;Primitives are copied by value; objects/arrays are referenced&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Closure capture semantics&lt;/td&gt;
&lt;td&gt;Closures close over bindings, but the value of a primitive is immutable once assigned&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mental model of "live" variables&lt;/td&gt;
&lt;td&gt;Not all variables in a closure are "live views" — only the bindings themselves are&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  2. JavaScript context trap: losing &lt;code&gt;this&lt;/code&gt;
&lt;/h2&gt;

&lt;p&gt;What will this code output when &lt;code&gt;user.greet()&lt;/code&gt; is called?&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;user&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="s2"&gt;Alex&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Hello, &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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&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;innerNormal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Normal: &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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&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;innerArrow&lt;/span&gt; &lt;span class="o"&gt;=&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Arrow: &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;name&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;`&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="p"&gt;};&lt;/span&gt;

    &lt;span class="nf"&gt;innerNormal&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="nf"&gt;innerArrow&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="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;greet&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Test your understanding of execution context, function invocation patterns, and arrow function lexical binding.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  ✅ Output
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Hello, Alex!
Normal: undefined
Arrow: Alex
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  🧠 Explanation
&lt;/h3&gt;

&lt;p&gt;This question tests three different ways &lt;code&gt;this&lt;/code&gt; is resolved in JavaScript:&lt;/p&gt;
&lt;h4&gt;
  
  
  1. Method call: &lt;code&gt;greet()&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Called as &lt;code&gt;user.greet()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;this&lt;/code&gt; is bound to the object preceding the dot → &lt;code&gt;user&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Output: &lt;code&gt;Hello, Alex!&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  2. Regular function: &lt;code&gt;innerNormal()&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Called as a standalone function: &lt;code&gt;innerNormal()&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;No object precedes the call. In strict mode (default in modern JS/modules), &lt;code&gt;this&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt;. In non-strict browser environments, it falls back to &lt;code&gt;window&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;undefined.name&lt;/code&gt; evaluates to &lt;code&gt;undefined&lt;/code&gt; (or &lt;code&gt;window.name&lt;/code&gt; which is typically &lt;code&gt;""&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;Output: &lt;code&gt;Normal: undefined&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  3. Arrow function: &lt;code&gt;innerArrow()&lt;/code&gt;
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Arrow functions &lt;strong&gt;do not have their own &lt;code&gt;this&lt;/code&gt; binding&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;They capture &lt;code&gt;this&lt;/code&gt; lexically from the enclosing execution context at definition time&lt;/li&gt;
&lt;li&gt;The enclosing context is &lt;code&gt;greet()&lt;/code&gt;, where &lt;code&gt;this === user&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Output: &lt;code&gt;Arrow: Alex&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  🔑 Core Concept
&lt;/h3&gt;

&lt;p&gt;&amp;gt; Regular functions resolve &lt;code&gt;this&lt;/code&gt; &lt;strong&gt;at call time&lt;/strong&gt; (dynamic binding).&lt;br&gt;
&amp;gt; Arrow functions capture &lt;code&gt;this&lt;/code&gt; &lt;strong&gt;at definition time&lt;/strong&gt; (lexical binding).&lt;/p&gt;
&lt;h3&gt;
  
  
  🛠 How to fix &lt;code&gt;innerNormal&lt;/code&gt; if you want it to see &lt;code&gt;user&lt;/code&gt;
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Option 1: Explicit binding at call time&lt;/span&gt;
&lt;span class="nx"&gt;innerNormal&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;call&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="c1"&gt;// Option 2: Explicit binding at definition time&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;innerNormal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&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;name&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;bind&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="c1"&gt;// Option 3: Lexical capture via closure (older pattern)&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nb"&gt;self&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&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;innerNormal&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;function &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;name&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;h3&gt;
  
  
  🎯 What this question tests
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Implicit &lt;code&gt;this&lt;/code&gt; binding&lt;/td&gt;
&lt;td&gt;Regular functions lose context when called without an explicit receiver&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Lexical &lt;code&gt;this&lt;/code&gt; capture&lt;/td&gt;
&lt;td&gt;Arrow functions inherit &lt;code&gt;this&lt;/code&gt; from their parent scope&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Strict vs non-strict mode&lt;/td&gt;
&lt;td&gt;Changes fallback behavior (&lt;code&gt;undefined&lt;/code&gt; vs global object)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Mental model of execution context&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;this&lt;/code&gt; is dynamic for regular functions, static for arrows&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  3. Illusion of an "own" property on mutation
&lt;/h2&gt;

&lt;p&gt;What will the code log to the console at the end?&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;grandparent&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;heritage&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="s2"&gt;gold&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;land&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
  &lt;span class="na"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="mi"&gt;100&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;parent&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="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grandparent&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;child&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="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heritage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;debts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grandparent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (1) ?&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;grandparent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heritage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (2) ?&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (3) ?&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heritage&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (4) ?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Test your understanding of the difference between reading a property from the prototype chain and writing a property on an object.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  ✅ Output
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"gold"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"land"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"debts"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="mi"&gt;150&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"gold"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"land"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"debts"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  🧠 Explanation
&lt;/h3&gt;

&lt;p&gt;This is the mental trap:&lt;/p&gt;
&lt;h4&gt;
  
  
  For &lt;code&gt;coins&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;The expression &lt;code&gt;child.coins += 50&lt;/code&gt; desugars to &lt;code&gt;child.coins = child.coins + 50&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JavaScript &lt;strong&gt;reads&lt;/strong&gt; &lt;code&gt;child.coins&lt;/code&gt;. It is not found on &lt;code&gt;child&lt;/code&gt;, so the engine walks the prototype chain to &lt;code&gt;grandparent&lt;/code&gt; and reads &lt;code&gt;100&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It adds &lt;code&gt;50&lt;/code&gt; → &lt;code&gt;150&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;It &lt;strong&gt;writes&lt;/strong&gt; the result to &lt;code&gt;child.coins&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Writes always land on the object that received the assignment. &lt;code&gt;child&lt;/code&gt; gets its own &lt;code&gt;coins: 150&lt;/code&gt;, while &lt;code&gt;grandparent.coins&lt;/code&gt; stays &lt;code&gt;100&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;
  
  
  For &lt;code&gt;heritage&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;The expression &lt;code&gt;child.heritage.push("debts")&lt;/code&gt; does &lt;strong&gt;not&lt;/strong&gt; assign to &lt;code&gt;heritage&lt;/code&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;JavaScript &lt;strong&gt;reads&lt;/strong&gt; &lt;code&gt;child.heritage&lt;/code&gt;, finds the array on &lt;code&gt;grandparent&lt;/code&gt;, and keeps a reference to that same array in the heap.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.push("debts")&lt;/code&gt; &lt;strong&gt;mutates&lt;/strong&gt; that shared array.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;code&gt;child&lt;/code&gt; never gets an own &lt;code&gt;heritage&lt;/code&gt; property — it still resolves to the grandparent's array, which now includes &lt;code&gt;"debts"&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  🔑 Core Concept
&lt;/h3&gt;

&lt;p&gt;&amp;gt; &lt;strong&gt;Read&lt;/strong&gt; vs &lt;strong&gt;write&lt;/strong&gt; behave differently on the prototype chain.&lt;br&gt;
&amp;gt; Assignment creates (or updates) an &lt;strong&gt;own&lt;/strong&gt; property on the target object.&lt;br&gt;
&amp;gt; Mutating a referenced object affects the &lt;strong&gt;shared&lt;/strong&gt; value visible to every object in the chain that points to it.&lt;/p&gt;
&lt;h3&gt;
  
  
  🛠 How to avoid accidental prototype mutation
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Option 1: Assign a new array instead of mutating the inherited one&lt;/span&gt;
&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heritage&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heritage&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;debts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

&lt;span class="c1"&gt;// Option 2: Copy mutable values when creating the child&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;child&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="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;parent&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nb"&gt;Object&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;assign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;grandparent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coins&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="na"&gt;heritage&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[...&lt;/span&gt;&lt;span class="nx"&gt;grandparent&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heritage&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;coins&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="mi"&gt;50&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="nx"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;heritage&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;debts&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// now only child's copy is affected&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  🎯 What this question tests
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Prototype property lookup&lt;/td&gt;
&lt;td&gt;Reads fall through the chain until a property is found&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assignment vs mutation&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;+=&lt;/code&gt; writes locally; &lt;code&gt;.push()&lt;/code&gt; mutates a shared reference&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Own vs inherited properties&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;hasOwnProperty&lt;/code&gt; and debugging surprises depend on this distinction&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Reference types on prototypes&lt;/td&gt;
&lt;td&gt;Shared mutable state on a prototype affects all descendants&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  4. JavaScript coercion &amp;amp; precedence trap
&lt;/h2&gt;

&lt;p&gt;What will this code output?&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;+&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="o"&gt;+&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;0&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;blockquote&gt;
&lt;p&gt;Test your understanding of operator precedence, implicit type conversion, and left-to-right evaluation.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  ✅ Output
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"51false"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  🧠 Explanation
&lt;/h3&gt;

&lt;p&gt;This expression combines unary operators, object-to-primitive coercion, and left-to-right associativity. Here's the exact execution flow:&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 1: Unary operators (highest precedence)
&lt;/h4&gt;

&lt;p&gt;Unary &lt;code&gt;+&lt;/code&gt; and &lt;code&gt;!&lt;/code&gt; are evaluated before binary &lt;code&gt;+&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;+"5"&lt;/code&gt; → &lt;code&gt;ToNumber&lt;/code&gt; conversion → &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;!"0"&lt;/code&gt; → &lt;code&gt;"0"&lt;/code&gt; is truthy → &lt;code&gt;!true&lt;/code&gt; → &lt;code&gt;false&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Expression becomes: &lt;code&gt;5 + [1] + false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 2: Left-to-right evaluation &amp;amp; first &lt;code&gt;+&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;Binary &lt;code&gt;+&lt;/code&gt; is left-associative. It evaluates &lt;code&gt;5 + [1]&lt;/code&gt; first.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;One operand is a &lt;code&gt;Number&lt;/code&gt;, the other is an &lt;code&gt;Object&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;JS invokes &lt;code&gt;ToPrimitive([1])&lt;/code&gt; → calls &lt;code&gt;Array.toString()&lt;/code&gt; → &lt;code&gt;"1"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Since one operand is now a string, &lt;code&gt;+&lt;/code&gt; switches to &lt;strong&gt;string concatenation&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;"5" + "1"&lt;/code&gt; → &lt;code&gt;"51"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Expression becomes: &lt;code&gt;"51" + false&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 3: Second &lt;code&gt;+&lt;/code&gt; &amp;amp; final coercion
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;"51"&lt;/code&gt; (string) + &lt;code&gt;false&lt;/code&gt; (boolean)&lt;/li&gt;
&lt;li&gt;Boolean &lt;code&gt;false&lt;/code&gt; is coerced to string → &lt;code&gt;"false"&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Concatenation: &lt;code&gt;"51" + "false"&lt;/code&gt; → &lt;code&gt;"51false"&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  🔑 Core Concept
&lt;/h3&gt;

&lt;p&gt;&amp;gt; &lt;strong&gt;Precedence&lt;/strong&gt; decides what runs first.&lt;br&gt;
&amp;gt; &lt;strong&gt;Associativity&lt;/strong&gt; decides direction (left → right for &lt;code&gt;+&lt;/code&gt;).&lt;br&gt;
&amp;gt; &lt;strong&gt;Coercion&lt;/strong&gt; decides how types interact when mismatched.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;+&lt;/code&gt; operator is unique in JS: it performs both addition and concatenation. If &lt;strong&gt;either&lt;/strong&gt; operand becomes a string during &lt;code&gt;ToPrimitive&lt;/code&gt;, the entire operation switches to string concatenation.&lt;/p&gt;
&lt;h3&gt;
  
  
  🛠 Common pitfalls to avoid
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Mistake&lt;/th&gt;
&lt;th&gt;Why it's wrong&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Thinking &lt;code&gt;5 + [1]&lt;/code&gt; equals &lt;code&gt;6&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;[1]&lt;/code&gt; is not a number; it's coerced to &lt;code&gt;"1"&lt;/code&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Thinking &lt;code&gt;!"0"&lt;/code&gt; equals &lt;code&gt;true&lt;/code&gt;
&lt;/td&gt;
&lt;td&gt;Only &lt;code&gt;""&lt;/code&gt;, &lt;code&gt;0&lt;/code&gt;, &lt;code&gt;null&lt;/code&gt;, &lt;code&gt;undefined&lt;/code&gt;, &lt;code&gt;NaN&lt;/code&gt; are falsy. &lt;code&gt;"0"&lt;/code&gt; is a non-empty string → truthy&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Assuming right-to-left evaluation&lt;/td&gt;
&lt;td&gt;
&lt;code&gt;+&lt;/code&gt; is strictly left-associative in JS&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;
&lt;h3&gt;
  
  
  🎯 What this question tests
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Operator precedence table&lt;/td&gt;
&lt;td&gt;Determines evaluation order before execution begins&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;
&lt;code&gt;ToPrimitive&lt;/code&gt; &amp;amp; &lt;code&gt;ToString&lt;/code&gt; algorithms&lt;/td&gt;
&lt;td&gt;How objects/arrays convert in mixed-type expressions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Left-to-right associativity&lt;/td&gt;
&lt;td&gt;Critical for chaining &lt;code&gt;+&lt;/code&gt; operations with side effects or type shifts&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Falsy/truthy rules&lt;/td&gt;
&lt;td&gt;Essential for &lt;code&gt;!&lt;/code&gt;, &lt;code&gt;&amp;amp;amp;&amp;amp;amp;&lt;/code&gt;, `&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  5. JavaScript event loop &amp;amp; queue priority trap
&lt;/h2&gt;

&lt;p&gt;In what order will the numbers be printed to the console?&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="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="nf"&gt;setTimeout&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;().&lt;/span&gt;&lt;span class="nf"&gt;then&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3&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="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="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nf"&gt;then&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="nf"&gt;setTimeout&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;6&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="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;7&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;blockquote&gt;
&lt;p&gt;Test your understanding of the call stack, microtask queue, and macrotask queue execution order.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h3&gt;
  
  
  ✅ Output
&lt;/h3&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
4
7
5
2
3
6
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  🧠 Explanation
&lt;/h3&gt;

&lt;p&gt;This question tests the exact execution order defined by the JavaScript event loop.&lt;/p&gt;
&lt;h4&gt;
  
  
  Step 1: Synchronous execution (call stack)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;console.log("1")&lt;/code&gt; runs immediately → outputs &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;setTimeout&lt;/code&gt; schedules its callback as a &lt;strong&gt;macrotask&lt;/strong&gt; and exits&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;new Promise&lt;/code&gt; executor runs &lt;strong&gt;synchronously&lt;/strong&gt; → &lt;code&gt;console.log("4")&lt;/code&gt; outputs &lt;code&gt;4&lt;/code&gt;. &lt;code&gt;resolve()&lt;/code&gt; is called&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;.then()&lt;/code&gt; schedules its callback as a &lt;strong&gt;microtask&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("7")&lt;/code&gt; runs immediately → outputs &lt;code&gt;7&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Call stack is now empty&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 2: Microtask queue (priority over macrotasks)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;The event loop checks the microtask queue before picking the next macrotask&lt;/li&gt;
&lt;li&gt;Microtask 1 runs: &lt;code&gt;console.log("5")&lt;/code&gt; → outputs &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Inside it, &lt;code&gt;setTimeout&lt;/code&gt; schedules a new callback as &lt;strong&gt;macrotask 2&lt;/strong&gt; (appended to the macrotask queue)&lt;/li&gt;
&lt;li&gt;Microtask queue is now empty&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 3: Macrotask queue (first cycle)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Event loop picks &lt;strong&gt;macrotask 1&lt;/strong&gt; (the first &lt;code&gt;setTimeout&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("2")&lt;/code&gt; → outputs &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.resolve().then(...)&lt;/code&gt; schedules &lt;strong&gt;microtask 2&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 4: Microtask queue (again)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Before moving to the next macrotask, the event loop &lt;strong&gt;must completely drain&lt;/strong&gt; the microtask queue&lt;/li&gt;
&lt;li&gt;Microtask 2 runs: &lt;code&gt;console.log("3")&lt;/code&gt; → outputs &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Microtask queue is empty&lt;/li&gt;
&lt;/ul&gt;
&lt;h4&gt;
  
  
  Step 5: Macrotask queue (second cycle)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Event loop picks &lt;strong&gt;macrotask 2&lt;/strong&gt; (the second &lt;code&gt;setTimeout&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("6")&lt;/code&gt; → outputs &lt;code&gt;6&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;
&lt;h3&gt;
  
  
  🔑 Core Concept
&lt;/h3&gt;

&lt;p&gt;&amp;gt; &lt;strong&gt;Execution order:&lt;/strong&gt; synchronous code → microtasks (&lt;code&gt;Promise&lt;/code&gt;, &lt;code&gt;queueMicrotask&lt;/code&gt;) → macrotasks (&lt;code&gt;setTimeout&lt;/code&gt;, &lt;code&gt;setInterval&lt;/code&gt;, I/O) → repeat.&lt;br&gt;
&amp;gt;&lt;br&gt;
&amp;gt; Every time a macrotask finishes, the event loop &lt;strong&gt;must completely drain the microtask queue&lt;/strong&gt; before proceeding to the next macrotask. Nested microtasks created during a macrotask will delay the next macrotask.&lt;/p&gt;
&lt;h3&gt;
  
  
  🎯 What this question tests
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Concept&lt;/th&gt;
&lt;th&gt;Why it matters&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Promise executor timing&lt;/td&gt;
&lt;td&gt;Runs synchronously during construction, not asynchronously&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Microtask vs macrotask priority&lt;/td&gt;
&lt;td&gt;Microtasks always clear before the next macrotask runs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Nested async scheduling&lt;/td&gt;
&lt;td&gt;New tasks are appended to the back of their respective queues&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Event loop phases&lt;/td&gt;
&lt;td&gt;Critical for debugging race conditions, UI freezes, and API batching&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h2&gt;
  
  
  6. Microtask order: &lt;code&gt;async/await&lt;/code&gt; vs promise chains
&lt;/h2&gt;

&lt;p&gt;In what exact order will the logs be printed to the console?&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="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;asyncFunc&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;3&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nf"&gt;asyncFunc&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="nb"&gt;Promise&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;4&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="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;5&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;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;6&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;blockquote&gt;
&lt;p&gt;Test your understanding of how &lt;code&gt;await&lt;/code&gt; is compiled under the hood and its exact scheduling priority compared to &lt;code&gt;.then()&lt;/code&gt;.&lt;/p&gt;
&lt;/blockquote&gt;



&lt;h3&gt;
  
  
  ✅ Output
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
2
6
3
4
5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  🧠 Explanation
&lt;/h3&gt;

&lt;p&gt;This question reveals exactly how &lt;code&gt;async/await&lt;/code&gt; is translated into promise chains and how the event loop schedules continuations.&lt;/p&gt;

&lt;h4&gt;
  
  
  Step 1: Synchronous phase
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;console.log("1")&lt;/code&gt; executes → outputs &lt;code&gt;1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;asyncFunc()&lt;/code&gt; is invoked. Code runs synchronously until the first &lt;code&gt;await&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("2")&lt;/code&gt; executes → outputs &lt;code&gt;2&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;await Promise.resolve()&lt;/code&gt; is encountered. The expression on the right evaluates to an already-resolved promise. The engine wraps the remaining function body (&lt;code&gt;console.log("3")&lt;/code&gt;) in a &lt;strong&gt;microtask&lt;/strong&gt; and suspends execution. This microtask is queued&lt;/li&gt;
&lt;li&gt;Control returns to the global scope&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Promise.resolve().then(...)&lt;/code&gt; registers a callback. This creates &lt;strong&gt;microtask 2&lt;/strong&gt; (logs &lt;code&gt;4&lt;/code&gt;). The chained &lt;code&gt;.then&lt;/code&gt; (logs &lt;code&gt;5&lt;/code&gt;) is &lt;em&gt;not&lt;/em&gt; queued yet; it waits for microtask 2 to resolve&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;console.log("6")&lt;/code&gt; executes → outputs &lt;code&gt;6&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Call stack is empty. Event loop switches to the microtask queue&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Step 2: Microtask queue processing (FIFO order)
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Microtask 1&lt;/strong&gt; (from &lt;code&gt;await&lt;/code&gt; continuation): runs &lt;code&gt;console.log("3")&lt;/code&gt; → outputs &lt;code&gt;3&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microtask 2&lt;/strong&gt; (from first &lt;code&gt;.then&lt;/code&gt;): runs &lt;code&gt;console.log("4")&lt;/code&gt; → outputs &lt;code&gt;4&lt;/code&gt;. Its resolution immediately queues the next &lt;code&gt;.then&lt;/code&gt; callback as &lt;strong&gt;microtask 3&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Microtask 3&lt;/strong&gt; (from chained &lt;code&gt;.then&lt;/code&gt;): runs &lt;code&gt;console.log("5")&lt;/code&gt; → outputs &lt;code&gt;5&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Microtask queue is empty&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  🔑 Core Concept
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;await&lt;/code&gt; is syntactic sugar for &lt;code&gt;.then()&lt;/code&gt;.&lt;br&gt;
The code after &lt;code&gt;await&lt;/code&gt; is compiled into a &lt;code&gt;.then()&lt;/code&gt; callback and scheduled as a &lt;strong&gt;microtask&lt;/strong&gt;.&lt;br&gt;
Microtasks are processed in strict FIFO order based on when they were registered during synchronous execution.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Want more? I’m maintaining a curated repository with tricky JS questions and edge cases. Check the full list here: [&lt;a href="https://github.com/Skillhacker-io/javascript-interview-questions/blob/main/questions/top-javascript-questions.md" rel="noopener noreferrer"&gt;https://github.com/Skillhacker-io/javascript-interview-questions/blob/main/questions/top-javascript-questions.md&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>interview</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How to Actually Measure Your Programming Level (Without Tutorial Hell)</title>
      <dc:creator>Vitaly Obolensky</dc:creator>
      <pubDate>Sat, 18 Apr 2026 11:00:13 +0000</pubDate>
      <link>https://dev.to/vitalyobolensky/how-to-actually-measure-your-programming-level-without-tutorial-hell-45e2</link>
      <guid>https://dev.to/vitalyobolensky/how-to-actually-measure-your-programming-level-without-tutorial-hell-45e2</guid>
      <description>&lt;p&gt;We all know the feeling: you watch a course, build a small project, and still aren't sure if you're "ready" for a junior role or a real codebase. &lt;/p&gt;

&lt;p&gt;Imposter syndrome isn't always about skill. Often, it's about &lt;strong&gt;lack of measurable feedback&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Let's talk about why traditional learning leaves us guessing, and how structured testing + peer benchmarking can change that.&lt;/p&gt;

&lt;h2&gt;
  
  
  📉 Why "I know it" isn't the same as "I can prove it"**
&lt;/h2&gt;

&lt;p&gt;Passive learning (tutorials, docs, videos) creates an illusion of competence. You recognize the syntax, so your brain says "got it". But recognition ≠ recall.&lt;/p&gt;

&lt;p&gt;Cognitive science calls this the &lt;strong&gt;fluency illusion&lt;/strong&gt;. The fix? Active recall + spaced repetition. In programming, that means:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Answering targeted questions under mild time pressure&lt;/li&gt;
&lt;li&gt;Explaining &lt;em&gt;why&lt;/em&gt; the wrong options are wrong&lt;/li&gt;
&lt;li&gt;Tracking progress over weeks, not hours&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🧩 Why multiple-choice (4 options) isn't "just guessing"
&lt;/h2&gt;

&lt;p&gt;Many devs dismiss MCQs as "quiz trash". But in skill assessment, they're a powerful tool when designed right:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Distractors matter&lt;/strong&gt; – good wrong answers expose specific misconceptions (e.g., confusing &lt;code&gt;let&lt;/code&gt; vs &lt;code&gt;var&lt;/code&gt;, or sync vs async behavior).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Speed + accuracy = real-world proxy&lt;/strong&gt; – interviews and debugging both reward quick pattern recognition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Benchmarking&lt;/strong&gt; – comparing your score to the community average removes ego and shows where you actually stand.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It's not about memorizing answers. It's about stress-testing your mental models.&lt;/p&gt;

&lt;h2&gt;
  
  
  📊 The missing piece: peer comparison
&lt;/h2&gt;

&lt;p&gt;Studying alone keeps you in a bubble. You might score 8/10 and think "I'm solid", until you see the average is 9.4 and the top 10% finish in half the time.&lt;/p&gt;

&lt;p&gt;Healthy benchmarking:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Shows skill gaps you didn't know existed&lt;/li&gt;
&lt;li&gt;Motivates consistent practice without burnout&lt;/li&gt;
&lt;li&gt;Turns vague "I need to get better" into specific "I'm weak on event loop edge cases"&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  🔧 I built a lightweight tool to try this
&lt;/h2&gt;

&lt;p&gt;While researching learning methods, I put together a small platform focused on &lt;strong&gt;practice vs testing modes&lt;/strong&gt;, 4-option questions, and anonymous community benchmarking. &lt;/p&gt;

&lt;p&gt;It's not another LeetCode clone. It's built for quick daily check-ins, tracking weak spots, and seeing how your answers compare to other developers' averages.&lt;/p&gt;

&lt;p&gt;👉 Try it here: &lt;a href="https://skillhacker.io" rel="noopener noreferrer"&gt;skillhacker.io&lt;/a&gt;&lt;br&gt;
&lt;em&gt;(Full disclosure: I'm the author. It's in early stages, so feedback is highly appreciated.)&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  📌 How to start measuring your level today
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Pick 1 topic you "kind of know"&lt;/li&gt;
&lt;li&gt;Take a 10-question set in test mode&lt;/li&gt;
&lt;li&gt;Review every wrong answer + read why distractors are wrong&lt;/li&gt;
&lt;li&gt;Repeat in practice mode without time pressure&lt;/li&gt;
&lt;li&gt;Compare your score to the community average&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Rinse. Repeat weekly. Watch the imposter syndrome shrink.&lt;/p&gt;

&lt;p&gt;What's your go-to method for validating your skills? Drop it in the comments 👇&lt;/p&gt;

</description>
      <category>programming</category>
      <category>learning</category>
      <category>testing</category>
      <category>interview</category>
    </item>
  </channel>
</rss>
