<?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>Forem: Maksim Nesterenko</title>
    <description>The latest articles on Forem by Maksim Nesterenko (@maksnester).</description>
    <link>https://forem.com/maksnester</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1208751%2F01db3911-19c7-46e2-bf76-68e8aaa4553f.jpeg</url>
      <title>Forem: Maksim Nesterenko</title>
      <link>https://forem.com/maksnester</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/maksnester"/>
    <language>en</language>
    <item>
      <title>Type vs Interface in typescript</title>
      <dc:creator>Maksim Nesterenko</dc:creator>
      <pubDate>Mon, 09 Sep 2024 13:40:34 +0000</pubDate>
      <link>https://forem.com/maksnester/type-vs-interface-in-typescript-5575</link>
      <guid>https://forem.com/maksnester/type-vs-interface-in-typescript-5575</guid>
      <description>&lt;p&gt;Today I learned some funny differences between types and interfaces in typescript. &lt;/p&gt;

&lt;p&gt;I used to think there was almost no practical difference between the &lt;code&gt;type&lt;/code&gt; and &lt;code&gt;interface&lt;/code&gt; and it all boils down to team preferences, but turned out these two behave quite differently when it comes to index signatures. The usage of interfaces can produce unexpected type incompatibilities (=TSC errors). Take a look at this example:&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;C&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;x&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="kr"&gt;number&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;D&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nl"&gt;x&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;const&lt;/span&gt; &lt;span class="nx"&gt;d&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;D&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;x&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;:&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;d&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// error&lt;/span&gt;
&lt;span class="c1"&gt;// Type 'D' is not assignable to type 'C'.&lt;/span&gt;
&lt;span class="c1"&gt;//   Index signature is missing in type 'D'.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But change the &lt;code&gt;interface D&lt;/code&gt; to &lt;code&gt;type D&lt;/code&gt; and it'll be alright. &lt;/p&gt;

&lt;p&gt;You can read more about it here &lt;a href="https://github.com/microsoft/TypeScript/issues/15300#issuecomment-371353444" rel="noopener noreferrer"&gt;https://github.com/microsoft/TypeScript/issues/15300#issuecomment-371353444&lt;/a&gt;&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>tsc</category>
    </item>
    <item>
      <title>Rebase a feature branch that is based on another feature branch which got merged</title>
      <dc:creator>Maksim Nesterenko</dc:creator>
      <pubDate>Mon, 12 Feb 2024 13:09:54 +0000</pubDate>
      <link>https://forem.com/maksnester/rebase-a-feature-branch-that-is-based-on-another-feature-branch-which-got-merged-2g8d</link>
      <guid>https://forem.com/maksnester/rebase-a-feature-branch-that-is-based-on-another-feature-branch-which-got-merged-2g8d</guid>
      <description>&lt;p&gt;So imagine you have 3 branches, where &lt;code&gt;F1&lt;/code&gt; is based on &lt;code&gt;master&lt;/code&gt; and &lt;code&gt;F2&lt;/code&gt; depends on &lt;code&gt;F1&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*-*-*               (master)
     \ %-%-%        (F1)
            \ x-x-x (F2) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To have &lt;code&gt;F2&lt;/code&gt; up-to-date with &lt;code&gt;F1&lt;/code&gt; you rebase it like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout F2
git rebase F1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;or, if someone else is pushing to &lt;code&gt;F1&lt;/code&gt; besides you and your local &lt;code&gt;F1&lt;/code&gt; might be not up-to-date, you do:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git checkout F2
git fetch
git rebase origin/F1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the work in the &lt;code&gt;F1&lt;/code&gt; branch is done and it's finally been merged into the master.&lt;/p&gt;

&lt;p&gt;The problem though, is that &lt;code&gt;F2&lt;/code&gt; is still based on top of &lt;code&gt;F1&lt;/code&gt; and contains its commits, so when you open a pull request for &lt;code&gt;F2&lt;/code&gt; against the &lt;code&gt;master&lt;/code&gt; it will contain the changes that do not make sense (commits from &lt;code&gt;F1&lt;/code&gt; that got merged already).&lt;/p&gt;

&lt;p&gt;The git tree looks like this now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*-*-*-%-%-%        (master)
     \            
       %-%-%-x-x-x (F2) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What we want here is to have our remaining &lt;code&gt;F2&lt;/code&gt; feature branch to be up-to-date with the &lt;code&gt;master&lt;/code&gt; and contain only the relevant commits.&lt;/p&gt;

&lt;p&gt;This can be done with &lt;a href="https://git-scm.com/docs/git-rebase"&gt;--onto&lt;/a&gt; rebase flag. So you can do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# just to make sure the F2 is up to date with the F1 first
git checkout F2
git rebase F1

# then change the base to be master
git rebase --onto master F1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That'll turn your git tree to be like this instead:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;*-*-*-%-%-%       (master)
           \            
            x-x-x (F2) 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After this, the commits in your pull request for &lt;code&gt;F2 -&amp;gt; master&lt;/code&gt; will make sense again.&lt;/p&gt;

