<?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: Bartosz Stefaniak</title>
    <description>The latest articles on DEV Community by Bartosz Stefaniak (@bartholomeas).</description>
    <link>https://dev.to/bartholomeas</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%2F1837390%2Ff054e114-5685-4782-b48d-e538fa002c35.jpg</url>
      <title>DEV Community: Bartosz Stefaniak</title>
      <link>https://dev.to/bartholomeas</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/bartholomeas"/>
    <language>en</language>
    <item>
      <title>Jak sformatować datę, liczbę lub walutę w Javascript?</title>
      <dc:creator>Bartosz Stefaniak</dc:creator>
      <pubDate>Mon, 05 Aug 2024 10:26:04 +0000</pubDate>
      <link>https://dev.to/bartholomeas/jak-sformatowac-date-liczbe-lub-walute-w-javascript-3nkk</link>
      <guid>https://dev.to/bartholomeas/jak-sformatowac-date-liczbe-lub-walute-w-javascript-3nkk</guid>
      <description>&lt;p&gt;Yo!&lt;/p&gt;

&lt;p&gt;Dziś mam dla Was bardzo ciekawy sposób na formatowanie większości danych (data, waluty, czas do danego momentu itd) bez konieczności zaciągania zewnętrznych paczek i polegania na rozwiązaniach innych. Pewnie większość osób (z mojej obserwacji), gdy musi przeformatować jakieś daty instaluje  daną paczkę (na Ciebie patrzę dayjs 👀), ale to w większości wypadków nie jest koniecznie, a tylko zwiększa końcowy rozmiar naszej aplikacji!&lt;/p&gt;

&lt;p&gt;W tym wpisie opiszę działanie wbudowanego obiektu w JS &lt;strong&gt;"Intl"&lt;/strong&gt;, który pozwala na sformatowanie naszych danych do określonego formatu w większości codziennych przypadków. 😉&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To obiekt rozszerzający Internationalization API z ECMAScript, które zapewnia porównywanie stringów z uwzględnieniem wielkości liter, formatowanie liczb, dat oraz czasu i wiele więcej (definicja z MDN)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Jego deklaracja wygląda mniej więcej tak:&lt;br&gt;
&lt;code&gt;const formatter = new Intl.&amp;lt;nazwa_metody&amp;gt;(&amp;lt;locale (np 'pl-PL')&amp;gt;, &amp;lt;opcjonalny obiekt z opcjami&amp;gt;)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Nie rozwodząc się zbyt długo, przejdę do pokazania działania na przykładach:&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatowanie liczb
&lt;/h2&gt;

&lt;p&gt;Jak widać na przykładach inicjalizujemy instancję naszego formattera podając localeString oraz opcjonalnie obiekt z konfiguracją, w której możemy przekazać co chcemy formatować oraz w jaki sposób. &lt;/p&gt;

&lt;p&gt;Jak widać na screenie poniżej przekazując &lt;br&gt;
&lt;code&gt;style='currency'&lt;/code&gt;&lt;br&gt;
 mówimy naszemu formaterowi, żeby zwracał wartości dostosowanie do formatu walut w danym kraju (przekazanym localeStringu). Jak wiadomo w USA z reguły używa się kropek zamiast przecinków (jak choćby w Polsce), a 'dolar' występuje przed liczbą. Na szczęście nie musimy martwić się o te kwestie, bo Intl zajmuje się tym za nas! ;)&lt;/p&gt;

&lt;p&gt;Instancją "NumberFormat" formatujemy wszystko co związane z liczbami, a więc waluty, ale również numery telefonów, procenty i podobne.&lt;/p&gt;

&lt;p&gt;Najwięcej wyjaśnią przykłady poniżej:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwik0mu9pc3qg3ywwl1q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwik0mu9pc3qg3ywwl1q.png" alt="Intl formatting NumberFormat" width="800" height="683"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatowanie walut
&lt;/h2&gt;

&lt;p&gt;W większości pokazane już w sekcji wyżej przy formatowaniu liczb, natomiast tutaj kilka przykładów uzupełniających:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgx7pega7y5xovrj5lfx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgx7pega7y5xovrj5lfx.png" alt="Intl formatting NumberFormat currencies" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatowanie dat
&lt;/h2&gt;