</description>
      <category>git</category>
    </item>
    <item>
      <title>Do you really need types for CSS modules?</title>
      <dc:creator>Maksim Nesterenko</dc:creator>
      <pubDate>Tue, 14 Nov 2023 13:39:35 +0000</pubDate>
      <link>https://forem.com/maksnester/do-you-really-need-types-for-css-modules-2d66</link>
      <guid>https://forem.com/maksnester/do-you-really-need-types-for-css-modules-2d66</guid>
      <description>&lt;p&gt;Some teams that use CSS modules + typescript on their projects often consider having type generation for the CSS class names.&lt;/p&gt;

&lt;p&gt;File-wise it looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;my-feature.tsx
my-feature.module.scss
my-feature.module.scss.d.ts &amp;lt;-- generated types
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The setup for such type generation is well-described in &lt;a href="https://medium.com/@sapegin/css-modules-with-typescript-and-webpack-6b221ebe5f10"&gt;this article&lt;/a&gt; &lt;em&gt;with the only exception that the plugin which is recommended in the article is no longer supported and I'd use &lt;a href="https://github.com/TeamSupercell/typings-for-css-modules-loade"&gt;this one&lt;/a&gt; instead.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Let's review the pros and cons of having such type generation on a project. &lt;/p&gt;

&lt;h2&gt;
  
  
  Pros ✅
&lt;/h2&gt;

&lt;p&gt;When it comes to using TypeScript with CSS modules, the idea of generating types for class names has its merits.&lt;/p&gt;

&lt;h4&gt;
  
  
  Superior autocomplete
&lt;/h4&gt;

&lt;p&gt;With type generation, you get superior autocomplete that functions universally without the need for extra plugins.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;However, if you don't generate types,&lt;/em&gt; WebStorm IDE already provides autocomplete. And Visual Studio Code users, as always, can install &lt;a href="https://marketplace.visualstudio.com/items?itemName=clinyong.vscode-css-modules"&gt;the CSS modules plugin&lt;/a&gt; to have it. &lt;/p&gt;

&lt;h4&gt;
  
  
  Typo-Safe development
&lt;/h4&gt;

&lt;p&gt;Type generation makes your development process typo-safe. It immediately throws a type error if you happen to mistype a class name. This not only helps catch errors early in your IDE but ensures these issues are reported during CI processes, providing a robust safety net.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;However, if you don't generate types,&lt;/em&gt; wouldn't you take a look that your CSS changes have an effect anyway? &lt;/p&gt;

&lt;h2&gt;
  
  
  Cons ❌
&lt;/h2&gt;

&lt;h4&gt;
  
  
  New and unused code has no types generated
&lt;/h4&gt;

&lt;p&gt;Webpack is a smart tool and it is good for discarding unused code. Types are not generated for the unused code, which could be confusing especially when writing new code. The code generation won't kick in until your component, that imports the CSS file is properly imported somewhere.&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/iHpE3bdS29w"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  Delayed updates
&lt;/h4&gt;

&lt;p&gt;Regenerating types and updating the IDE takes some time. While not a major hurdle, it adds a step to your development workflow.&lt;/p&gt;

&lt;h4&gt;
  
  
  Jump to declaration gets worse
&lt;/h4&gt;

&lt;p&gt;The "jump to declaration" feature in IDEs might lead you to the TypeScript file instead of the corresponding SCSS file, which is quite annoying. I don't know how to fix this in webstorm, but VSCode has a &lt;a href="https://marketplace.visualstudio.com/items?itemName=viijay-kr.react-ts-css"&gt;plugin&lt;/a&gt; (of course).&lt;/p&gt;

&lt;p&gt;&lt;iframe width="710" height="399" src="https://www.youtube.com/embed/4T1pz6iQLDI"&gt;
&lt;/iframe&gt;
&lt;/p&gt;

&lt;h4&gt;
  
  
  One more webpack plugin
&lt;/h4&gt;

&lt;p&gt;To enable this setup you'll need one more dependency, one more plugin in your configuration. It is also &lt;a href="https://github.com/TeamSupercell/typings-for-css-modules-loader#webpack-rebuilds--builds-slow"&gt;recommended&lt;/a&gt; to ignore watching the generated files by applying webpack.WatchIgnorePlugin. It is not a big deal, but it adds the slightest new complexity to your webpack config for the sake of generating types.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion:
&lt;/h2&gt;

&lt;p&gt;So with that being said, I think the pros of types generating for CSS modules aren't really that big considering the downsides. This tooling might stand in your way sometimes, instead of making your work more productive.&lt;/p&gt;

&lt;p&gt;Without types generating, autocomplete is OK out of the box in webstorm (and vscode has a plugin). "Typo-safety" is nice, but when working with CSS you're normally supposed to give your changes a look anyway so you'll know quickly if something went wrong, or, perhaps, you have visual regression tests that can help you out. &lt;/p&gt;

&lt;p&gt;Of course, this all depends on your team's priorities and preferences. I, personally, really like how types help to highlight when classes are applied for no reason in code without any CSS attached to them. But I also don't like that "jump to declaration" doesn't navigate me to the CSS file in webstorm. So for smaller projects, I might prefer skipping the type generation for CSS modules. &lt;/p&gt;

</description>
      <category>cssmodules</category>
      <category>css</category>
      <category>typescript</category>
      <category>webpack</category>
    </item>
  </channel>
</rss>