&lt;p&gt;Tworząc instancję Intl z &lt;strong&gt;DateTimeFormat&lt;/strong&gt; bez problemu sformatujemy również datę tak, aby spełniała nasze oczekiwania, potrzebujesz nazwy miesiąca w danym języku? &lt;em&gt;Myk&lt;/em&gt; i masz nazwę miesiąca pisaną słownie w obecnym języku strony.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjrjw6gmrsidsz3ele8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjrjw6gmrsidsz3ele8p.png" alt="Intl formatting dates" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatowanie czasu relatywnie do daty
&lt;/h2&gt;

&lt;p&gt;Z instancją &lt;strong&gt;RelativeTimeFormat&lt;/strong&gt; możemy wyświetlać, że coś się wydarzyło "X czasu temu" lub "za X czasu" w odniesieniu od obecnego momentu. Niesamowicie użyteczne na przykład w czatach, gdzie wiemy, że ktoś był dostępny "X minut temu" (znane choćby z Messengera ;) ).&lt;/p&gt;

&lt;p&gt;Użycie jest niesamowicie proste jak widać poniżej, do naszej instancji &lt;strong&gt;RelativeTimeFormat&lt;/strong&gt; podajemy liczbowo wartość "czasu", a następnie jednostkę, w której chcemy to liczyć (na przykład godziny, minuty). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2saqu6vk04ahe0homs91.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2saqu6vk04ahe0homs91.png" alt="Intl formatting relative time" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatowanie elementów w listach
&lt;/h2&gt;

&lt;p&gt;Możemy formatować nasze tablice z danymi na tekst, który "wylicza" te elementy. Zamiast używać &lt;strong&gt;join&lt;/strong&gt; na tablicy i dodawać łącznik (na przykład "czy") możemy po prostu użyć Intl! &lt;br&gt;
Dzięki temu z tablicy&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;["Samsung", "Apple", "Nokia"]&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 otrzymamy&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;Samsung, Apple i Nokia&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 ( w przypadku opcji conjuction).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbk74pyd3m9hdz0ewwtg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbk74pyd3m9hdz0ewwtg0.png" alt="Intl formatting list items" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Collator
&lt;/h2&gt;

&lt;p&gt;Super ciekawy przypadek pozwalający nam na porównywanie elementów uwzględniając ich rozmiar liter i specyficzne dla danego języka znaki (np ą, ę, ó dla Polskiego) zależnie od konfiguracji.&lt;/p&gt;

&lt;p&gt;Jak widzimy poniżej collatorPL podczas sortowania porównuje polskie znaki przy sortowaniu, dlatego "ą" jest dalej niż "a". Natomiast collatorUS nie rozróżnia tych znaków i traktuje "ą" jak "a" itd (z racji, że w angielskim alfabecie one nie występują) i tak też je sortuje. Najwięcej  pokaże screen poniżej!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewmjjuje01ko9ejth3fz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewmjjuje01ko9ejth3fz.png" alt="Intl formatting collator" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Podsumowanie
&lt;/h2&gt;

&lt;p&gt;To tyle na dzisiaj! Znałeś ten obiekt, używałeś/ używasz go na codzień? &lt;/p&gt;

&lt;p&gt;~ Bartek&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Moja strona:&lt;/strong&gt; &lt;a href="https://bstefaniak.pl" rel="noopener noreferrer"&gt;https://bstefaniak.pl&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;Linkedin:&lt;/strong&gt; &lt;a href="https://www.linkedin.com/in/bartosz-stefaniak-a82727222/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/bartosz-stefaniak-a82727222/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Materiały źródłowe&lt;/strong&gt;&lt;br&gt;
MDN: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#examples" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#examples&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to format date, currency or time period?</title>
      <dc:creator>Bartosz Stefaniak</dc:creator>
      <pubDate>Mon, 05 Aug 2024 10:16:33 +0000</pubDate>
      <link>https://dev.to/bartholomeas/how-to-format-date-currency-or-time-period-58kf</link>
      <guid>https://dev.to/bartholomeas/how-to-format-date-currency-or-time-period-58kf</guid>
      <description>&lt;p&gt;Yo!&lt;/p&gt;

&lt;p&gt;Today I have a very interesting way for you to format most of your data (date, currencies, time to a particular moment, etc) without having to download external packages and rely on the solutions of others. Probably most people (from my observation), when they need to reformat some dates install a given package (I'm looking at you dayjs 👀), but this in most cases is not necessary, and only increases the final size of our application!&lt;/p&gt;

&lt;p&gt;In this post, I'll describe the workings of the built-in object in JS &lt;strong&gt;"Intl "&lt;/strong&gt;, which allows us to format our data to a specific format in most everyday 😉&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;It's an object that extends the Internationalization API from ECMAScript, which provides case-sensitive string comparison, number, date and time formatting and more (definition from MDN)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Its declaration looks roughly like this:&lt;br&gt;
&lt;code&gt;const formatter = new Intl.&amp;lt;name_method&amp;gt;(&amp;lt;locale (e.g. 'en-PL')&amp;gt;, &amp;lt;optional object with options&amp;gt;)&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Without dwelling too long, I will proceed to show the operation with examples:&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting numbers
&lt;/h2&gt;

&lt;p&gt;As you can see in the examples, we initialize an instance of our formatter by passing localeString and an optional configuration object, where we can pass what we want to format and how. &lt;/p&gt;

&lt;p&gt;As you can see in the screenshot below by passing &lt;br&gt;
&lt;code&gt;style=`currency&lt;/code&gt; we tell our formatter to return values to match the currency format in the given country (the passed localeString). As you know, the US generally uses periods instead of commas (as in Poland, for example), and 'dollar' comes before a number. Fortunately, we don't have to worry about these issues, because Intl takes care of it for us! ;)&lt;/p&gt;

&lt;p&gt;With the "NumberFormat" instance we format everything related to numbers, so currencies, but also phone numbers, percentages and the like.&lt;/p&gt;

&lt;p&gt;The examples below will explain the most:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwik0mu9pc3qg3ywwl1q.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fuwik0mu9pc3qg3ywwl1q.png" alt="Intl formatting NumberFormat" width="800" height="683"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting currencies
&lt;/h2&gt;

&lt;p&gt;Mostly already shown in the section above on formatting numbers, while here are some complementary examples:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgx7pega7y5xovrj5lfx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fcgx7pega7y5xovrj5lfx.png" alt="Intl formatting NumberFormat currencies" width="800" height="596"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting dates
&lt;/h2&gt;

&lt;p&gt;By creating an Intl instance with &lt;strong&gt;DateTimeFormat&lt;/strong&gt; we can also easily format the date to meet our needs, need the name of the month in a particular language? &lt;em&gt;Myk&lt;/em&gt; and you have the month name written in words in the current site language.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjrjw6gmrsidsz3ele8p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxjrjw6gmrsidsz3ele8p.png" alt="Intl formatting dates" width="800" height="435"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting the time relatively to the date
&lt;/h2&gt;

&lt;p&gt;With the &lt;strong&gt;RelativeTimeFormat&lt;/strong&gt; instance, we can display that something happened "X time ago" or "in X time" relative to the current moment. Incredibly useful in chat rooms, for example, where we know that someone was available "X minutes ago" (known even from Messenger ;) ).&lt;/p&gt;

&lt;p&gt;The usage is incredibly simple as you can see below, to our &lt;strong&gt;RelativeTimeFormat&lt;/strong&gt; instance we give a numeric value of "time" and then the unit in which we want to count it (for example, hours, minutes). &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2saqu6vk04ahe0homs91.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2saqu6vk04ahe0homs91.png" alt="Intl formatting relative time" width="800" height="423"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Formatting elements in lists
&lt;/h2&gt;

&lt;p&gt;We can format our arrays of data into text that "enumerates" those elements. Instead of using &lt;strong&gt;join&lt;/strong&gt; on the array and adding a hyphen (for example, "if") we can simply use Intl! &lt;br&gt;
This way from the array &lt;code&gt;["Samsung", "Apple", "Nokia"]&lt;/code&gt; we will get&lt;br&gt;
&lt;br&gt;
 &lt;code&gt;Samsung, Apple and Nokia&lt;/code&gt;&lt;br&gt;
&lt;br&gt;
 ( in case of conjuction option).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbk74pyd3m9hdz0ewwtg0.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbk74pyd3m9hdz0ewwtg0.png" alt="Intl formatting list items" width="800" height="469"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Collator
&lt;/h2&gt;

&lt;p&gt;A super interesting case that allows us to compare elements taking into account their letter size and language-specific characters (e.g. ą, ę, ó for Polish) depending on the configuration.&lt;/p&gt;

&lt;p&gt;As we can see below collatorPL compares Polish characters when sorting, so "ą" is further away than "a". On the other hand, collatorUS does not distinguish between these characters and treats "ą" as "a" etc. (due to the fact that they do not occur in the English alphabet) and sorts them that way. The screen below will show the most!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewmjjuje01ko9ejth3fz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fewmjjuje01ko9ejth3fz.png" alt="Intl formatting collator" width="800" height="402"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;That's it for today! Did you know this object, use/use it on a daily basis? &lt;/p&gt;

&lt;p&gt;~ Bartek&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My website:&lt;/strong&gt; &lt;a href="https://bstefaniak.pl" rel="noopener noreferrer"&gt;https://bstefaniak.pl&lt;/a&gt; (currently only polish)&lt;br&gt;
&lt;strong&gt;Linkedin:&lt;/strong&gt; &lt;a href="https://www.linkedin.com/in/bartosz-stefaniak-a82727222/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/bartosz-stefaniak-a82727222/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source material&lt;/strong&gt;.&lt;br&gt;
MDN: &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#examples" rel="noopener noreferrer"&gt;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#examples&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Whats new in Typescript 5.5?</title>
      <dc:creator>Bartosz Stefaniak</dc:creator>
      <pubDate>Thu, 25 Jul 2024 09:10:06 +0000</pubDate>
      <link>https://dev.to/bartholomeas/co-nowego-w-typescript-55-da0</link>
      <guid>https://dev.to/bartholomeas/co-nowego-w-typescript-55-da0</guid>
      <description>&lt;p&gt;Check also my website! (Currently only in Polish): &lt;a href="https://bstefaniak.pl/blog/co-nowego-w-typescript-5-5" rel="noopener noreferrer"&gt;https://bstefaniak.pl/blog/co-nowego-w-typescript-5-5&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Howdy!&lt;br&gt;
Not so long ago the new Typescript officially came out, specifically version 5.5, which brought some really cool fiches, which are presented in this post. Most of them happen underneath, without our interference, but I think they are worth knowing about ;)&lt;br&gt;
Enjoy reading!&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Better type inference
&lt;/h2&gt;

&lt;p&gt;As you can see below, we create our &lt;strong&gt;trailerParkBoys&lt;/strong&gt; object array, to which we assign some data.&lt;br&gt;
Next up is the &lt;strong&gt;getTrailerParkBoy&lt;/strong&gt; function, which takes an array of strings &lt;strong&gt;names&lt;/strong&gt;. Then we do an operation on the &lt;strong&gt;names&lt;/strong&gt; array; we map and filter all the elements that are &lt;strong&gt;"undefined ‘&lt;/strong&gt;, so there shouldn't be any &lt;strong&gt;’undefined ”&lt;/strong&gt; element in our &lt;strong&gt;boys&lt;/strong&gt; array anymore right?&lt;/p&gt;

&lt;h3&gt;
  
  
  Inference in TS below 5.5
&lt;/h3&gt;

&lt;p&gt;Well, that's exactly in &lt;strong&gt;TS under 5.5&lt;/strong&gt; (the version in the screenshot is 5.4.5) that's not true 😕.&lt;br&gt;
You can see this in the following lines, where we try to call the &lt;strong&gt;"action ”&lt;/strong&gt; method on every element in the &lt;strong&gt;boys&lt;/strong&gt; array, because for TS, the &lt;strong&gt;boy&lt;/strong&gt; element may still be undefined (even though we filtered out all undefined elements beforehand!).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyeox729aai0colrmlfg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgyeox729aai0colrmlfg.png" alt="Infer TS 5.4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Improving type inference in TS 5.5.
&lt;/h3&gt;

&lt;p&gt;But here's where Typescript 5.5 goes all in white. 😎&lt;br&gt;
Now Typescript no longer screams about a possibly undefined element, now the type is correctly deduced and we can execute the &lt;strong&gt;boy.action()&lt;/strong&gt; method without any problems.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw55srqc38vmt1p3ztel.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faw55srqc38vmt1p3ztel.png" alt="Infer TS 5.5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Why is this so?
&lt;/h3&gt;

&lt;p&gt;Typescript 5.5 now automatically creates a “guard check” for itself in methods such as &lt;strong&gt;.filter&lt;/strong&gt;, i.e. a check to make sure that the element we have in mind is of the correct type, and not (in this case) &lt;strong&gt;undefined&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;As in the screenshot below, depending on the value of &lt;strong&gt;true&lt;/strong&gt; or &lt;strong&gt;false&lt;/strong&gt; of the condition occurring, our element is the expected type (in the case of &lt;strong&gt;true&lt;/strong&gt;) or something else, in the case of &lt;strong&gt;false&lt;/strong&gt;; in our example it's “undefined”. &lt;/p&gt;

&lt;p&gt;The value &lt;strong&gt;true&lt;/strong&gt; or &lt;strong&gt;false&lt;/strong&gt; results from the result of the check that takes place underneath (and looks like the commented out isTrailerParkBoy function in the screenshot below)&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

const isSpecifiedType(value: WantedType | undefined): value is WantedType =&amp;gt; .... 
(... is ...Type expects boolean)


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;NOTE&lt;/strong&gt; only works when we directly give “undefined” as a possible type&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdx340q7lqndm9z3tqe9.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdx340q7lqndm9z3tqe9.png" alt="Type Guard"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Conditions when it works!
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;the function does not have an explicit type &lt;strong&gt;return&lt;/strong&gt; or direct type assignment.&lt;/li&gt;
&lt;li&gt;The function contains only one return statement and no implicit returns.&lt;/li&gt;
&lt;li&gt;The function does not modify its parameter.&lt;/li&gt;
&lt;li&gt;the function returns an expression of type &lt;strong&gt;boolean&lt;/strong&gt;, which is related to the refinement of the parameter type.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;An example where this does not work:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;

function getClassroomAverage(students: string[], allScores: Map&amp;lt;string, number&amp;gt;) {
  const studentScores = students
    .map(student =&amp;gt; allScores.get(student))
    .filter(score =&amp;gt; !!score);

  return studentScores.reduce((a, b) =&amp;gt; a + b) / studentScores.length;
  // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  // error: object is possibly 'undefined'.
}


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

&lt;/div&gt;

&lt;p&gt;Because the &lt;strong&gt;!!score&lt;/strong&gt; condition in the .filter method can be &lt;strong&gt;false&lt;/strong&gt; in the case of null, undefined (and indeed undesirable values), but so will be &lt;strong&gt;false&lt;/strong&gt; when score = 0&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Type Narrowing
&lt;/h2&gt;

&lt;p&gt;Many times it has happened to you that all signs on heaven and earth indicated that some object &lt;strong&gt;NEWLY&lt;/strong&gt; has a given key, but in fact the editor was spilling errors left and right screaming that &lt;strong&gt;MAAN, THERE IS NO SUCH KEY&lt;/strong&gt;? &lt;br&gt;
To me, quite often, that amounted to fiddling with the code, adding guards or projecting types to make TS realize how wrong he was.&lt;br&gt;
Fortunately, that's just history now 😎&lt;/p&gt;

&lt;h3&gt;
  
  
  TS older than 5.5 did not properly narrow the type when getting to the value in the object
&lt;/h3&gt;

&lt;p&gt;As you can see below in Typescript &lt;strong&gt;older than&lt;/strong&gt; 5.5, despite trying to get into the value of a &lt;strong&gt;obj&lt;/strong&gt; record after checking that the value exists and is a string Typescript still threw the error &lt;em&gt;Object is of type 'unknown'&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Until now, we had to deal with this problem in a different way, for example, by casting the value manually ```as&lt;br&gt;
&lt;br&gt;
 TYPE''&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdhl6c4knhbt9qu3it49.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjdhl6c4knhbt9qu3it49.png" alt="TS 5.4 narrowing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  TS 5.5 correctly deduces the type
&lt;/h3&gt;

&lt;p&gt;As you can see below in Typescript 5.5 this has been corrected, so that after checking the value {% raw %}&lt;code&gt;obj[key]&lt;/code&gt; we know that it exists and is of type &lt;strong&gt;string&lt;/strong&gt; therefore we are able to get to it without any problem using &lt;code&gt;obj[key]&lt;/code&gt; and call the string method &lt;code&gt;toUpperCase()&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcr7tcgzt3kodgtb94lz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frcr7tcgzt3kodgtb94lz.png" alt="TS 5.5 Narrowing"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Type checking in REGEXP
&lt;/h2&gt;

&lt;p&gt;Now RegExps are validated! This is great news, because now every redundant or dropped parenthesis will throw us an error. (aaa, for the rest, no one writes regexps by hand anyway 👀)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk4la4t8k6c25jh93ljjh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fk4la4t8k6c25jh93ljjh.png" alt="Regexp"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  4. JSDoc type imports
&lt;/h2&gt;

&lt;p&gt;I suspect that JSDoc is somehow not incredibly often used due to the fact that Typescript itself “documents” the code we write. Nevertheless, I subjectively like this type of “summary” of what a function, method, class does and what it is responsible for.&lt;/p&gt;

&lt;p&gt;To shore up; until now we were limited to primitive types, type rewriting, or trying to import it from some modules.&lt;/p&gt;

&lt;p&gt;Before TypeScript 5.5, when you tried to import types from external files in JSDoc in JavaScript files, you often got runtime (execution time) errors. This was because TypeScript treated these imports as if they were actual module imports, leading to problems when in fact only types were imported, not entire implementations.&lt;br&gt;
In addition, Typescript, due to its limited static type parsing capabilities, could not accurately recognize types from external files.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F61yzsqxyz5rd5vypa53e.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F61yzsqxyz5rd5vypa53e.png" alt="JSDOC TS 5.4"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In TS 5.5, we can use the &lt;strong&gt;@import&lt;/strong&gt; keyword to import our type as we normally do.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkysmqexjulzkcpya3cqu.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fkysmqexjulzkcpya3cqu.png" alt="JSDOC TS 5.5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  5. configDir variable in tsconfigs
&lt;/h2&gt;

&lt;p&gt;An improvement mostly useful in monorepo projects, but worth knowing about. We can have a base &lt;strong&gt;tsconfig&lt;/strong&gt; file that we share in several different places. Well, that's all cool, but what if you need to recompile TS to a particular folder, for example? Then a little problem begins, because even though our “outDir” will spit out the resulting files to the /dist folder (conventionally named dist), but where our base file is located, not the one that extends it.&lt;/p&gt;

&lt;p&gt;With the help of the new variable configDir, which makes this path to be with respect to the file it extends, not the base file.&lt;br&gt;
That is, in the case in the screenshot, it will compile our TS files to the &lt;strong&gt;/front-dir/dist&lt;/strong&gt; folder (rather than /dist).&lt;/p&gt;

&lt;p&gt;We use this in the same way as the variables in the template string: `&lt;code&gt;“${configDir}/folder_name”&lt;/code&gt;; just like in the screengrab below. ;)&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ya9hotvx5d8ifvx9qfa.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9ya9hotvx5d8ifvx9qfa.png" alt="TSConfig files"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;These are, in my opinion, the most important items that will make your work much more enjoyable. &lt;br&gt;
I encourage you to bite into the other tidbits in this issue of TS 🤓.&lt;/p&gt;

&lt;p&gt;Until the next one!&lt;br&gt;
~ Bartek&lt;/p&gt;

&lt;p&gt;Check also my website! (Currently only in Polish): &lt;a href="https://bstefaniak.pl/blog/co-nowego-w-typescript-5-5" rel="noopener noreferrer"&gt;https://bstefaniak.pl/blog/co-nowego-w-typescript-5-5&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Source material:&lt;/strong&gt;.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#easier-api-consumption-from-ecmascript-modules" rel="noopener noreferrer"&gt;https://devblogs.microsoft.com/typescript/announcing-typescript-5-5-beta/#easier-api-consumption-from-ecmascript-modules&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Linkedin: &lt;a href="https://www.linkedin.com/in/bartosz-stefaniak-a82727222/" rel="noopener noreferrer"&gt;https://www.linkedin.com/in/bartosz-stefaniak-a82727222/&lt;/a&gt;&lt;/p&gt;

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