<?xml version="1.0" encoding="utf-8"?>
<feed xml:lang="en-US" xmlns:media="http://search.yahoo.com/mrss/" xmlns="http://www.w3.org/2005/Atom">
<id>urn:uuid:1dd3f532-4b34-4b57-8a23-8abad84ed53d</id>
<title>Marius Schulz</title>
<subtitle>Marius Schulz on web development, modern JavaScript, and TypeScript.</subtitle>
<link rel="alternate" type="text/html" href="https://mariusschulz.com/" />
<link rel="first" href="https://mariusschulz.com/blog/feed/posts-1.xml" />
<link rel="previous" href="https://mariusschulz.com/blog/feed/posts-0.xml" />
<link rel="self" href="https://mariusschulz.com/blog/feed/posts-1.xml" />
<link rel="next" href="https://mariusschulz.com/blog/feed/posts-2.xml" />
<link rel="last" href="https://mariusschulz.com/blog/feed/posts-19.xml" />
<icon>https://mariusschulz.com/_astro/marius-schulz-250x250.DjcxjcYa.jpg</icon>
<logo>https://mariusschulz.com/_astro/marius-schulz-250x250.DjcxjcYa.jpg</logo>
<icon xmlns="http://webfeeds.org/rss/1.0">https://mariusschulz.com/_astro/marius-schulz-250x250.DjcxjcYa.jpg</icon>
<updated>2022-06-06T00:00:00.000Z</updated>
<author>
  <name>Marius Schulz</name>
  <uri>https://mariusschulz.com/</uri>
  <email>marius.schulz@me.com</email>
</author>
<entry>
  <id>urn:uuid:cc49d6fe-336c-427a-901d-3bbf9ff4d246</id>
  <title>Keyboard Shortcuts for Jumping and Deleting in iTerm2</title>
  <link href="https://mariusschulz.com/blog/keyboard-shortcuts-for-jumping-and-deleting-in-iterm2" />
  <published>2022-06-05T00:00:00.000Z</published>
  <updated>2022-06-06T00:00:00.000Z</updated>
  <summary type="text">How to configure familiar keyboard shortcuts for common navigation and edit actions in the iTerm2 command prompt, such as jumping to the start or end of a word or line.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;As a software engineer, I find myself using the terminal every day to run all sorts of commands. My current terminal of choice is &lt;a href=&quot;https://iterm2.com/&quot;&gt;iTerm2&lt;/a&gt; which I’ve been using happily for many years.&lt;/p&gt;
&lt;p&gt;Whenever I set up iTerm2 on a new Mac, one of the first things I do is to configure familiar keyboard shortcuts for common navigation and edit actions in the command prompt. For example, I typically configure the &lt;kbd&gt;⌥&lt;/kbd&gt;&lt;kbd&gt;←&lt;/kbd&gt; shortcut which jumps me to the start of the word under the cursor.&lt;/p&gt;
&lt;p&gt;To configure custom keyboard shortcuts in iTerm2, open the preferences dialog and navigate to the &lt;em&gt;Profiles › Keys › Key Mappings&lt;/em&gt; tab:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../images/iterm/iterm_settings_key_mappings-2x.webp&quot; alt=&quot;iTerm2 Preferences: Profiles › Keys › Key Mappings&quot;&gt;&lt;/p&gt;
&lt;p&gt;Click the “+” button to add a new key mapping, or double-click an existing key mapping to edit it. For the “Jump to start of word” command, select the “Send Escape Sequence” action and send the escape sequence &lt;code&gt;Esc+b&lt;/code&gt;:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;../images/iterm/iterm_settings_edit_key_mapping-2x.webp&quot; alt=&quot;iTerm2 Preferences: dialog for editing a key mapping&quot;&gt;&lt;/p&gt;
&lt;p&gt;Now, whenever you&amp;#39;re typing a command in iTerm2, it&amp;#39;s really easy to jump back to the start of the word (or even multiple words) to insert more text or delete part of the command — no more need for repeatedly pressing the &lt;kbd&gt;←&lt;/kbd&gt; key to navigate back character by character.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the full list of keyboard shortcuts that I configure for various jump and delete commands:&lt;/p&gt;
&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Shortcut&lt;/th&gt;
&lt;th&gt;Command&lt;/th&gt;
&lt;th&gt;Action&lt;/th&gt;
&lt;th&gt;Send&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;&lt;tr&gt;
&lt;td&gt;&lt;kbd&gt;⌥&lt;/kbd&gt;&lt;kbd&gt;←&lt;/kbd&gt;&lt;/td&gt;
&lt;td&gt;Jump to start of word&lt;/td&gt;
&lt;td&gt;Send Escape Sequence&lt;/td&gt;
&lt;td&gt;&lt;code&gt;b&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;kbd&gt;⌥&lt;/kbd&gt;&lt;kbd&gt;→&lt;/kbd&gt;&lt;/td&gt;
&lt;td&gt;Jump to end of word&lt;/td&gt;
&lt;td&gt;Send Escape Sequence&lt;/td&gt;
&lt;td&gt;&lt;code&gt;f&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;kbd&gt;⌘&lt;/kbd&gt;&lt;kbd&gt;←&lt;/kbd&gt;&lt;/td&gt;
&lt;td&gt;Jump to start of line&lt;/td&gt;
&lt;td&gt;Send Hex Codes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x01&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;kbd&gt;⌘&lt;/kbd&gt;&lt;kbd&gt;→&lt;/kbd&gt;&lt;/td&gt;
&lt;td&gt;Jump to end of line&lt;/td&gt;
&lt;td&gt;Send Hex Codes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x05&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;kbd&gt;⌥&lt;/kbd&gt;&lt;kbd&gt;⌫&lt;/kbd&gt;&lt;/td&gt;
&lt;td&gt;Delete to start of word&lt;/td&gt;
&lt;td&gt;Send Hex Codes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x17&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;kbd&gt;⌘&lt;/kbd&gt;&lt;kbd&gt;⌫&lt;/kbd&gt;&lt;/td&gt;
&lt;td&gt;Delete entire line&lt;/td&gt;
&lt;td&gt;Send Hex Codes&lt;/td&gt;
&lt;td&gt;&lt;code&gt;0x15&lt;/code&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;&lt;/table&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:6d5e9c03-3be4-4c6b-908f-ff28e983a865</id>
  <title>Assertion Functions in TypeScript</title>
  <link href="https://mariusschulz.com/blog/assertion-functions-in-typescript" />
  <published>2022-02-13T00:00:00.000Z</published>
  <updated>2022-02-13T00:00:00.000Z</updated>
  <summary type="text">TypeScript 3.7 implemented support for assertion functions in the type system. An assertion function is a function that throws an error if something unexpected happened. Using assertion signatures, we can tell TypeScript that a function should be treated as an assertion function.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;TypeScript 3.7 implemented support for assertion functions in the type system. An assertion function is a function that throws an error if something unexpected happened. Using assertion signatures, we can tell TypeScript that a function should be treated as an assertion function.&lt;/p&gt;
&lt;h2&gt;An Example: The &lt;code&gt;document.getElementById()&lt;/code&gt; Method&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s start by looking at an example in which we&amp;#39;re using the &lt;code&gt;document.getElementById()&lt;/code&gt; method to find a DOM element that has the ID &amp;quot;root&amp;quot;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&amp;#39;re calling the &lt;code&gt;root.addEventListener()&lt;/code&gt; method to attach a click handler to the element. However, TypeScript reports a type error:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);

// Object is possibly null
root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;root&lt;/code&gt; variable is of type &lt;code&gt;HTMLElement | null&lt;/code&gt;, which is why TypeScript reports a type error &amp;quot;Object is possibly null&amp;quot; when we&amp;#39;re trying to call the &lt;code&gt;root.addEventListener()&lt;/code&gt; method. In order for our code to be considered type-correct, we somehow need to make sure that the &lt;code&gt;root&lt;/code&gt; variable is non-null and non-undefined before calling the &lt;code&gt;root.addEventListener()&lt;/code&gt; method. We have a couple of options for how we can do that, including:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Using the non-null assertion operator &lt;code&gt;!&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Implementing an inline null check&lt;/li&gt;
&lt;li&gt;Implementing an assertion function&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Let&amp;#39;s look at each of the three options.&lt;/p&gt;
&lt;h2&gt;Using the Non-Null Assertion Operator&lt;/h2&gt;
&lt;p&gt;First up, we&amp;#39;ll try and use the non-null assertion operator &lt;code&gt;!&lt;/code&gt; which is written as a post-fix operator after the &lt;code&gt;document.getElementById()&lt;/code&gt; call:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;)!;

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The non-null assertion operator &lt;code&gt;!&lt;/code&gt; tells TypeScript to assume that the value returned by &lt;code&gt;document.getElementById()&lt;/code&gt; is non-null and non-undefined (also known as “non-nullish”). TypeScript will exclude the types &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; from the type of the expression to which we apply the &lt;code&gt;!&lt;/code&gt; operator.&lt;/p&gt;
&lt;p&gt;In this case, the return type of the &lt;code&gt;document.getElementById()&lt;/code&gt; method is &lt;code&gt;HTMLElement | null&lt;/code&gt;, so if we apply the &lt;code&gt;!&lt;/code&gt; operator, we get &lt;code&gt;HTMLElement&lt;/code&gt; as the resulting type. Consequently, TypeScript no longer reports the type error that we saw previously.&lt;/p&gt;
&lt;p&gt;However, using the non-null assertion operator is probably not the right fix in this situation. The &lt;code&gt;!&lt;/code&gt; operator is completely erased when our TypeScript code is compiled to JavaScript:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The non-null assertion operator has no runtime manifestation whatsoever. That is, the TypeScript compiler does not emit any validation code to verify that the expression is actually non-nullish. Therefore, if the &lt;code&gt;document.getElementById()&lt;/code&gt; call returns &lt;code&gt;null&lt;/code&gt; because no matching element can be found, our &lt;code&gt;root&lt;/code&gt; variable will hold the value &lt;code&gt;null&lt;/code&gt; and our attempt to call the &lt;code&gt;root.addEventListener()&lt;/code&gt; method will fail.&lt;/p&gt;
&lt;h2&gt;Implementing an Inline Null Check&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s now consider the second option and implement an inline null check to verify that the &lt;code&gt;root&lt;/code&gt; variable holds a non-null value:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);

if (root === null) {
  throw Error(&amp;quot;Unable to find DOM element #root&amp;quot;);
}

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because of our null check, TypeScript&amp;#39;s type checker will narrow the type of the &lt;code&gt;root&lt;/code&gt; variable from &lt;code&gt;HTMLElement | null&lt;/code&gt; (before the null check) to &lt;code&gt;HTMLElement&lt;/code&gt; (after the null check):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);

// Type: HTMLElement | null
root;

if (root === null) {
  throw Error(&amp;quot;Unable to find DOM element #root&amp;quot;);
}

// Type: HTMLElement
root;

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This approach is much safer than the previous approach using the non-null assertion operator. We&amp;#39;re explicitly handling the case in which the &lt;code&gt;root&lt;/code&gt; variable holds the value &lt;code&gt;null&lt;/code&gt; by throwing an error with a descriptive error message.&lt;/p&gt;
&lt;p&gt;Also, note that this approach does not contain any TypeScript-specific syntax whatsoever; all of the above is syntactically valid JavaScript. TypeScript&amp;#39;s &lt;a href=&quot;/blog/control-flow-based-type-analysis-in-typescript&quot;&gt;control flow analysis&lt;/a&gt; understands the effect of our null check and narrows the type of the &lt;code&gt;root&lt;/code&gt; variable in different places of the program — no explicit type annotations needed.&lt;/p&gt;
&lt;h2&gt;Implementing an Assertion Function&lt;/h2&gt;
&lt;p&gt;Lastly, let&amp;#39;s now see how we can use an assertion function to implement this null check in a reusable way. We&amp;#39;ll start by implementing an &lt;code&gt;assertNonNullish&lt;/code&gt; function that will throw an error if the provided value is either &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function assertNonNullish(
  value: unknown,
  message: string
) {
  if (value === null || value === undefined) {
    throw Error(message);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&amp;#39;re using the &lt;a href=&quot;/blog/the-unknown-type-in-typescript&quot;&gt;&lt;code&gt;unknown&lt;/code&gt; type&lt;/a&gt; for the &lt;code&gt;value&lt;/code&gt; parameter here to allow callsites to pass a value of an arbitrary type. We&amp;#39;re only comparing the &lt;code&gt;value&lt;/code&gt; parameter to the values &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;, so we don&amp;#39;t need to require the &lt;code&gt;value&lt;/code&gt; parameter to have a more specific type.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s how we would use the &lt;code&gt;assertNonNullish&lt;/code&gt; function in our example from before. We&amp;#39;re passing it the &lt;code&gt;root&lt;/code&gt; variable as well as the error message:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);
assertNonNullish(root, &amp;quot;Unable to find DOM element #root&amp;quot;);

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, TypeScript still produces a type error for the &lt;code&gt;root.addEventListener()&lt;/code&gt; method call:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);
assertNonNullish(root, &amp;quot;Unable to find DOM element #root&amp;quot;);

// Object is possibly null
root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If we have a look at the type of the &lt;code&gt;root&lt;/code&gt; variable before and after the &lt;code&gt;assertNonNullish()&lt;/code&gt; call, we&amp;#39;ll see that it is of type &lt;code&gt;HTMLElement | null&lt;/code&gt; in both places:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);

// Type: HTMLElement | null
root;

assertNonNullish(root, &amp;quot;Unable to find DOM element #root&amp;quot;);

// Type: HTMLElement | null
root;

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is because TypeScript doesn&amp;#39;t understand that our &lt;code&gt;assertNonNullish&lt;/code&gt; function will throw an error if the provided &lt;code&gt;value&lt;/code&gt; is nullish. We need to explicitly let TypeScript know that the &lt;code&gt;assertNonNullish&lt;/code&gt; function should be treated as an &lt;strong&gt;assertion function&lt;/strong&gt; that &lt;em&gt;asserts&lt;/em&gt; that the value is non-nullish, and that it will throw an error otherwise. We can do that using the &lt;code&gt;asserts&lt;/code&gt; keyword in the return type annotation:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function assertNonNullish&amp;lt;TValue&amp;gt;(
  value: TValue,
  message: string
): asserts value is NonNullable&amp;lt;TValue&amp;gt; {
  if (value === null || value === undefined) {
    throw Error(message);
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First of all, note that the &lt;code&gt;assertNonNullish&lt;/code&gt; function is now a generic function. It declares a single type parameter &lt;code&gt;TValue&lt;/code&gt; that we use as the type of the &lt;code&gt;value&lt;/code&gt; parameter; we&amp;#39;re also using the &lt;code&gt;TValue&lt;/code&gt; type in the return type annotation.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;asserts value is NonNullable&amp;lt;TValue&amp;gt;&lt;/code&gt; return type annotation is what&amp;#39;s called an &lt;strong&gt;assertion signature&lt;/strong&gt;. This assertion signature says that if the function returns normally (that is, if it doesn&amp;#39;t throw an error), it has asserted that the &lt;code&gt;value&lt;/code&gt; parameter is of type &lt;code&gt;NonNullable&amp;lt;TValue&amp;gt;&lt;/code&gt;. TypeScript uses this piece of information to narrow the type of the expression that we passed to the &lt;code&gt;value&lt;/code&gt; parameter.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;NonNullable&amp;lt;T&amp;gt;&lt;/code&gt; type is a &lt;a href=&quot;/blog/conditional-types-in-typescript#the-nonnullable-t-conditional-type&quot;&gt;conditional type&lt;/a&gt; that is defined in the &lt;em&gt;lib.es5.d.ts&lt;/em&gt; type declaration file that ships with the TypeScript compiler:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;/**
 * Exclude null and undefined from T
 */
type NonNullable&amp;lt;T&amp;gt; = T extends null | undefined ? never : T;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When applied to the type &lt;code&gt;T&lt;/code&gt;, the &lt;code&gt;NonNullable&amp;lt;T&amp;gt;&lt;/code&gt; helper type removes the types &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; from &lt;code&gt;T&lt;/code&gt;. Here are a few examples:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;NonNullable&amp;lt;HTMLElement&amp;gt;&lt;/code&gt; evaluates to &lt;code&gt;HTMLElement&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NonNullable&amp;lt;HTMLElement | null&amp;gt;&lt;/code&gt; evaluates to &lt;code&gt;HTMLElement&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NonNullable&amp;lt;HTMLElement | null | undefined&amp;gt;&lt;/code&gt; evaluates to &lt;code&gt;HTMLElement&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NonNullable&amp;lt;null&amp;gt;&lt;/code&gt; evaluates to &lt;code&gt;never&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NonNullable&amp;lt;undefined&amp;gt;&lt;/code&gt; evaluates to &lt;code&gt;never&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NonNullable&amp;lt;null | undefined&amp;gt;&lt;/code&gt; evaluates to &lt;code&gt;never&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;With our assertion signature in place, TypeScript now correctly narrows the type of the &lt;code&gt;root&lt;/code&gt; variable after the &lt;code&gt;assertNonNullish()&lt;/code&gt; function call. The type checker understands that when &lt;code&gt;root&lt;/code&gt; holds a nullish value, the &lt;code&gt;assertNonNullish&lt;/code&gt; function will throw an error. If the control flow of the program makes it past the &lt;code&gt;assertNonNullish()&lt;/code&gt; function call, the &lt;code&gt;root&lt;/code&gt; variable must contain a non-nullish value, and therefore TypeScript narrows its type accordingly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);

// Type: HTMLElement | null
root;

assertNonNullish(root, &amp;quot;Unable to find DOM element #root&amp;quot;);

// Type: HTMLElement
root;

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As a result of this type narrowing, our example now type-checks correctly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const root = document.getElementById(&amp;quot;root&amp;quot;);
assertNonNullish(root, &amp;quot;Unable to find DOM element #root&amp;quot;);

root.addEventListener(&amp;quot;click&amp;quot;, e =&amp;gt; {
  /* ... */
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So here we have it: a reusable &lt;code&gt;assertNonNullish&lt;/code&gt; assertion function that we can use to verify that an expression has a non-nullish value and to narrow the type of that expression accordingly by removing the &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; types from it.&lt;/p&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:43da542e-0260-4930-aead-36d7209e8c1b</id>
  <title>Optional Chaining: The ?. Operator in TypeScript</title>
  <link href="https://mariusschulz.com/blog/optional-chaining-the-operator-in-typescript" />
  <published>2021-08-02T00:00:00.000Z</published>
  <updated>2021-09-11T00:00:00.000Z</updated>
  <summary type="text">TypeScript 3.7 added support for the ?. operator, also known as the optional chaining operator. We can use this operator to descend into an object whose properties potentially hold the values null or undefined without writing any null checks for intermediate properties.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;TypeScript 3.7 added support for the &lt;code&gt;?.&lt;/code&gt; operator, also known as the optional chaining operator. We can use optional chaining to descend into an object whose properties potentially hold the values &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt; without writing any null checks for intermediate properties.&lt;/p&gt;
&lt;p&gt;Optional chaining is not a feature specific to TypeScript. The &lt;code&gt;?.&lt;/code&gt; operator got added to the ECMAScript standard as part of &lt;a href=&quot;https://262.ecma-international.org/11.0/&quot;&gt;ES2020&lt;/a&gt;. All modern browsers &lt;a href=&quot;https://caniuse.com/mdn-javascript_operators_optional_chaining&quot;&gt;natively support optional chaining&lt;/a&gt; (not including IE11).&lt;/p&gt;
&lt;p&gt;In this post, I will go over the following three optional chaining operators and explain why we might want to use them in our TypeScript or JavaScript code:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;?.&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;?.[]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;?.()&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2&gt;Motivation&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s start by looking at a real-world example in which optional chaining comes in handy. I&amp;#39;ve defined a &lt;code&gt;serializeJSON&lt;/code&gt; function that takes in any value and serializes it as JSON. I&amp;#39;m passing a user object with two properties to the function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any) {
  return JSON.stringify(value);
}

const user = {
  name: &amp;quot;Marius Schulz&amp;quot;,
  twitter: &amp;quot;mariusschulz&amp;quot;,
};

const json = serializeJSON(user);

console.log(json);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The program prints the following output to the console:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{&amp;quot;name&amp;quot;:&amp;quot;Marius Schulz&amp;quot;,&amp;quot;twitter&amp;quot;:&amp;quot;mariusschulz&amp;quot;}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now let&amp;#39;s say that we want to let callers of our function specify the indentation level. We&amp;#39;ll define a &lt;code&gt;SerializationOptions&lt;/code&gt; type and add an &lt;code&gt;options&lt;/code&gt; parameter to the &lt;code&gt;serializeJSON&lt;/code&gt; function. We&amp;#39;ll retrieve the indentation level from the &lt;code&gt;options.formatting.indent&lt;/code&gt; property:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type SerializationOptions = {
  formatting: {
    indent: number;
  };
};

function serializeJSON(value: any, options: SerializationOptions) {
  const indent = options.formatting.indent;
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can now specify an indentation level of two spaces when calling &lt;code&gt;serializeJSON&lt;/code&gt; like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const user = {
  name: &amp;quot;Marius Schulz&amp;quot;,
  twitter: &amp;quot;mariusschulz&amp;quot;,
};

const json = serializeJSON(user, {
  formatting: {
    indent: 2,
  },
});

console.log(json);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As we would expect, the resulting JSON is now indented with two spaces and broken across multiple lines:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;name&amp;quot;: &amp;quot;Marius Schulz&amp;quot;,
  &amp;quot;twitter&amp;quot;: &amp;quot;mariusschulz&amp;quot;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Typically, &lt;code&gt;options&lt;/code&gt; parameters like the one we introduced here are optional. Callers of the function may specify an options object, but they&amp;#39;re not required to. Let&amp;#39;s adjust our function signature accordingly and make the &lt;code&gt;options&lt;/code&gt; parameter optional by appending a question mark to the parameter name:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options.formatting.indent;
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Assuming we have the &lt;a href=&quot;/blog/non-nullable-types-in-typescript#strict-null-checking&quot;&gt;&lt;code&gt;--strictNullChecks&lt;/code&gt; option&lt;/a&gt; enabled in our TypeScript project (which is part of the &lt;a href=&quot;/blog/the-strict-compiler-option-in-typescript&quot;&gt;&lt;code&gt;--strict&lt;/code&gt; family of compiler options&lt;/a&gt;), TypeScript should now report the following type error in our &lt;code&gt;options.formatting.indent&lt;/code&gt; expression:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Object is possibly &amp;#39;undefined&amp;#39;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The &lt;code&gt;options&lt;/code&gt; parameter is optional, and as a result it might hold the value &lt;code&gt;undefined&lt;/code&gt;. We should first check whether &lt;code&gt;options&lt;/code&gt; holds the value &lt;code&gt;undefined&lt;/code&gt; before accessing &lt;code&gt;options.formatting&lt;/code&gt;, otherwise we risk getting an error at runtime:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options !== undefined
    ? options.formatting.indent
    : undefined;
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We could also use a slightly more generic null check instead that will check for both &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; — note that we&amp;#39;re deliberately using &lt;code&gt;!=&lt;/code&gt; instead of &lt;code&gt;!==&lt;/code&gt; in this case:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options != null
    ? options.formatting.indent
    : undefined;
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the type error goes away. We can call the &lt;code&gt;serializeJSON&lt;/code&gt; function and pass it an options object with an explicit indentation level:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const json = serializeJSON(user, {
  formatting: {
    indent: 2,
  },
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or we can call it without specifying an options object, in which case the &lt;code&gt;indent&lt;/code&gt; variable will hold the value &lt;code&gt;undefined&lt;/code&gt; and &lt;code&gt;JSON.stringify&lt;/code&gt; will use a default indentation level of zero:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const json = serializeJSON(user);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Both function calls above are type-correct. However, what if we also wanted to be able to call our &lt;code&gt;serializeJSON&lt;/code&gt; function like this?&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const json = serializeJSON(user, {});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is another common pattern you&amp;#39;ll see. Options objects tend to declare some or all of their properties as optional so that callers of the function can specify as many (or as few) options as needed. We need to make the &lt;code&gt;formatting&lt;/code&gt; property in our &lt;code&gt;SerializationOptions&lt;/code&gt; type optional in order to support this pattern:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type SerializationOptions = {
  formatting?: {
    indent: number;
  };
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice the question mark after the name of the &lt;code&gt;formatting&lt;/code&gt; property. Now the &lt;code&gt;serializeJSON(user, {})&lt;/code&gt; call is type-correct, but TypeScript reports another type error when accessing &lt;code&gt;options.formatting.indent&lt;/code&gt;:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Object is possibly &amp;#39;undefined&amp;#39;.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;We&amp;#39;ll need to add another null check here given that &lt;code&gt;options.formatting&lt;/code&gt; could now hold the value &lt;code&gt;undefined&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options != null
    ? options.formatting != null
      ? options.formatting.indent
      : undefined
    : undefined;
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This code is now type-correct, and it safely accesses the &lt;code&gt;options.formatting.indent&lt;/code&gt; property. These nested null checks are getting pretty unwieldy though, so let&amp;#39;s see how we can simplify this property access using the optional chaining operator.&lt;/p&gt;
&lt;h2&gt;The &lt;code&gt;?.&lt;/code&gt; Operator: Dot Notation&lt;/h2&gt;
&lt;p&gt;We can use the &lt;code&gt;?.&lt;/code&gt; operator to access &lt;code&gt;options.formatting.indent&lt;/code&gt; with checks for nullish values at every level of this property chain:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options?.formatting?.indent;
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;a href=&quot;https://tc39.es/ecma262/multipage/&quot;&gt;ECMAScript specification&lt;/a&gt; describes optional chaining as follows:&lt;/p&gt;
&lt;blockquote&gt;
&lt;p&gt;Optional chaining [is] a property access and function invocation operator that short-circuits if the value to access/invoke is nullish.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;p&gt;The JavaScript runtime evaluates the &lt;code&gt;options?.formatting?.indent&lt;/code&gt; expression as follows:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;options&lt;/code&gt; holds the value &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, produce the value &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise, if &lt;code&gt;options.formatting&lt;/code&gt; holds the value &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, produce the value &lt;code&gt;undefined&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Otherwise, produce the value of &lt;code&gt;options.formatting.indent&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Note that the &lt;code&gt;?.&lt;/code&gt; operator always produces the value &lt;code&gt;undefined&lt;/code&gt; when it stops descending into a property chain, even when it encounters the value &lt;code&gt;null&lt;/code&gt;. TypeScript models this behavior in its type system. In the following example, TypeScript infers the &lt;code&gt;indent&lt;/code&gt; local variable to be of type &lt;code&gt;number | undefined&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options?.formatting?.indent;
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Thanks to optional chaining, this code is a lot more succinct and just as type-safe as before.&lt;/p&gt;
&lt;h2&gt;The &lt;code&gt;?.[]&lt;/code&gt; Operator: Bracket Notation&lt;/h2&gt;
&lt;p&gt;Next, let&amp;#39;s now look at the &lt;code&gt;?.[]&lt;/code&gt; operator, another operator in the optional chaining family.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s say that our &lt;code&gt;indent&lt;/code&gt; property on the &lt;code&gt;SerializationOptions&lt;/code&gt; type was called &lt;code&gt;indent-level&lt;/code&gt; instead. We&amp;#39;ll need to use quotes to define a property that has a hyphen in its name:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type SerializationOptions = {
  formatting?: {
    &amp;quot;indent-level&amp;quot;: number;
  };
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We could now specify a value for the &lt;code&gt;indent-level&lt;/code&gt; property like this when calling the &lt;code&gt;serializeJSON&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const json = serializeJSON(user, {
  formatting: {
    &amp;quot;indent-level&amp;quot;: 2,
  },
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, the following attempt to access the &lt;code&gt;indent-level&lt;/code&gt; property using optional chaining is a syntax error:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const indent = options?.formatting?.&amp;quot;indent-level&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We cannot use the &lt;code&gt;?.&lt;/code&gt; operator directly followed by a string literal — that would be invalid syntax. Instead, we can use the bracket notation of optional chaining and access the &lt;code&gt;indent-level&lt;/code&gt; property using the &lt;code&gt;?.[]&lt;/code&gt; operator:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const indent = options?.formatting?.[&amp;quot;indent-level&amp;quot;];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&amp;#39;s our complete &lt;code&gt;serializeJSON&lt;/code&gt; function:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options?.formatting?.[&amp;quot;indent-level&amp;quot;];
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&amp;#39;s pretty much the same as before, aside from additional square brackets for the final property access.&lt;/p&gt;
&lt;h2&gt;The &lt;code&gt;?.()&lt;/code&gt; Operator: Method Calls&lt;/h2&gt;
&lt;p&gt;The third and final operator in the optional chaining family is &lt;code&gt;?.()&lt;/code&gt;. We can use the &lt;code&gt;?.()&lt;/code&gt; operator to invoke a method which may not exist.&lt;/p&gt;
&lt;p&gt;To see when this operator is useful, let&amp;#39;s change our &lt;code&gt;SerializationOptions&lt;/code&gt; type once again. We&amp;#39;ll replace the &lt;code&gt;indent&lt;/code&gt; property (typed as a number) by a &lt;code&gt;getIndent&lt;/code&gt; property (typed as a parameterless function returning a number):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type SerializationOptions = {
  formatting?: {
    getIndent?: () =&amp;gt; number;
  };
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can call our &lt;code&gt;serializeJSON&lt;/code&gt; function and specify an indentation level of two as follows:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const json = serializeJSON(user, {
  formatting: {
    getIndent: () =&amp;gt; 2,
  },
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To get the indentation level within our &lt;code&gt;serializeJSON&lt;/code&gt; function, we can use the &lt;code&gt;?.()&lt;/code&gt; operator to conditionally invoke the &lt;code&gt;getIndent&lt;/code&gt; method if (and only if) it is defined:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const indent = options?.formatting?.getIndent?.();
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the &lt;code&gt;getIndent&lt;/code&gt; method is not defined, no attempt will be made to invoke it. The entire property chain will evaluate to &lt;code&gt;undefined&lt;/code&gt; in that case, avoiding the infamous &amp;quot;getIndent is not a function&amp;quot; error.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s our complete &lt;code&gt;serializeJSON&lt;/code&gt; function once again:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function serializeJSON(value: any, options?: SerializationOptions) {
  const indent = options?.formatting?.getIndent?.();
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Compiling Optional Chaining to Older JavaScript&lt;/h2&gt;
&lt;p&gt;Now that we&amp;#39;ve seen how the optional chaining operators work and how they&amp;#39;re type-checked, let&amp;#39;s have a look at the compiled JavaScript which the TypeScript compiler emits when targeting older JavaScript versions.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s the JavaScript code that the TypeScript compiler will emit, with whitespace adjusted for readability:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function serializeJSON(value, options) {
  var _a, _b;
  var indent =
    (_b =
      (_a =
        options === null || options === void 0
          ? void 0
          : options.formatting) === null || _a === void 0
        ? void 0
        : _a.getIndent) === null || _b === void 0
      ? void 0
      : _b.call(_a);
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;There&amp;#39;s quite a lot going on in the assignment to the &lt;code&gt;indent&lt;/code&gt; variable. Let&amp;#39;s simplify the code step by step. We&amp;#39;ll start by renaming the local variables &lt;code&gt;_a&lt;/code&gt; and &lt;code&gt;_b&lt;/code&gt; to &lt;code&gt;formatting&lt;/code&gt; and &lt;code&gt;getIndent&lt;/code&gt;, respectively:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function serializeJSON(value, options) {
  var formatting, getIndent;
  var indent =
    (getIndent =
      (formatting =
        options === null || options === void 0
          ? void 0
          : options.formatting) === null || formatting === void 0
        ? void 0
        : formatting.getIndent) === null || getIndent === void 0
      ? void 0
      : getIndent.call(formatting);
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, let&amp;#39;s address the &lt;code&gt;void 0&lt;/code&gt; expression. The &lt;code&gt;void&lt;/code&gt; operator always produces the value &lt;code&gt;undefined&lt;/code&gt;, no matter what value it&amp;#39;s applied to. We can replace the &lt;code&gt;void 0&lt;/code&gt; expression by the value &lt;code&gt;undefined&lt;/code&gt; directly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function serializeJSON(value, options) {
  var formatting, getIndent;
  var indent =
    (getIndent =
      (formatting =
        options === null || options === undefined
          ? undefined
          : options.formatting) === null || formatting === undefined
        ? undefined
        : formatting.getIndent) === null || getIndent === undefined
      ? undefined
      : getIndent.call(formatting);
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, let&amp;#39;s extract the assignment to the &lt;code&gt;formatting&lt;/code&gt; variable into a separate statement:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function serializeJSON(value, options) {
  var formatting =
    options === null || options === undefined
      ? undefined
      : options.formatting;

  var getIndent;
  var indent =
    (getIndent =
      formatting === null || formatting === undefined
        ? undefined
        : formatting.getIndent) === null || getIndent === undefined
      ? undefined
      : getIndent.call(formatting);
  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&amp;#39;s do the same with the assignment to &lt;code&gt;getIndent&lt;/code&gt; and add some whitespace:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function serializeJSON(value, options) {
  var formatting =
    options === null || options === undefined
      ? undefined
      : options.formatting;

  var getIndent =
    formatting === null || formatting === undefined
      ? undefined
      : formatting.getIndent;

  var indent =
    getIndent === null || getIndent === undefined
      ? undefined
      : getIndent.call(formatting);

  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Lastly, let&amp;#39;s combine the checks using &lt;code&gt;===&lt;/code&gt; for the values &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; into a single check using the &lt;code&gt;==&lt;/code&gt; operator. Unless we&amp;#39;re dealing with the &lt;a href=&quot;/blog/nullish-coalescing-the-operator-in-typescript/#compiled-output-checking-for-null-and-undefined&quot;&gt;special &lt;code&gt;document.all&lt;/code&gt; value in our null checks&lt;/a&gt;, the two are equivalent:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;function serializeJSON(value, options) {
  var formatting = options == null
    ? undefined
    : options.formatting;

  var getIndent = formatting == null
    ? undefined
    : formatting.getIndent;

  var indent = getIndent == null
    ? undefined
    : getIndent.call(formatting);

  return JSON.stringify(value, null, indent);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now the structure of the code is a lot more apparent. You can see that TypeScript is emitting the null checks that we would have written ourselves if we hadn&amp;#39;t been able to use the optional chaining operators.&lt;/p&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:ecc53675-7a11-4ec1-b4e7-9668f67f60db</id>
  <title>How to Squash the First Two Commits in a Git Repository</title>
  <link href="https://mariusschulz.com/blog/how-to-squash-the-first-two-commits-in-a-git-repository" />
  <published>2020-11-01T00:00:00.000Z</published>
  <updated>2020-11-15T00:00:00.000Z</updated>
  <summary type="text">Git 1.7.12 introduced the --root flag for the rebase command that lets you rewrite all the history leading to a specific commit down to the root commit.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;I recently needed to squash the first two commits in one of my Git repositories. As usual, I ran the &lt;code&gt;git rebase -i&lt;/code&gt; command to do an interactive rebase, but I noticed that the root commit didn&amp;#39;t appear in the list of commits.&lt;/p&gt;
&lt;p&gt;Here&amp;#39;s what my Git history looked like:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ git log --graph --oneline
* fe2c946 (HEAD -&amp;gt; main) More changes
* 2702f8b Small tweaks
* ffb98dd Initial commit
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;When I ran &lt;code&gt;git rebase -i ffb98dd&lt;/code&gt;, this was the output I got (omitted for brevity):&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pick 2702f8b Small tweaks
pick fe2c946 More changes

# Rebase ffb98dd..fe2c946 onto ffb98dd (2 commands)
# ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As you can see, the second commmit &lt;code&gt;2702f8b&lt;/code&gt; and the third commit &lt;code&gt;fe2c946&lt;/code&gt; were listed, but the initial commit &lt;code&gt;ffb98dd&lt;/code&gt; wasn&amp;#39;t. So how do you squash the second commit into the root commit if the root commit isn&amp;#39;t listed?&lt;/p&gt;
&lt;p&gt;The solution for this problem was introduced in &lt;a href=&quot;https://github.com/git/git/blob/e2850a27a95c6f5b141dd88398b1702d2e524a81/Documentation/RelNotes/1.7.12.txt#L59-L60&quot;&gt;Git 1.17.12&lt;/a&gt;. We can now specify the &lt;code&gt;--root&lt;/code&gt; flag for the &lt;code&gt;rebase&lt;/code&gt; command to rebase all reachable commits up to the root:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ git rebase -i --root
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This allows us to rewrite the Git history down to the root commit. Now, the output includes the root commit &lt;code&gt;ffb98dd&lt;/code&gt; in the first line:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pick ffb98dd Initial commit
pick 2702f8b Small tweaks
pick fe2c946 More changes

# Rebase fe2c946 onto 6fafbe0 (3 commands)
# ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can use the &lt;code&gt;squash&lt;/code&gt; command in the second line to combine &lt;code&gt;ffb98dd&lt;/code&gt; and &lt;code&gt;2702f8b&lt;/code&gt; into a single commit:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;pick ffb98dd Initial commit
squash 2702f8b Small tweaks
pick fe2c946 More changes

# Rebase fe2c946 onto 6fafbe0 (3 commands)
# ...
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we need to choose a message for the new combined commit. I kept &amp;quot;Initial commit&amp;quot; since it&amp;#39;s still an accurate description. Once the command completes, the Git history looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;* bfd9495 (HEAD -&amp;gt; main) More changes
* 34901ec Initial commit
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And there we go! The changes made in the &amp;quot;Small tweaks&amp;quot; commit &lt;code&gt;2702f8b&lt;/code&gt; have been folded into our initial commit. Using the &lt;code&gt;--root&lt;/code&gt; option with the &lt;code&gt;rebase&lt;/code&gt; command, we were able to squash the first two commits into a single one.&lt;/p&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:6b7a17fe-9a6b-4749-863d-e21810336d24</id>
  <title>Nullish Coalescing: The ?? Operator in TypeScript</title>
  <link href="https://mariusschulz.com/blog/nullish-coalescing-the-operator-in-typescript" />
  <published>2020-08-06T00:00:00.000Z</published>
  <updated>2021-08-14T00:00:00.000Z</updated>
  <summary type="text">TypeScript 3.7 added support for the ?? operator, which is known as the nullish coalescing operator. We can use this operator to provide a fallback value for a value that might be null or undefined.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;TypeScript 3.7 added support for the &lt;code&gt;??&lt;/code&gt; operator, which is known as the &lt;strong&gt;nullish coalescing operator&lt;/strong&gt;. We can use this operator to provide a fallback value for a value that might be &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Truthy and Falsy Values in JavaScript&lt;/h2&gt;
&lt;p&gt;Before we dive into the &lt;code&gt;??&lt;/code&gt; operator, let&amp;#39;s recall that JavaScript values can either be &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/Truthy&quot;&gt;truthy&lt;/a&gt; or &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Glossary/Falsy&quot;&gt;falsy&lt;/a&gt;: when coerced to a Boolean, a value can either produce the value &lt;code&gt;true&lt;/code&gt; or &lt;code&gt;false&lt;/code&gt;. In JavaScript, the following values are considered to be falsy:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;false&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;-0&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;0n&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;NaN&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;&amp;quot;&amp;quot;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;null&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;undefined&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;All other JavaScript values will produce the value &lt;code&gt;true&lt;/code&gt; when coerced to a Boolean and are thus considered truthy.&lt;/p&gt;
&lt;h2&gt;Providing Fallback Values with the &lt;code&gt;??&lt;/code&gt; Operator&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;??&lt;/code&gt; operator can be used to provide a fallback value in case another value is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;. It takes two operands and is written like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;value ?? fallbackValue;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the left operand is &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, the &lt;code&gt;??&lt;/code&gt; expression evaluates to the right operand:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;null ?? &amp;quot;n/a&amp;quot;;
// &amp;quot;n/a&amp;quot;

undefined ?? &amp;quot;n/a&amp;quot;;
// &amp;quot;n/a&amp;quot;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Otherwise, the &lt;code&gt;??&lt;/code&gt; expression evaluates to the left operand:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;false ?? true;
// false

0 ?? 100;
// 0

&amp;quot;&amp;quot; ?? &amp;quot;n/a&amp;quot;;
// &amp;quot;&amp;quot;

NaN ?? 0;
// NaN
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that all left operands above are falsy values. If we had used &lt;a href=&quot;/blog/the-and-and-or-operators-in-javascript&quot;&gt;the &lt;code&gt;||&lt;/code&gt; operator&lt;/a&gt; instead of the &lt;code&gt;??&lt;/code&gt; operator, all of these expressions would&amp;#39;ve evaluated to their respective right operands:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;false || true;
// true

0 || 100;
// 100

&amp;quot;&amp;quot; || &amp;quot;n/a&amp;quot;;
// &amp;quot;n/a&amp;quot;

NaN || 0;
// 0
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This behavior is why you shouldn&amp;#39;t use the &lt;code&gt;||&lt;/code&gt; operator to provide a fallback value for a nullable value. For falsy values, the result might not be the one you wanted or expected. Consider this example:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type Options = {
  prettyPrint?: boolean;
};

function serializeJSON(value: unknown, options: Options): string {
  const prettyPrint = options.prettyPrint ?? true;
  // ...
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The expression &lt;code&gt;options.prettyPrint ?? true&lt;/code&gt; lets us provide the default value &lt;code&gt;true&lt;/code&gt; in case that the &lt;code&gt;prettyPrint&lt;/code&gt; property contains the value &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;. If &lt;code&gt;prettyPrint&lt;/code&gt; contains the value &lt;code&gt;false&lt;/code&gt;, the expression &lt;code&gt;false ?? true&lt;/code&gt; still evaluates to &lt;code&gt;false&lt;/code&gt;, which is exactly the behavior we want here.&lt;/p&gt;
&lt;p&gt;Note that using the &lt;code&gt;||&lt;/code&gt; operator here would lead to incorrect results. &lt;code&gt;options.prettyPrint || true&lt;/code&gt; would evaluate to &lt;code&gt;true&lt;/code&gt; for the values &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;, but also for the value &lt;code&gt;false&lt;/code&gt;. This would clearly not be intended. I&amp;#39;ve seen this happen in practice a handful of times, so make sure to keep this case in mind and use towards the &lt;code&gt;??&lt;/code&gt; operator instead.&lt;/p&gt;
&lt;h2&gt;Compiled Output: ES2020 and Newer&lt;/h2&gt;
&lt;p&gt;The nullish coalescing operator has reached Stage 4 (&amp;quot;Finished&amp;quot;) of &lt;a href=&quot;https://tc39.es/process-document/&quot;&gt;the TC39 process&lt;/a&gt; and is now &lt;a href=&quot;https://github.com/tc39/proposals/blob/master/finished-proposals.md&quot;&gt;officially part of ES2020&lt;/a&gt;. Therefore, the TypeScript compiler will emit the &lt;code&gt;??&lt;/code&gt; operator as is without any downleveling when you&amp;#39;re targeting &lt;code&gt;&amp;quot;ES2020&amp;quot;&lt;/code&gt; (or a newer language version) or &lt;code&gt;&amp;quot;ESNext&amp;quot;&lt;/code&gt; in your &lt;em&gt;tsconfig.json&lt;/em&gt; file:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-json&quot;&gt;{
  &amp;quot;compilerOptions&amp;quot;: {
    &amp;quot;strict&amp;quot;: true,
    &amp;quot;target&amp;quot;: &amp;quot;ES2020&amp;quot;
  }
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;So, this simple expression will be emitted unchanged:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;value ?? fallbackValue;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you&amp;#39;re planning on using the &lt;code&gt;??&lt;/code&gt; operator while targeting &lt;code&gt;&amp;quot;ES2020&amp;quot;&lt;/code&gt; or a newer language version, head over to &lt;a href=&quot;https://caniuse.com/#feat=mdn-javascript_operators_nullish_coalescing&quot;&gt;caniuse.com&lt;/a&gt; and &lt;a href=&quot;https://node.green/#ES2020-features--nullish-coalescing-operator-----&quot;&gt;node.green&lt;/a&gt; and make sure that all the JavaScript engines you need to support have implemented the operator.&lt;/p&gt;
&lt;h2&gt;Compiled JavaScript Output: ES2019 and Older&lt;/h2&gt;
&lt;p&gt;If you&amp;#39;re targeting &lt;code&gt;&amp;quot;ES2019&amp;quot;&lt;/code&gt; or an older language version in your &lt;em&gt;tsconfig.json&lt;/em&gt; file, the TypeScript compiler will rewrite the nullish coalescing operator into a conditional expression. That way, we can start using the &lt;code&gt;??&lt;/code&gt; operator in our code today and still have the compiled code successfully parse and execute in older JavaScript engines.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s look at the same simple &lt;code&gt;??&lt;/code&gt; expression again:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;value ?? fallbackValue;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Assuming we&amp;#39;re targeting &lt;code&gt;&amp;quot;ES2019&amp;quot;&lt;/code&gt; or a lower language version, the TypeScript compiler will emit the following JavaScript code:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;value !== null &amp;amp;&amp;amp; value !== void 0 ? value : fallbackValue;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;value&lt;/code&gt; variable is compared against both &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt; (the result of the expression &lt;code&gt;void 0&lt;/code&gt;). If both comparisons produce the value &lt;code&gt;false&lt;/code&gt;, the entire expression evaluates to &lt;code&gt;value&lt;/code&gt;; otherwise, it evaluates to &lt;code&gt;fallbackValue&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now, let&amp;#39;s look at a slightly more complex example. Instead of a simple &lt;code&gt;value&lt;/code&gt; variable, we&amp;#39;re going to use a &lt;code&gt;getValue()&lt;/code&gt; call expression as the left operand of the &lt;code&gt;??&lt;/code&gt; operator:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const value = getValue() ?? fallbackValue;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In this case, the compiler will emit the following JavaScript code (modulo whitespace differences):&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;var _a;
const value = (_a = getValue()) !== null &amp;amp;&amp;amp; _a !== void 0
  ? _a
  : fallbackValue;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;You can see that the compiler generated an intermediate variable &lt;code&gt;_a&lt;/code&gt; to store the return value of the &lt;code&gt;getValue()&lt;/code&gt; call. The &lt;code&gt;_a&lt;/code&gt; variable is then compared against &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;void 0&lt;/code&gt; and (potentially) used as the resulting value of the entire expression. This intermediate variable is necessary so that the &lt;code&gt;getValue&lt;/code&gt; function is only called once.&lt;/p&gt;
&lt;h2&gt;Compiled Output: Checking for &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;You might be wondering why the compiler emits the following expression to check the &lt;code&gt;value&lt;/code&gt; variable against &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;value !== null &amp;amp;&amp;amp; value !== void 0;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Couldn&amp;#39;t the compiler emit the following shorter check instead?&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;value != null;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Unfortunately, it can&amp;#39;t do that without sacrificing correctness. For almost all values in JavaScript, the comparison &lt;code&gt;value == null&lt;/code&gt; is equivalent to &lt;code&gt;value === null || value === undefined&lt;/code&gt;. For those values, the negation &lt;code&gt;value != null&lt;/code&gt; is equivalent to &lt;code&gt;value !== null &amp;amp;&amp;amp; value !== undefined&lt;/code&gt;. However, there is one value for which these two checks aren&amp;#39;t equivalent, and that value is &lt;code&gt;document.all&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;document.all === null;
// false

document.all === undefined;
// false

document.all == null;
// true

document.all == undefined;
// true
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The value &lt;code&gt;document.all&lt;/code&gt; is not considered to be strictly equal to either &lt;code&gt;null&lt;/code&gt; or &lt;code&gt;undefined&lt;/code&gt;, but it is considered to be loosely equal to both &lt;code&gt;null&lt;/code&gt; and &lt;code&gt;undefined&lt;/code&gt;. Because of this anomaly, the TypeScript compiler can&amp;#39;t emit &lt;code&gt;value != null&lt;/code&gt; as a check because it would produce incorrect results for &lt;code&gt;document.all&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;You can read more about this curious behavior in an answer to the &lt;a href=&quot;https://stackoverflow.com/a/10394873/362634&quot;&gt;Why is document.all falsy?&lt;/a&gt; question on Stack Overflow. Oh, the things we do for web compatibility.&lt;/p&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:94d82e13-cdfe-4efe-ab22-20c6fbeef566</id>
  <title>Declaring Global Variables in TypeScript</title>
  <link href="https://mariusschulz.com/blog/declaring-global-variables-in-typescript" />
  <published>2020-04-14T00:00:00.000Z</published>
  <updated>2020-04-16T00:00:00.000Z</updated>
  <summary type="text">Different approaches for declaring a global variable in TypeScript.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;Every now and then, you might want to statically type a global variable in TypeScript. For example, in some of my web applications, I need to pass a few properties from my markup rendered on the server to my JavaScript code running in the browser. To do that, I typically define a global variable named &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; within an inline script and assign to it a JSON-serialized object:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-html&quot;&gt;&amp;lt;script&amp;gt;
  window.__INITIAL_DATA__ = {
    userID: &amp;quot;536891193569405430&amp;quot;,
  };
&amp;lt;/script&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, if I try to access &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt; in a TypeScript file, the compiler will produce a type error because it can&amp;#39;t find a definition of the &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; property anywhere:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Property &amp;#39;__INITIAL_DATA__&amp;#39; does not exist
// on type &amp;#39;Window &amp;amp; typeof globalThis&amp;#39;
const initialData = window.__INITIAL_DATA__;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;I&amp;#39;m going to show you a few different approaches for letting TypeScript know about the &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt; property and making the type error go away.&lt;/p&gt;
&lt;h2&gt;Using a Type Assertion&lt;/h2&gt;
&lt;p&gt;The quickest way to make the type error go away is to use the &lt;code&gt;any&lt;/code&gt; type in a type assertion. We can treat the &lt;code&gt;window&lt;/code&gt; object to be of type &lt;code&gt;any&lt;/code&gt; so that we can access its &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; property:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const initialData = (window as any).__INITIAL_DATA__;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This solution works, and we longer get a type error. This is a pragmatic approach if you need an ad-hoc way to access a property on the &lt;code&gt;window&lt;/code&gt; object that TypeScript doesn&amp;#39;t know about.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;(window as any).__INITIAL_DATA__&lt;/code&gt; expression is of type &lt;code&gt;any&lt;/code&gt;, and therefore &lt;code&gt;initialData&lt;/code&gt; is of type &lt;code&gt;any&lt;/code&gt; too. We could go one step further and use another type assertion to give the &lt;code&gt;initialData&lt;/code&gt; variable a more specific type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type InitialData = {
  userID: string;
};

const initialData = (window as any).__INITIAL_DATA__ as InitialData;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we can access &lt;code&gt;initialData.userID&lt;/code&gt; in a type-safe way:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const userID = initialData.userID; // Type string
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Do keep in mind that this is not a guarantee that &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt; will be set correctly at runtime. The type checker trusts us and it is our job to make sure that we assign an object with the expected shape to &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Declare a Global Variable&lt;/h2&gt;
&lt;p&gt;Another approach is to declare a global variable using the &lt;code&gt;declare var&lt;/code&gt; syntax. This way, we can let TypeScript know that it can expect to find a global variable with the given name and type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;declare var __INITIAL_DATA__: InitialData;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can now access the &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; variable directly …&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const initialData = __INITIAL_DATA__;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;… or off of the &lt;code&gt;window&lt;/code&gt; object:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const initialData = window.__INITIAL_DATA__;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the access via &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt; will not work from within an &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules&quot;&gt;ECMAScript module&lt;/a&gt;. If your JavaScript file contains top-level &lt;code&gt;import&lt;/code&gt; or &lt;code&gt;export&lt;/code&gt; declarations, it is considered a module, and you will receive a type error if you try to access the &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; on the &lt;code&gt;window&lt;/code&gt; object.&lt;/p&gt;
&lt;p&gt;You can declare a global variable in the global scope by using the &lt;code&gt;declare global { ... }&lt;/code&gt; syntax to be able to access both &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt; as well as &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; directly within a JavaScript module.:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;export function someExportedFunction() {
  // ...
}

declare global {
  var __INITIAL_DATA__: InitialData;
}

const initialData = window.__INITIAL_DATA__;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you need to access &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt; in several files or modules, it might be a good idea to create a &lt;em&gt;globals.d.ts&lt;/em&gt; file in your project. In that file, you can declare all global variables you&amp;#39;ll use:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;declare var __INITIAL_DATA__: InitialData;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;As long as &lt;em&gt;globals.d.ts&lt;/em&gt; is part of your TypeScript project, the compiler will know that &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; is a global variable, and it will let you access it via both &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; as well as &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;Augmenting the Window Interface&lt;/h2&gt;
&lt;p&gt;Lastly, you can use TypeScript&amp;#39;s &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/declaration-merging.html#merging-interfaces&quot;&gt;interface declaration merging&lt;/a&gt; to let the compiler know that it can expect to find a property named &lt;code&gt;__INITIAL_DATA__&lt;/code&gt; on the &lt;code&gt;Window&lt;/code&gt; type and therefore the &lt;code&gt;window&lt;/code&gt; object. To do that, you&amp;#39;ll need to define an interface named &lt;code&gt;Window&lt;/code&gt; with a property named &lt;code&gt;__INITIAL_DATA__&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;interface Window {
  __INITIAL_DATA__: InitialData;
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;TypeScript will merge this interface definition together with the &lt;code&gt;Window&lt;/code&gt; interface defined in &lt;em&gt;lib.dom.d.ts&lt;/em&gt;, resulting in a single &lt;code&gt;Window&lt;/code&gt; type. Now, the following assignment will no longer produce a type error:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const initialData = window.__INITIAL_DATA__;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that once again, this approach will not work within a JavaScript module. You&amp;#39;ll need to use the &lt;code&gt;declare global { ... }&lt;/code&gt; syntax again in order for the &lt;code&gt;window.__INITIAL_DATA__&lt;/code&gt; expression to type-check correctly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;export function someExportedFunction() {
  // ...
}

declare global {
  interface Window {
    __INITIAL_DATA__: InitialData;
  }
}

const initialData = window.__INITIAL_DATA__;
&lt;/code&gt;&lt;/pre&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:e3c28813-f8e0-4f3b-a6a5-41251f66f984</id>
  <title>Concatenating Arrays in JavaScript</title>
  <link href="https://mariusschulz.com/blog/concatenating-arrays-in-javascript" />
  <published>2020-03-31T00:00:00.000Z</published>
  <updated>2021-05-01T00:00:00.000Z</updated>
  <summary type="text">In JavaScript, there are different approaches to concatenating multiple arrays into a single one. A comparison of push(), concat(), and spread syntax.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;It&amp;#39;s a common task to concatenate multiple arrays into a single one. In JavaScript, there are several different approaches we can take. Some of them mutate the target array; others leave all input arrays unchanged and return a new array instead.&lt;/p&gt;
&lt;p&gt;In this post, I want to compare the following common approaches:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Appending elements to an existing array with &lt;code&gt;Array.prototype.push()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Appending elements to a new array with &lt;code&gt;Array.prototype.push()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Concatenating multiple arrays with &lt;code&gt;Array.prototype.concat()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Using spread syntax in an array literal&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Let&amp;#39;s take a look.&lt;/p&gt;
&lt;h2&gt;Appending Elements to an Existing Array with &lt;code&gt;Array.prototype.push()&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;First up, the good old &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push&quot;&gt;&lt;code&gt;Array.prototype.push()&lt;/code&gt;&lt;/a&gt; method. Let&amp;#39;s assume we have the following two arrays:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can append all elements of &lt;code&gt;array2&lt;/code&gt; to &lt;code&gt;array1&lt;/code&gt; by looping over &lt;code&gt;array2&lt;/code&gt; and calling &lt;code&gt;array1.push()&lt;/code&gt; repeatedly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;for (const element of array2) {
  array1.push(element);
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once that code has run, &lt;code&gt;array1&lt;/code&gt; now contains all six values; it has been modified in place. &lt;code&gt;array2&lt;/code&gt;, on the other hand, remains unchanged:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3, 4, 5, 6]
array2; // [4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of the &lt;code&gt;for...of&lt;/code&gt; loop, we could&amp;#39;ve passed all elements of &lt;code&gt;array2&lt;/code&gt; as arguments to the &lt;code&gt;push()&lt;/code&gt; method call using &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax&quot;&gt;spread syntax&lt;/a&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1.push(...array2);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is equivalent to the following method call:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1.push(4, 5, 6);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result is the same in all cases. &lt;code&gt;array1&lt;/code&gt; now contains all six values, &lt;code&gt;array2&lt;/code&gt; remains unchanged:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3, 4, 5, 6]
array2; // [4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sometimes, mutating the target array might not be the desired behavior. This is particularly important when you&amp;#39;re writing your code in a functional style, composing &lt;a href=&quot;https://en.wikipedia.org/wiki/Pure_function&quot;&gt;pure functions&lt;/a&gt; that don&amp;#39;t have side effects. Those functions should not modify any of their parameters; you therefore shouldn&amp;#39;t call the &lt;code&gt;push()&lt;/code&gt; method on an array that was passed to the function as a parameter.&lt;/p&gt;
&lt;h2&gt;Appending Elements to a New Array with &lt;code&gt;Array.prototype.push()&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s take a look at an approach that uses the &lt;code&gt;push()&lt;/code&gt; method without mutating any of the arrays that we want to concatenate. Here are our two input arrays again:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Instead of appending the elements of &lt;code&gt;array2&lt;/code&gt; to &lt;code&gt;array1&lt;/code&gt;, we can create a new empty array and push the elements of both &lt;code&gt;array1&lt;/code&gt; and &lt;code&gt;array2&lt;/code&gt; into that:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const concatenated = [];
concatenated.push(...array1);
concatenated.push(...array2);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&amp;#39;s what the three arrays look like after the above code has finished executing:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3]
array2; // [4, 5, 6]
concatenated; // [1, 2, 3, 4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;push()&lt;/code&gt; is still a mutating method, but in this case, it only mutated &lt;code&gt;concatenated&lt;/code&gt;, leaving &lt;code&gt;array1&lt;/code&gt; and &lt;code&gt;array2&lt;/code&gt; unchanged. This approach is fine even within a pure function that takes &lt;code&gt;array1&lt;/code&gt; and &lt;code&gt;array2&lt;/code&gt; as parameters. As long as the function is returning the same value when given the same parameters and it doesn&amp;#39;t have any side effects (such as modifying its parameters), it is still considered a pure function, even if we locally use mutation within the function body.&lt;/p&gt;
&lt;h2&gt;Concatenating Multiple Arrays with &lt;code&gt;Array.prototype.concat()&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Now that we&amp;#39;ve seen how to work with the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push&quot;&gt;&lt;code&gt;Array.prototype.push()&lt;/code&gt;&lt;/a&gt; method, which mutates the target array, let&amp;#39;s take a look at &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/concat&quot;&gt;&lt;code&gt;Array.prototype.concat()&lt;/code&gt;&lt;/a&gt;, a non-mutating method. We&amp;#39;ll start out with the same two arrays again:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&amp;#39;ll then call the &lt;code&gt;concat()&lt;/code&gt; method on &lt;code&gt;array1&lt;/code&gt;, passing &lt;code&gt;array2&lt;/code&gt; as a parameter:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const concatenated = array1.concat(array2);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since the &lt;code&gt;concat()&lt;/code&gt; method is non-mutating, it neither modifies &lt;code&gt;array1&lt;/code&gt; nor &lt;code&gt;array2&lt;/code&gt;. Instead, it returns a new array that contains all elements of &lt;code&gt;array1&lt;/code&gt; and &lt;code&gt;array2&lt;/code&gt; concatenated together:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3]
array2; // [4, 5, 6]
concatenated; // [1, 2, 3, 4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Just like the &lt;code&gt;push()&lt;/code&gt; method, the &lt;code&gt;concat()&lt;/code&gt; method accepts arbitrarily many arguments. That&amp;#39;s useful if you want to concatenate three or more arrays together:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const array3 = [7, 8, 9];
const concatenated = array1.concat(array2, array3);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And just like before, all input arrays remain unchanged:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3]
array2; // [4, 5, 6]
array3; // [7, 8, 9]
concatenated; // [1, 2, 3, 4, 5, 6, 7, 8, 9]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Sometimes, you might not know upfront how many arrays you want to concatenate. Let&amp;#39;s say we have an array of arrays that we want to concatenate into a single one:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const array3 = [7, 8, 9];
const arrays = [array1, array2, array3];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using spread syntax again, we can spread all elements of &lt;code&gt;arrays&lt;/code&gt; as arguments into the &lt;code&gt;concat()&lt;/code&gt; method call:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const concatenated = [].concat(...arrays);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that we&amp;#39;re creating an empty array here so that we can call the &lt;code&gt;concat()&lt;/code&gt; method on it. Since it doesn&amp;#39;t contain any elements, the empty array doesn&amp;#39;t change the resulting concatenated array. And as before, all input arrays remain unchanged:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3]
array2; // [4, 5, 6]
array3; // [7, 8, 9]
arrays; // [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
concatenated; // [1, 2, 3, 4, 5, 6, 7, 8, 9]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the &lt;code&gt;concat()&lt;/code&gt; method doesn&amp;#39;t recursively flatten arrays. It concatenates all elements in all of its arrays without unwrapping nested arrays:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, [2], 3];
const array2 = [4, [5, [6]]];
const concatenated = array1.concat(array2);
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The resulting &lt;code&gt;concatenated&lt;/code&gt; array contains the three elements of &lt;code&gt;array1&lt;/code&gt;, followed by the two elements of &lt;code&gt;array2&lt;/code&gt;, totaling five elements:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;concatenated; // [1, [2], 3, 4, [5, [6]]]
concatenated.length; // 5
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Using Spread Syntax in an Array Literal&lt;/h2&gt;
&lt;p&gt;Lastly, let&amp;#39;s look at &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax#Spread_in_array_literals&quot;&gt;spread syntax in array literals&lt;/a&gt;. Just like before, we&amp;#39;ll assume we have two input arrays that we want to concatenate:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Using spread syntax in an array literal, we can create a new array that contains all elements of &lt;code&gt;array1&lt;/code&gt;, followed by all elements of &lt;code&gt;array2&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const concatenated = [...array1, ...array2];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And once again, we can see that neither &lt;code&gt;array1&lt;/code&gt; nor &lt;code&gt;array2&lt;/code&gt; has been modified:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3]
array2; // [4, 5, 6]
concatenated; // [1, 2, 3, 4, 5, 6]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The great thing about spread syntax is that it invokes the &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols&quot;&gt;iteration protocol&lt;/a&gt; of the element that we&amp;#39;re spreading. This means that spreading works with any iterable, rather than only with arrays. For example, we could spread all values in a &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set&quot;&gt;&lt;code&gt;Set&lt;/code&gt;&lt;/a&gt; into a new array:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const uniques = new Set([1, 2, 2, 3, 3, 3]);
const array = [...uniques];

uniques; // Set (3) {1, 2, 3}
array; // [1, 2, 3]
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is useful when you want to concatenate multiple arrays into a single one and remove any duplicate values:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;const array1 = [1, 2, 3];
const array2 = [2, 3, 4];
const uniques = [...new Set([...array1, ...array2])];
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We&amp;#39;re concatenating &lt;code&gt;array1&lt;/code&gt; and &lt;code&gt;array2&lt;/code&gt; into a new array that contains all six elements: &lt;code&gt;[1, 2, 3, 2, 3, 4]&lt;/code&gt;. That new array is passed to the &lt;code&gt;Set&lt;/code&gt; constructor. Set can&amp;#39;t contain duplicate values, so when we spread the set into the outer new array, we end up with four unique values:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;array1; // [1, 2, 3]
array2; // [2, 3, 4]
uniques; // [1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Summary&lt;/h2&gt;
&lt;p&gt;We&amp;#39;ve seen different approaches to concatenate multiple arrays into a single one:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Using the &lt;code&gt;Array.prototype.push()&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;Using the &lt;code&gt;Array.prototype.concat()&lt;/code&gt; method&lt;/li&gt;
&lt;li&gt;Using spread syntax in array literals&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Most importantly, you should remember that the &lt;code&gt;push()&lt;/code&gt; mutates the target array, modifying it in place. The &lt;code&gt;concat()&lt;/code&gt; method and spread syntax in array literals, on the other hand, are non-mutating; both approaches create a new array instead.&lt;/p&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:11a262db-bb6c-42dc-83db-03bcf97b23ec</id>
  <title>The Omit Helper Type in TypeScript</title>
  <link href="https://mariusschulz.com/blog/the-omit-helper-type-in-typescript" />
  <published>2020-03-28T00:00:00.000Z</published>
  <updated>2020-08-30T00:00:00.000Z</updated>
  <summary type="text">TypeScript 3.5 added an Omit&lt;T, K&gt; helper type which lets us create an object type that omits specific properties from another object type.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;In version 3.5, TypeScript added an &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; helper type to the &lt;em&gt;lib.es5.d.ts&lt;/em&gt; type definition file that ships as part of the TypeScript compiler. The &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; type lets us create an object type that omits specific properties from another object type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type User = {
  id: string;
  name: string;
  email: string;
};

type UserWithoutEmail = Omit&amp;lt;User, &amp;quot;email&amp;quot;&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = {
  id: string;
  name: string;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; helper type is defined in &lt;em&gt;lib.es5.d.ts&lt;/em&gt; like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;/**
 * Construct a type with the properties of T except for those in type K.
 */
type Omit&amp;lt;T, K extends keyof any&amp;gt; = Pick&amp;lt;T, Exclude&amp;lt;keyof T, K&amp;gt;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To untangle this type definition and understand how it works, let&amp;#39;s see how we could&amp;#39;ve come up with our own version of the &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; helper type ourselves.&lt;/p&gt;
&lt;h2&gt;Defining the &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; Helper Type&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s start with the same &lt;code&gt;User&lt;/code&gt; type we&amp;#39;ve seen above:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type User = {
  id: string;
  name: string;
  email: string;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;First, we need to be able to retrieve all keys of the &lt;code&gt;User&lt;/code&gt; type. We can use the &lt;a href=&quot;/blog/keyof-and-lookup-types-in-typescript&quot;&gt;&lt;code&gt;keyof&lt;/code&gt; operator&lt;/a&gt; to retrieve a union of &lt;a href=&quot;/blog/string-literal-types-in-typescript&quot;&gt;string literal types&lt;/a&gt; that contains all property keys of this object type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type UserKeys = keyof User;

// This is equivalent to:
type UserKeys = &amp;quot;id&amp;quot; | &amp;quot;name&amp;quot; | &amp;quot;email&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Next, we need to be able to exclude a specific string literal type from a union of string literal types. In the case of our &lt;code&gt;User&lt;/code&gt; type, we want to exclude the type &lt;code&gt;&amp;quot;email&amp;quot;&lt;/code&gt; from the union &lt;code&gt;&amp;quot;id&amp;quot; | &amp;quot;name&amp;quot; | &amp;quot;email&amp;quot;&lt;/code&gt;. We can use the &lt;code&gt;Exclude&amp;lt;T, U&amp;gt;&lt;/code&gt; helper type to do that:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type UserKeysWithoutEmail = Exclude&amp;lt;UserKeys, &amp;quot;email&amp;quot;&amp;gt;;

// This is equivalent to:
type UserKeysWithoutEmail = Exclude&amp;lt;&amp;quot;id&amp;quot; | &amp;quot;name&amp;quot; | &amp;quot;email&amp;quot;, &amp;quot;email&amp;quot;&amp;gt;;

// This is equivalent to:
type UserKeysWithoutEmail = &amp;quot;id&amp;quot; | &amp;quot;name&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Exclude&amp;lt;T, U&amp;gt;&lt;/code&gt; type is defined in &lt;em&gt;lib.es5.d.ts&lt;/em&gt; like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;/**
 * Exclude from T those types that are assignable to U
 */
type Exclude&amp;lt;T, U&amp;gt; = T extends U ? never : T;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&amp;#39;s using a &lt;a href=&quot;/blog/conditional-types-in-typescript&quot;&gt;conditional type&lt;/a&gt; and the &lt;a href=&quot;/blog/the-never-type-in-typescript&quot;&gt;&lt;code&gt;never&lt;/code&gt; type&lt;/a&gt;. Using the &lt;code&gt;Exclude&amp;lt;T, U&amp;gt;&lt;/code&gt; helper type, we&amp;#39;re removing those types in our union type &lt;code&gt;&amp;quot;id&amp;quot; | &amp;quot;name&amp;quot; | &amp;quot;email&amp;quot;&lt;/code&gt; that are assignable to the &lt;code&gt;&amp;quot;email&amp;quot;&lt;/code&gt; type. That is only true for the string literal type &lt;code&gt;&amp;quot;email&amp;quot;&lt;/code&gt; itself, so we&amp;#39;re left with the union type &lt;code&gt;&amp;quot;id | &amp;quot;name&amp;quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Finally, we need to create an object type that contains a subset of the properties of our &lt;code&gt;User&lt;/code&gt; type. Specifically, we want to create an object type that contains only those properties whose keys are found in the &lt;code&gt;UserKeysWithoutEmail&lt;/code&gt; union type. We can use the &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt; helper type to pick those properties off of our &lt;code&gt;User&lt;/code&gt; type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type UserWithoutEmail = Pick&amp;lt;User, UserKeysWithoutEmail&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = Pick&amp;lt;User, &amp;quot;id&amp;quot; | &amp;quot;name&amp;quot;&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = {
  id: string;
  name: string;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Here&amp;#39;s how the &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt; helper type is defined within &lt;em&gt;lib.es5.d.ts&lt;/em&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;/**
 * From T, pick a set of properties whose keys are in the union K
 */
type Pick&amp;lt;T, K extends keyof T&amp;gt; = {
  [P in K]: T[P];
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt; type is a &lt;a href=&quot;/blog/mapped-types-in-typescript&quot;&gt;mapped type&lt;/a&gt; that&amp;#39;s using the &lt;code&gt;keyof&lt;/code&gt; operator and an &lt;a href=&quot;/blog/keyof-and-lookup-types-in-typescript#indexed-access-types&quot;&gt;indexed access type&lt;/a&gt; &lt;code&gt;T[P]&lt;/code&gt; to retrieve the type of the property &lt;code&gt;P&lt;/code&gt; in the object type &lt;code&gt;T&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Now, let&amp;#39;s summarize all the type operations we&amp;#39;ve performed using &lt;code&gt;keyof&lt;/code&gt;, &lt;code&gt;Exclude&amp;lt;T, U&amp;gt;&lt;/code&gt;, and &lt;code&gt;Pick&amp;lt;T, K&amp;gt;&lt;/code&gt; in a single type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type UserWithoutEmail = Pick&amp;lt;User, Exclude&amp;lt;keyof User, &amp;quot;email&amp;quot;&amp;gt;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that this type is specific to our &lt;code&gt;User&lt;/code&gt; type. Let&amp;#39;s make this a generic type so we can reuse it in other places:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type Omit&amp;lt;T, K&amp;gt; = Pick&amp;lt;T, Exclude&amp;lt;keyof T, K&amp;gt;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can now use this type to compute our &lt;code&gt;UserWithoutEmail&lt;/code&gt; type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type UserWithoutEmail = Omit&amp;lt;User, &amp;quot;email&amp;quot;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since object keys can only be strings, numbers, or symbols, we can add a generic constraint to the type parameter &lt;code&gt;K&lt;/code&gt; of our &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; helper type to only allow types &lt;code&gt;string&lt;/code&gt;, &lt;code&gt;number&lt;/code&gt;, or &lt;code&gt;symbol&lt;/code&gt; for keys:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type Omit&amp;lt;T, K extends string | number | symbol&amp;gt; = Pick&amp;lt;T, Exclude&amp;lt;keyof T, K&amp;gt;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The generic constraint &lt;code&gt;extends string | number | symbol&lt;/code&gt; is a bit verbose. We can replace the &lt;code&gt;string | number | symbol&lt;/code&gt; union type by the &lt;code&gt;keyof any&lt;/code&gt; type since the two are equivalent:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type Omit&amp;lt;T, K extends keyof any&amp;gt; = Pick&amp;lt;T, Exclude&amp;lt;keyof T, K&amp;gt;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And there we go! We&amp;#39;ve arrived at the exact definition of the &lt;code&gt;Omit&amp;lt;T, K&amp;gt;&lt;/code&gt; helper type as it is found within the &lt;em&gt;lib.es5.d.ts&lt;/em&gt; type definition file:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;/**
 * Construct a type with the properties of T except for those in type K.
 */
type Omit&amp;lt;T, K extends keyof any&amp;gt; = Pick&amp;lt;T, Exclude&amp;lt;keyof T, K&amp;gt;&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Unrolling &lt;code&gt;Omit&amp;lt;User, &amp;quot;email&amp;quot;&amp;gt;&lt;/code&gt;&lt;/h2&gt;
&lt;p&gt;Here&amp;#39;s a step-by-step evaluation of the &lt;code&gt;Omit&amp;lt;User, &amp;quot;email&amp;quot;&amp;gt;&lt;/code&gt; type. Try to follow every step to understand how TypeScript is computing the final type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;type User = {
  id: string;
  name: string;
  email: string;
};

type UserWithoutEmail = Omit&amp;lt;User, &amp;quot;email&amp;quot;&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = Pick&amp;lt;User, Exclude&amp;lt;keyof User, &amp;quot;email&amp;quot;&amp;gt;&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = Pick&amp;lt;User, Exclude&amp;lt;&amp;quot;id&amp;quot; | &amp;quot;name&amp;quot; | &amp;quot;email&amp;quot;, &amp;quot;email&amp;quot;&amp;gt;&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = Pick&amp;lt;
  User,
  | (&amp;quot;id&amp;quot; extends &amp;quot;email&amp;quot; ? never : &amp;quot;id&amp;quot;)
  | (&amp;quot;name&amp;quot; extends &amp;quot;email&amp;quot; ? never : &amp;quot;name&amp;quot;)
  | (&amp;quot;email&amp;quot; extends &amp;quot;email&amp;quot; ? never : &amp;quot;email&amp;quot;)
&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = Pick&amp;lt;User, &amp;quot;id&amp;quot; | &amp;quot;name&amp;quot; | never&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = Pick&amp;lt;User, &amp;quot;id&amp;quot; | &amp;quot;name&amp;quot;&amp;gt;;

// This is equivalent to:
type UserWithoutEmail = {
  [P in &amp;quot;id&amp;quot; | &amp;quot;name&amp;quot;]: User[P];
};

// This is equivalent to:
type UserWithoutEmail = {
  id: User[&amp;quot;id&amp;quot;];
  name: User[&amp;quot;name&amp;quot;];
};

// This is equivalent to:
type UserWithoutEmail = {
  id: string;
  name: string;
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Et voilà, our final &lt;code&gt;UserWithoutEmail&lt;/code&gt; type.&lt;/p&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:11a77245-32cc-4cb5-a67b-1bb229b3ec05</id>
  <title>Fast Searching with ripgrep</title>
  <link href="https://mariusschulz.com/blog/fast-searching-with-ripgrep" />
  <published>2020-03-19T00:00:00.000Z</published>
  <updated>2020-04-16T00:00:00.000Z</updated>
  <summary type="text">In this post, I want to introduce you to ripgrep, a smart and fast command line search tool that I find myself using all the time when programming.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;In this post, I want to introduce you to &lt;a href=&quot;https://github.com/BurntSushi/ripgrep&quot;&gt;ripgrep&lt;/a&gt;, a smart and fast command line search tool that I find myself using all the time when programming. ripgrep recursively searches directories for a regex pattern and outputs all matches that it finds.&lt;/p&gt;
&lt;h2&gt;Why ripgrep?&lt;/h2&gt;
&lt;p&gt;So what makes ripgrep so great? After all, there are plenty of other search tools out there already, like &lt;a href=&quot;https://www.gnu.org/savannah-checkouts/gnu/grep/&quot;&gt;grep&lt;/a&gt;, &lt;a href=&quot;https://beyondgrep.com/&quot;&gt;ack&lt;/a&gt;, or &lt;a href=&quot;https://github.com/ggreer/the_silver_searcher&quot;&gt;The Silver Searcher&lt;/a&gt;. For me, it boils down to the following reasons:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;ripgrep is smart.&lt;/strong&gt; It picks sensible defaults out of the box. I like that! For example, ripgrep respects &lt;code&gt;.gitignore&lt;/code&gt; files and skips matching files and directories by default. It also ignores binary files, skips hidden files and directories, and doesn&amp;#39;t follow symbolic links.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ripgrep is fast.&lt;/strong&gt; In fact, it&amp;#39;s &lt;em&gt;very&lt;/em&gt; fast. I&amp;#39;ve thrown hundreds of thousands of files at it and didn&amp;#39;t encounter any performance issues. Check out &lt;a href=&quot;https://blog.burntsushi.net/ripgrep/&quot;&gt;ripgrep is faster than {grep, ag, git grep, ucg, pt, sift}&lt;/a&gt; for a detailed analysis and various performance benchmarks.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;ripgrep also has full Unicode support, can search compressed files, and optionally lets you switch its regex engine to use &lt;a href=&quot;https://www.pcre.org/current/doc/html/pcre2syntax.html&quot;&gt;PCRE2 regular expressions&lt;/a&gt;.&lt;/p&gt;
&lt;h2&gt;Installation&lt;/h2&gt;
&lt;p&gt;If you&amp;#39;re using &lt;a href=&quot;https://brew.sh/&quot;&gt;Homebrew&lt;/a&gt;, you can run the following command to install ripgrep:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ brew install ripgrep
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If you&amp;#39;re using a different package manager, you can find a comprehensive list of installation instructions in the &lt;a href=&quot;https://github.com/BurntSushi/ripgrep#installation&quot;&gt;README.md&lt;/a&gt; on GitHub.&lt;/p&gt;
&lt;h2&gt;The Basics&lt;/h2&gt;
&lt;p&gt;The name of the ripgrep executable is &lt;code&gt;rg&lt;/code&gt;. In its most basic form, a simple search can look like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg &amp;#39;// TODO&amp;#39;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This command will recursively search all files in the current directory (and its subdirectories) for the string &lt;code&gt;// TODO&lt;/code&gt; and output the matches that it finds. If I run this command within the &lt;em&gt;src&lt;/em&gt; directory of the &lt;a href=&quot;https://github.com/prettier/prettier&quot;&gt;prettier repository&lt;/a&gt;, the output looks like this:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg &amp;#39;// TODO&amp;#39;
language-css/parser-postcss.js
521:  // TODO: Remove this hack when this issue is fixed:

language-markdown/parser-markdown.js
121:    // TODO: Delete this in 2.0

language-handlebars/parser-glimmer.js
32:      // TODO: `locStart` and `locEnd` should return a number offset

common/util-shared.js
42:  mapDoc, // TODO: remove in 2.0, we already exposed it in docUtils

language-js/utils.js
239:// TODO: This is a bad hack and we need a better way to distinguish between

language-html/utils.js
80:  // TODO: handle non-text children in &amp;lt;pre&amp;gt;

common/internal-plugins.js
91:      // TODO: switch these to just `postcss` and use `language` instead.
134:      // TODO: Delete this in 2.0

language-html/constants.evaluate.js
21:  // TODO: send PR to upstream

language-js/printer-estree.js
5:// TODO(azz): anything that imports from main shouldn&amp;#39;t be in a `language-*` dir.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The matches are grouped by file name. For each match, ripgrep prints the line number and highlights the matching substring.&lt;/p&gt;
&lt;h2&gt;Frequently Used Options&lt;/h2&gt;
&lt;p&gt;For the remainder of this article, I&amp;#39;ll go over several ripgrep options that I find myself using frequently to perform various search tasks when programming. I&amp;#39;m using the &lt;a href=&quot;https://github.com/prettier/prettier&quot;&gt;prettier repository&lt;/a&gt; to demonstrate the different options and what effect they have.&lt;/p&gt;
&lt;p&gt;Feel free to clone the repository and follow along:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ git clone https://github.com/prettier/prettier.git
$ cd prettier
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Also, unless stated otherwise, I&amp;#39;m running all commands from within the &lt;em&gt;src&lt;/em&gt; directory:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ cd src
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;No Options&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s start with running ripgrep without any options. The default behavior might do exactly what you want already. Here, I am searching for the string &lt;code&gt;// TODO&lt;/code&gt; within the current working directory:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg &amp;#39;// TODO&amp;#39;
language-css/parser-postcss.js
521:  // TODO: Remove this hack when this issue is fixed:

language-markdown/parser-markdown.js
121:    // TODO: Delete this in 2.0

language-handlebars/parser-glimmer.js
32:      // TODO: `locStart` and `locEnd` should return a number offset

common/util-shared.js
42:  mapDoc, // TODO: remove in 2.0, we already exposed it in docUtils

language-js/utils.js
239:// TODO: This is a bad hack and we need a better way to distinguish between

language-html/utils.js
80:  // TODO: handle non-text children in &amp;lt;pre&amp;gt;

common/internal-plugins.js
91:      // TODO: switch these to just `postcss` and use `language` instead.
134:      // TODO: Delete this in 2.0

language-html/constants.evaluate.js
21:  // TODO: send PR to upstream

language-js/printer-estree.js
5:// TODO(azz): anything that imports from main shouldn&amp;#39;t be in a `language-*` dir.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can see all matches, grouped by file name, with line numbers and highlighted matching substrings. If you want to quickly find a given string in a bunch of files and directories, this could be sufficient already.&lt;/p&gt;
&lt;h2&gt;Files with Matches&lt;/h2&gt;
&lt;p&gt;Sometimes, you&amp;#39;re not interested in seeing the matches themselves, but rather the paths of all files that contain at least one match. You can use the &lt;code&gt;--files-with-matches&lt;/code&gt; option for that, or &lt;code&gt;-l&lt;/code&gt; for short:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg -l &amp;#39;// TODO&amp;#39;
language-markdown/parser-markdown.js
common/util-shared.js
language-html/constants.evaluate.js
language-css/parser-postcss.js
common/internal-plugins.js
language-js/printer-estree.js
language-html/utils.js
language-js/utils.js
language-handlebars/parser-glimmer.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that ripgrep doesn&amp;#39;t emit the files in a specific sort order by default. This is for performance reasons. If you want the list of file paths to be sorted alphabetically, you can use the &lt;code&gt;--sort path&lt;/code&gt; option:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg -l &amp;#39;// TODO&amp;#39; --sort path
common/internal-plugins.js
common/util-shared.js
language-css/parser-postcss.js
language-handlebars/parser-glimmer.js
language-html/constants.evaluate.js
language-html/utils.js
language-js/printer-estree.js
language-js/utils.js
language-markdown/parser-markdown.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Please be aware that using the &lt;code&gt;--sort path&lt;/code&gt; option &lt;a href=&quot;https://github.com/BurntSushi/ripgrep/issues/152&quot;&gt;disables all parallelism&lt;/a&gt; in ripgrep. Unless you&amp;#39;re searching a large number of files though, you likely won&amp;#39;t notice much of a performance difference.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;-l&lt;/code&gt; flag is particularly useful to pipe ripgrep&amp;#39;s output into another program and perform additional operations on the matching files. For example, you could use ripgrep to find all files matching the string &lt;code&gt;@format&lt;/code&gt; and format them with Prettier using the &lt;code&gt;prettier&lt;/code&gt; executable:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg -l &amp;#39;@format&amp;#39; | xargs prettier --write
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Files Without a Match&lt;/h2&gt;
&lt;p&gt;Sometimes, you might not be interested in files that &lt;em&gt;do&lt;/em&gt; contain a match, but rather in those that &lt;em&gt;don&amp;#39;t&lt;/em&gt;. The &lt;code&gt;--files-without-match&lt;/code&gt; option outputs exactly those files. Unlike the &lt;code&gt;--files-with-matches&lt;/code&gt; option, the &lt;code&gt;--files-without-match&lt;/code&gt; option does not have a short alias.&lt;/p&gt;
&lt;p&gt;The following command lists all files that don&amp;#39;t contain any of the strings &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, or &lt;code&gt;const&lt;/code&gt;. These JavaScript files don&amp;#39;t contain any local variable declarations:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg --files-without-match &amp;#39;\b(var|let|const)\b&amp;#39;
language-yaml/pragma.js
language-graphql/pragma.js
document/index.js
utils/get-last.js
language-js/preprocess.js
common/internal-plugins.js
common/third-party.js
utils/arrayify.js
language-html/pragma.js
common/errors.js
language-html/clean.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And again, we can sort the list of files by using the &lt;code&gt;--sort path&lt;/code&gt; option:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg --files-without-match &amp;#39;\b(var|let|const)\b&amp;#39; --sort path
common/errors.js
common/internal-plugins.js
common/third-party.js
document/index.js
language-graphql/pragma.js
language-html/clean.js
language-html/pragma.js
language-js/preprocess.js
language-yaml/pragma.js
utils/arrayify.js
utils/get-last.js
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Notice that we&amp;#39;re using several regular expression features in our search pattern:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;\b&lt;/code&gt; matches a &lt;a href=&quot;https://www.regular-expressions.info/wordboundaries.html&quot;&gt;word boundary&lt;/a&gt;. That way, the string &lt;code&gt;delete&lt;/code&gt; won&amp;#39;t match the &lt;code&gt;let&lt;/code&gt; pattern.&lt;/li&gt;
&lt;li&gt;&lt;code&gt;|&lt;/code&gt; denotes an &lt;a href=&quot;https://www.regular-expressions.info/alternation.html&quot;&gt;alternation&lt;/a&gt;. The pattern &lt;code&gt;var|let|const&lt;/code&gt; matches any string that matches any of the patterns &lt;code&gt;var&lt;/code&gt;, &lt;code&gt;let&lt;/code&gt;, or &lt;code&gt;const&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;ripgrep will treat the search pattern as a regular expression by default; there&amp;#39;s no need to specify another flag to turn the search pattern into a regular expression.&lt;/p&gt;
&lt;h2&gt;Inverting Matches&lt;/h2&gt;
&lt;p&gt;Sometimes, you might be interested in all lines that do &lt;em&gt;not&lt;/em&gt; match a given pattern, rather than those lines that do. ripgrep lets us show those lines using the &lt;code&gt;--invert-match&lt;/code&gt; (or &lt;code&gt;-v&lt;/code&gt; for short) flag.&lt;/p&gt;
&lt;p&gt;Every now and then, I want to run a sanity check on all lines of code I&amp;#39;ve changed in a given Git commit. This is particularly helpful when running a codemod that changes hundreds or thousands of files. In those cases, I want to see a sorted and de-duplicated list of all changed lines. Here&amp;#39;s the command I use:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;git show | rg &amp;#39;^[-+]&amp;#39; | rg -v &amp;#39;^[-+]{3}&amp;#39; | sort | uniq
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For the commit &lt;a href=&quot;https://github.com/prettier/prettier/commit/6daa7e199e2d71cee66f5ebee3b2efe4648d7b99&quot;&gt;6daa7e199e2d71cee66f5ebee3b2efe4648d7b99&lt;/a&gt; in the Prettier repository, this is the output:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;+      - &amp;quot;patch-release&amp;quot;
-      - patch-release
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If I were to remove the &lt;code&gt;rg -v &amp;#39;^[-+]{3}&amp;#39;&lt;/code&gt; bit from the pipe, the output would include file names as well, which is not what I want:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;+      - &amp;quot;patch-release&amp;quot;
+++ b/.github/workflows/dev-test.yml
+++ b/.github/workflows/lint.yml
+++ b/.github/workflows/prod-test.yml
-      - patch-release
--- a/.github/workflows/dev-test.yml
--- a/.github/workflows/lint.yml
--- a/.github/workflows/prod-test.yml
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;By piping the output of the first search through &lt;code&gt;rg -v &amp;#39;^[-+]{3}&amp;#39;&lt;/code&gt;, I&amp;#39;m excluding all lines that start with three pluses or minuses, giving me a cleaner output at the end.&lt;/p&gt;
&lt;h2&gt;Fixed Strings&lt;/h2&gt;
&lt;p&gt;Usually, it&amp;#39;s useful that ripgrep treats every search pattern as a regular expression by default. We&amp;#39;ve seen in the previous section how we can search for several strings using the pattern &lt;code&gt;var|let|const&lt;/code&gt; using an alternation, and there was no need for an additional flag to tell ripgrep to interpret the pattern as a regular expression rather than a fixed string.&lt;/p&gt;
&lt;p&gt;However, if we want to search for a string that is not a well-formed regular expression, we get an error:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg &amp;#39;?.&amp;#39;
regex parse error:
    ?.
    ^
error: repetition operator missing expression
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the above example, our search for the pattern &lt;code&gt;?.&lt;/code&gt; failed because the pattern is malformed. In a regular expression, the &lt;code&gt;?&lt;/code&gt; character denotes a repetition operator that makes the previous expression optional. It must follow an expression, which it doesn&amp;#39;t do here.&lt;/p&gt;
&lt;p&gt;We can tell ripgrep that we want it to interpret the search string as a fixed string rather than a regular expression pattern. All characters that would have special meaning in a regular expression (e.g. &lt;code&gt;$&lt;/code&gt;, &lt;code&gt;?&lt;/code&gt;, &lt;code&gt;|&lt;/code&gt;, …) will be matched verbatim. The flag we need to use to turn on this behavior is called &lt;code&gt;--fixed-strings&lt;/code&gt;, or &lt;code&gt;-F&lt;/code&gt; for short:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg -F &amp;#39;?.&amp;#39;
language-js/printer-estree.js
4763:    return &amp;quot;?.&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, the search has succeeded and we get all results matching the string &lt;code&gt;?.&lt;/code&gt; verbatim.&lt;/p&gt;
&lt;h2&gt;Context Around a Match&lt;/h2&gt;
&lt;p&gt;Sometimes, seeing only the matching lines themselves without any preceding or following lines might lack context. Take the search for &lt;code&gt;// TODO&lt;/code&gt; as an example again:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg &amp;#39;// TODO&amp;#39;
language-css/parser-postcss.js
521:  // TODO: Remove this hack when this issue is fixed:

common/util-shared.js
42:  mapDoc, // TODO: remove in 2.0, we already exposed it in docUtils

common/internal-plugins.js
91:      // TODO: switch these to just `postcss` and use `language` instead.
134:      // TODO: Delete this in 2.0

language-markdown/parser-markdown.js
121:    // TODO: Delete this in 2.0

language-handlebars/parser-glimmer.js
32:      // TODO: `locStart` and `locEnd` should return a number offset

language-js/utils.js
239:// TODO: This is a bad hack and we need a better way to distinguish between

language-js/printer-estree.js
5:// TODO(azz): anything that imports from main shouldn&amp;#39;t be in a `language-*` dir.

language-html/constants.evaluate.js
21:  // TODO: send PR to upstream

language-html/utils.js
80:  // TODO: handle non-text children in &amp;lt;pre&amp;gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Wouldn&amp;#39;t it be helpful if we could see a few lines following each &lt;code&gt;// TODO&lt;/code&gt; comment to get an idea of the code to which each comment refers? It turns out that ripgrep can do that. We can specify the &lt;code&gt;--context&lt;/code&gt; option (or &lt;code&gt;-C&lt;/code&gt; for short) and pass it an argument &lt;code&gt;N&lt;/code&gt; to have ripgrep display &lt;code&gt;N&lt;/code&gt; lines before and after each matching line:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg &amp;#39;// TODO&amp;#39; -C 2
language-css/parser-postcss.js
519-  }
520-
521:  // TODO: Remove this hack when this issue is fixed:
522-  // https://github.com/shellscape/postcss-less/issues/88
523-  const LessParser = require(&amp;quot;postcss-less/dist/less-parser&amp;quot;);

language-markdown/parser-markdown.js
119-  parsers: {
120-    remark: markdownParser,
121:    // TODO: Delete this in 2.0
122-    markdown: markdownParser,
123-    mdx: mdxParser

common/util-shared.js
40-  isPreviousLineEmpty,
41-  getNextNonSpaceNonCommentCharacterIndex,
42:  mapDoc, // TODO: remove in 2.0, we already exposed it in docUtils
43-  makeString: util.makeString,
44-  addLeadingComment: util.addLeadingComment,

common/internal-plugins.js
89-  {
90-    parsers: {
91:      // TODO: switch these to just `postcss` and use `language` instead.
92-      get css() {
93-        return eval(&amp;quot;require&amp;quot;)(&amp;quot;../language-css/parser-postcss&amp;quot;).parsers.css;
--
132-          .remark;
133-      },
134:      // TODO: Delete this in 2.0
135-      get markdown() {
136-        return eval(&amp;quot;require&amp;quot;)(&amp;quot;../language-markdown/parser-markdown&amp;quot;).parsers

language-js/utils.js
237-}
238-
239:// TODO: This is a bad hack and we need a better way to distinguish between
240-// arrow functions and otherwise
241-function isFunctionNotation(node, options) {

language-handlebars/parser-glimmer.js
30-      parse,
31-      astFormat: &amp;quot;glimmer&amp;quot;,
32:      // TODO: `locStart` and `locEnd` should return a number offset
33-      // https://prettier.io/docs/en/plugins.html#parsers
34-      // but we need access to the original text to use

language-html/constants.evaluate.js
19-
20-const CSS_DISPLAY_TAGS = Object.assign({}, getCssStyleTags(&amp;quot;display&amp;quot;), {
21:  // TODO: send PR to upstream
22-  button: &amp;quot;inline-block&amp;quot;,
23-

language-html/utils.js
78-  }
79-
80:  // TODO: handle non-text children in &amp;lt;pre&amp;gt;
81-  if (
82-    isPreLikeNode(node) &amp;amp;&amp;amp;

language-js/printer-estree.js
3-const assert = require(&amp;quot;assert&amp;quot;);
4-
5:// TODO(azz): anything that imports from main shouldn&amp;#39;t be in a `language-*` dir.
6-const comments = require(&amp;quot;../main/comments&amp;quot;);
7-const {
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, we can see two lines before and after each &lt;code&gt;// TODO&lt;/code&gt; comment, giving us some more context without having to open those files.&lt;/p&gt;
&lt;p&gt;If you want to control the number of lines before and after the matching line independently, you can use the &lt;code&gt;--before-context&lt;/code&gt; and &lt;code&gt;--after-context&lt;/code&gt; options, respectively, or &lt;code&gt;-B&lt;/code&gt; and &lt;code&gt;-A&lt;/code&gt; for short. For example, here are all &lt;code&gt;// TODO&lt;/code&gt; comments, followed by the next three lines:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg &amp;#39;// TODO&amp;#39; -A 3
language-markdown/parser-markdown.js
121:    // TODO: Delete this in 2.0
122-    markdown: markdownParser,
123-    mdx: mdxParser
124-  }

common/util-shared.js
42:  mapDoc, // TODO: remove in 2.0, we already exposed it in docUtils
43-  makeString: util.makeString,
44-  addLeadingComment: util.addLeadingComment,
45-  addDanglingComment: util.addDanglingComment,

common/internal-plugins.js
91:      // TODO: switch these to just `postcss` and use `language` instead.
92-      get css() {
93-        return eval(&amp;quot;require&amp;quot;)(&amp;quot;../language-css/parser-postcss&amp;quot;).parsers.css;
94-      },
--
134:      // TODO: Delete this in 2.0
135-      get markdown() {
136-        return eval(&amp;quot;require&amp;quot;)(&amp;quot;../language-markdown/parser-markdown&amp;quot;).parsers
137-          .remark;

language-handlebars/parser-glimmer.js
32:      // TODO: `locStart` and `locEnd` should return a number offset
33-      // https://prettier.io/docs/en/plugins.html#parsers
34-      // but we need access to the original text to use
35-      // `loc.start` and `loc.end` objects to calculate the offset

language-js/utils.js
239:// TODO: This is a bad hack and we need a better way to distinguish between
240-// arrow functions and otherwise
241-function isFunctionNotation(node, options) {
242-  return isGetterOrSetter(node) || sameLocStart(node, node.value, options);

language-js/printer-estree.js
5:// TODO(azz): anything that imports from main shouldn&amp;#39;t be in a `language-*` dir.
6-const comments = require(&amp;quot;../main/comments&amp;quot;);
7-const {
8-  getParentExportDeclaration,

language-css/parser-postcss.js
521:  // TODO: Remove this hack when this issue is fixed:
522-  // https://github.com/shellscape/postcss-less/issues/88
523-  const LessParser = require(&amp;quot;postcss-less/dist/less-parser&amp;quot;);
524-  LessParser.prototype.atrule = function() {

language-html/constants.evaluate.js
21:  // TODO: send PR to upstream
22-  button: &amp;quot;inline-block&amp;quot;,
23-
24-  // special cases for some css display=none elements

language-html/utils.js
80:  // TODO: handle non-text children in &amp;lt;pre&amp;gt;
81-  if (
82-    isPreLikeNode(node) &amp;amp;&amp;amp;
83-    node.children.some(
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Specific File Types Only&lt;/h2&gt;
&lt;p&gt;The &lt;code&gt;--type&lt;/code&gt; option, or &lt;code&gt;-t&lt;/code&gt; for short, lets you restrict searches to files of a specific type. To see how this option works, let&amp;#39;s move up one level from the &lt;em&gt;src&lt;/em&gt; directory into the root of the prettier repository:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ cd ..
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Let&amp;#39;s confirm the current working directory:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ pwd
/Users/marius/code/prettier
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alright, now we&amp;#39;re ready. We can run a search for &lt;code&gt;@format&lt;/code&gt; only on JavaScript files:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg -t js &amp;#39;@format&amp;#39;
src/language-yaml/pragma.js
12:  return `# @format\n\n${text}`;

src/language-graphql/pragma.js
8:  return &amp;quot;# @format\n\n&amp;quot; + text;

src/language-css/clean.js
35:     * @format

src/language-html/pragma.js
8:  return &amp;quot;&amp;lt;!-- @format --&amp;gt;\n\n&amp;quot; + text.replace(/^\s*\n/, &amp;quot;&amp;quot;);

src/main/core-options.js
110:    description: &amp;quot;Insert @format pragma into file&amp;#39;s first docblock comment.&amp;quot;,
234:      Require either &amp;#39;@prettier&amp;#39; or &amp;#39;@format&amp;#39; to be present in the file&amp;#39;s first docblock comment

tests/insert-pragma/js/module-with-pragma.js
5: * @format

tests/require-pragma/js/module-with-pragma.js
3: * @format
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Or we could search within Markdown files only:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg -t md &amp;#39;@format&amp;#39;
docs/cli.md
101:Valid pragmas are `@prettier` and `@format`.
105:Insert a `@format` pragma to the top of formatted files when pragma is absent. Works well when used in tandem with `--require-pragma`.

docs/options.md
258: * @format
270:Prettier can insert a special @format marker at the top of files specifying that the file has been formatted with prettier. This works well when used in tandem with the `--require-pragma` option. If there is already a docblock at the top of the file then this option will add a newline to it with the @format marker.

website/blog/2017-09-15-1.7.0.md
108: * @format
187:- [**Add option to require @prettier or @format pragma**](https://github.com/prettier/prettier/pull/2772) by [@wbinnssmith](https://github.com/wbinnssmith)

website/blog/2017-05-03-1.3.0.md
25:- When pretty-printing a file, add `@format` to the first block comment like `@flow`.
26:- Have a lint rule with autofix that checks if the file is correctly pretty printed when `@format` is present.
29:- Update the default code templates to add `@format` to the header.
30:- When you run code formatting via cmd-shift-c inside of Nuclide, automatically insert the `@format` header.
31:- Disable all the stylistic rules like max-len when `@format` is in the header.
34:- When pushing a new release of prettier, also run it through all the files with `@format` in order to avoid getting warnings afterwards.
35:- Add tracking for the number of files with `@format` over time.

website/blog/2017-11-07-1.8.0.md
136:#### Add option to insert `@format` to first docblock if absent ([#2865](https://github.com/prettier/prettier/pull/2865)) by [@samouri](https://github.com/samouri)
138:In 1.7, we added an option called `--require-pragma` to require files contain an `/** @format */` pragma to be formatted. In order to add this pragma to a large set of files you can now use [`--insert-pragma`](https://prettier.io/docs/en/cli.html#insert-pragma) flag.

website/blog/2018-02-26-1.11.0.md
814: * @format
820: * @format

website/versioned_docs/version-stable/cli.md
102:Valid pragmas are `@prettier` and `@format`.
106:Insert a `@format` pragma to the top of formatted files when pragma is absent. Works well when used in tandem with `--require-pragma`.

website/versioned_docs/version-stable/options.md
259: * @format
271:Prettier can insert a special @format marker at the top of files specifying that the file has been formatted with prettier. This works well when used in tandem with the `--require-pragma` option. If there is already a docblock at the top of the file then this option will add a newline to it with the @format marker.

tests/markdown/real-world-case.md
292:Valid pragmas are `@prettier` and `@format`.
695: * @format

tests/require-pragma/markdown/with-pragma-in-multiline.md
6:  @format
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Note that the type specifiers &lt;code&gt;js&lt;/code&gt; and &lt;code&gt;md&lt;/code&gt; are not file name extensions themselves. The type specifiers represent a set of file name extensions that are considered to be of that type:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;js&lt;/code&gt; represents the extensions &lt;em&gt;*.js&lt;/em&gt;, &lt;em&gt;*.jsx&lt;/em&gt;, and &lt;em&gt;*.vue&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;md&lt;/code&gt; represents the extensions &lt;em&gt;*.markdown&lt;/em&gt;, &lt;em&gt;*.md&lt;/em&gt;, &lt;em&gt;*.mdown&lt;/em&gt;, and &lt;em&gt;*.mkdn&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;You can pull up the complete list of supported type specifiers and the corresponding file name extensions by running the &lt;code&gt;rg --type-list&lt;/code&gt; command.&lt;/p&gt;
&lt;h2&gt;Using a Glob&lt;/h2&gt;
&lt;p&gt;Sometimes, using the &lt;code&gt;--type&lt;/code&gt; (or &lt;code&gt;-t&lt;/code&gt; for short) option might not give you enough control over which files to include in the search. In those cases, you can use the &lt;code&gt;--glob&lt;/code&gt; (or &lt;code&gt;-g&lt;/code&gt; for short) option. ripgrep will only search files whose paths match the specified glob.&lt;/p&gt;
&lt;p&gt;For example, you could run a search for &lt;code&gt;// TODO&lt;/code&gt; comments within only those JavaScript files whose name starts with &amp;quot;parser-&amp;quot;:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;$ rg -g &amp;#39;parser-*.js&amp;#39; &amp;#39;// TODO&amp;#39;
language-markdown/parser-markdown.js
121:    // TODO: Delete this in 2.0

language-handlebars/parser-glimmer.js
32:      // TODO: `locStart` and `locEnd` should return a number offset

language-css/parser-postcss.js
521:  // TODO: Remove this hack when this issue is fixed:
&lt;/code&gt;&lt;/pre&gt;
&lt;h2&gt;Showing the Help Page&lt;/h2&gt;
&lt;p&gt;Lastly, if you ever forget what a specific option is called, or if you want to see what other options are available, ripgrep provides two different levels of help:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rg -h&lt;/code&gt;: short descriptions with a condensed layout&lt;/li&gt;
&lt;li&gt;&lt;code&gt;rg --help&lt;/code&gt;: long descriptions with detailed explanations&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Here&amp;#39;s what ripgrep 12.0.0 prints when running the &lt;code&gt;rg -h&lt;/code&gt; command:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;ripgrep 12.0.0
Andrew Gallant &amp;lt;jamslam@gmail.com&amp;gt;

ripgrep (rg) recursively searches your current directory for a regex pattern.
By default, ripgrep will respect your .gitignore and automatically skip hidden
files/directories and binary files.

ripgrep&amp;#39;s default regex engine uses finite automata and guarantees linear
time searching. Because of this, features like backreferences and arbitrary
look-around are not supported. However, if ripgrep is built with PCRE2, then
the --pcre2 flag can be used to enable backreferences and look-around.

ripgrep supports configuration files. Set RIPGREP_CONFIG_PATH to a
configuration file. The file can specify one shell argument per line. Lines
starting with &amp;#39;#&amp;#39; are ignored. For more details, see the man page or the
README.

ripgrep will automatically detect if stdin exists and search stdin for a regex
pattern, e.g. &amp;#39;ls | rg foo&amp;#39;. In some environments, stdin may exist when it
shouldn&amp;#39;t. To turn off stdin detection explicitly specify the directory to
search, e.g. &amp;#39;rg foo ./&amp;#39;.

Tip: to disable all smart filtering and make ripgrep behave a bit more like
classical grep, use &amp;#39;rg -uuu&amp;#39;.

Project home page: https://github.com/BurntSushi/ripgrep

Use -h for short descriptions and --help for more details.

USAGE:
    rg [OPTIONS] PATTERN [PATH ...]
    rg [OPTIONS] [-e PATTERN ...] [-f PATTERNFILE ...] [PATH ...]
    rg [OPTIONS] --files [PATH ...]
    rg [OPTIONS] --type-list
    command | rg [OPTIONS] PATTERN

ARGS:
    &amp;lt;PATTERN&amp;gt;    A regular expression used for searching.
    &amp;lt;PATH&amp;gt;...    A file or directory to search.

OPTIONS:
    -A, --after-context &amp;lt;NUM&amp;gt;               Show NUM lines after each match.
        --auto-hybrid-regex                 Dynamically use PCRE2 if necessary.
    -B, --before-context &amp;lt;NUM&amp;gt;              Show NUM lines before each match.
        --binary                            Search binary files.
        --block-buffered                    Force block buffering.
    -b, --byte-offset                       Print the 0-based byte offset for each matching line.
    -s, --case-sensitive                    Search case sensitively (default).
        --color &amp;lt;WHEN&amp;gt;                      Controls when to use color.
        --colors &amp;lt;COLOR_SPEC&amp;gt;...            Configure color settings and styles.
        --column                            Show column numbers.
    -C, --context &amp;lt;NUM&amp;gt;                     Show NUM lines before and after each match.
        --context-separator &amp;lt;SEPARATOR&amp;gt;     Set the context separator string.
    -c, --count                             Only show the count of matching lines for each file.
        --count-matches                     Only show the count of individual matches for each file.
        --crlf                              Support CRLF line terminators (useful on Windows).
        --debug                             Show debug messages.
        --dfa-size-limit &amp;lt;NUM+SUFFIX?&amp;gt;      The upper size limit of the regex DFA.
    -E, --encoding &amp;lt;ENCODING&amp;gt;               Specify the text encoding of files to search.
        --engine &amp;lt;ENGINE&amp;gt;                   Specify which regexp engine to use. [default: default]
    -f, --file &amp;lt;PATTERNFILE&amp;gt;...             Search for patterns from the given file.
        --files                             Print each file that would be searched.
    -l, --files-with-matches                Only print the paths with at least one match.
        --files-without-match               Only print the paths that contain zero matches.
    -F, --fixed-strings                     Treat the pattern as a literal string.
    -L, --follow                            Follow symbolic links.
    -g, --glob &amp;lt;GLOB&amp;gt;...                    Include or exclude files.
        --glob-case-insensitive             Process all glob patterns case insensitively.
    -h, --help                              Prints help information. Use --help for more details.
        --heading                           Print matches grouped by each file.
        --hidden                            Search hidden files and directories.
        --iglob &amp;lt;GLOB&amp;gt;...                   Include or exclude files case insensitively.
    -i, --ignore-case                       Case insensitive search.
        --ignore-file &amp;lt;PATH&amp;gt;...             Specify additional ignore files.
        --ignore-file-case-insensitive      Process ignore files case insensitively.
        --include-zero                      Include files with zero matches in summary
    -v, --invert-match                      Invert matching.
        --json                              Show search results in a JSON Lines format.
        --line-buffered                     Force line buffering.
    -n, --line-number                       Show line numbers.
    -x, --line-regexp                       Only show matches surrounded by line boundaries.
    -M, --max-columns &amp;lt;NUM&amp;gt;                 Don&amp;#39;t print lines longer than this limit.
        --max-columns-preview               Print a preview for lines exceeding the limit.
    -m, --max-count &amp;lt;NUM&amp;gt;                   Limit the number of matches.
        --max-depth &amp;lt;NUM&amp;gt;                   Descend at most NUM directories.
        --max-filesize &amp;lt;NUM+SUFFIX?&amp;gt;        Ignore files larger than NUM in size.
        --mmap                              Search using memory maps when possible.
    -U, --multiline                         Enable matching across multiple lines.
        --multiline-dotall                  Make &amp;#39;.&amp;#39; match new lines when multiline is enabled.
        --no-config                         Never read configuration files.
    -I, --no-filename                       Never print the file path with the matched lines.
        --no-heading                        Don&amp;#39;t group matches by each file.
        --no-ignore                         Don&amp;#39;t respect ignore files.
        --no-ignore-dot                     Don&amp;#39;t respect .ignore files.
        --no-ignore-exclude                 Don&amp;#39;t respect local exclusion files.
        --no-ignore-files                   Don&amp;#39;t respect --ignore-file arguments.
        --no-ignore-global                  Don&amp;#39;t respect global ignore files.
        --no-ignore-messages                Suppress gitignore parse error messages.
        --no-ignore-parent                  Don&amp;#39;t respect ignore files in parent directories.
        --no-ignore-vcs                     Don&amp;#39;t respect VCS ignore files.
    -N, --no-line-number                    Suppress line numbers.
        --no-messages                       Suppress some error messages.
        --no-mmap                           Never use memory maps.
        --no-pcre2-unicode                  Disable Unicode mode for PCRE2 matching.
        --no-require-git                    Do not require a git repository to use gitignores.
        --no-unicode                        Disable Unicode mode.
    -0, --null                              Print a NUL byte after file paths.
        --null-data                         Use NUL as a line terminator instead of \n.
        --one-file-system                   Do not descend into directories on other file systems.
    -o, --only-matching                     Print only matches parts of a line.
        --passthru                          Print both matching and non-matching lines.
        --path-separator &amp;lt;SEPARATOR&amp;gt;        Set the path separator.
    -P, --pcre2                             Enable PCRE2 matching.
        --pcre2-version                     Print the version of PCRE2 that ripgrep uses.
        --pre &amp;lt;COMMAND&amp;gt;                     search outputs of COMMAND FILE for each FILE
        --pre-glob &amp;lt;GLOB&amp;gt;...                Include or exclude files from a preprocessing command.
    -p, --pretty                            Alias for --color always --heading --line-number.
    -q, --quiet                             Do not print anything to stdout.
        --regex-size-limit &amp;lt;NUM+SUFFIX?&amp;gt;    The upper size limit of the compiled regex.
    -e, --regexp &amp;lt;PATTERN&amp;gt;...               A pattern to search for.
    -r, --replace &amp;lt;REPLACEMENT_TEXT&amp;gt;        Replace matches with the given text.
    -z, --search-zip                        Search in compressed files.
    -S, --smart-case                        Smart case search.
        --sort &amp;lt;SORTBY&amp;gt;                     Sort results in ascending order. Implies --threads=1.
        --sortr &amp;lt;SORTBY&amp;gt;                    Sort results in descending order. Implies --threads=1.
        --stats                             Print statistics about this ripgrep search.
    -a, --text                              Search binary files as if they were text.
    -j, --threads &amp;lt;NUM&amp;gt;                     The approximate number of threads to use.
        --trim                              Trim prefixed whitespace from matches.
    -t, --type &amp;lt;TYPE&amp;gt;...                    Only search files matching TYPE.
        --type-add &amp;lt;TYPE_SPEC&amp;gt;...           Add a new glob for a file type.
        --type-clear &amp;lt;TYPE&amp;gt;...              Clear globs for a file type.
        --type-list                         Show all supported file types.
    -T, --type-not &amp;lt;TYPE&amp;gt;...                Do not search files matching TYPE.
    -u, --unrestricted                      Reduce the level of &amp;quot;smart&amp;quot; searching.
    -V, --version                           Prints version information
        --vimgrep                           Show results in vim compatible format.
    -H, --with-filename                     Print the file path with the matched lines.
    -w, --word-regexp                       Only show matches surrounded by word boundaries.
&lt;/code&gt;&lt;/pre&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
<entry>
  <id>urn:uuid:30b233ee-3837-4073-acf1-868bfe8796ed</id>
  <title>Const Assertions in Literal Expressions in TypeScript</title>
  <link href="https://mariusschulz.com/blog/const-assertions-in-literal-expressions-in-typescript" />
  <published>2019-12-15T00:00:00.000Z</published>
  <updated>2020-11-29T00:00:00.000Z</updated>
  <summary type="text">With TypeScript 3.4, const assertions were added to the language. A const assertion is a special kind of type assertion in which the const keyword is used instead of a type name.</summary>
  <content type="html" xml:base="https://mariusschulz.com/">&lt;p&gt;With TypeScript 3.4, &lt;code&gt;const&lt;/code&gt; assertions were added to the language. A &lt;code&gt;const&lt;/code&gt; assertion is a special kind of type assertion in which the &lt;code&gt;const&lt;/code&gt; keyword is used instead of a type name. In this post, I&amp;#39;ll explain how &lt;code&gt;const&lt;/code&gt; assertions work and why we might want to use them.&lt;/p&gt;
&lt;h2&gt;Motivation for &lt;code&gt;const&lt;/code&gt; Assertions&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s say we&amp;#39;ve written the following &lt;code&gt;fetchJSON&lt;/code&gt; function. It accepts a URL and an HTTP request method, uses the browser&amp;#39;s &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API&quot;&gt;Fetch API&lt;/a&gt; to make a GET or POST request to that URL, and deserializes the response as JSON:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;function fetchJSON(url: string, method: &amp;quot;GET&amp;quot; | &amp;quot;POST&amp;quot;) {
  return fetch(url, { method }).then(response =&amp;gt; response.json());
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We can call this function and pass an arbitrary URL to the &lt;code&gt;url&lt;/code&gt; param and the string &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; to the &lt;code&gt;method&lt;/code&gt; param. Note that we&amp;#39;re using two &lt;strong&gt;string literals&lt;/strong&gt; here:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// OK, no type error
fetchJSON(&amp;quot;https://example.com/&amp;quot;, &amp;quot;GET&amp;quot;).then(data =&amp;gt; {
  // ...
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;To verify whether this function call is type-correct, TypeScript will check the types of all arguments of the function call against the parameter types defined in the function declaration. In this case, the types of both arguments are assignable to the parameter types, and therefore this function call type-checks correctly.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s now do a little bit of refactoring. The HTTP specification defines various additional &lt;a href=&quot;https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods&quot;&gt;request methods&lt;/a&gt; such as DELETE, HEAD, PUT, and others. We can define an &lt;code&gt;HTTPRequestMethod&lt;/code&gt; enum-style mapping object and list the various request methods:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const HTTPRequestMethod = {
  CONNECT: &amp;quot;CONNECT&amp;quot;,
  DELETE: &amp;quot;DELETE&amp;quot;,
  GET: &amp;quot;GET&amp;quot;,
  HEAD: &amp;quot;HEAD&amp;quot;,
  OPTIONS: &amp;quot;OPTIONS&amp;quot;,
  PATCH: &amp;quot;PATCH&amp;quot;,
  POST: &amp;quot;POST&amp;quot;,
  PUT: &amp;quot;PUT&amp;quot;,
  TRACE: &amp;quot;TRACE&amp;quot;,
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now we can replace the string literal &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; in our &lt;code&gt;fetchJSON&lt;/code&gt; function call by &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt;:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;fetchJSON(&amp;quot;https://example.com/&amp;quot;, HTTPRequestMethod.GET).then(data =&amp;gt; {
  // ...
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But now, TypeScript produces a type error! The type checker points out that the type of &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; is not assignable to the type of the &lt;code&gt;method&lt;/code&gt; param:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;// Error: Argument of type &amp;#39;string&amp;#39; is not assignable
// to parameter of type &amp;#39;&amp;quot;GET&amp;quot; | &amp;quot;POST&amp;quot;&amp;#39;.
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Why is that? &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; evaluates to the string &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;, the same value that we passed as an argument before. What&amp;#39;s the difference between the types of the property &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; and the string literal &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;? To answer that question, we have to understand how &lt;strong&gt;string literal types&lt;/strong&gt; work and how TypeScript performs &lt;strong&gt;literal type widening&lt;/strong&gt;.&lt;/p&gt;
&lt;h2&gt;String Literal Types&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s look at the type of the value &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; when we assign it to a variable declared using the &lt;code&gt;const&lt;/code&gt; keyword:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: &amp;quot;GET&amp;quot;
const httpRequestMethod = &amp;quot;GET&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;TypeScript infers the type &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; for our &lt;code&gt;httpRequestMethod&lt;/code&gt; variable. &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; is what&amp;#39;s called a &lt;a href=&quot;/blog/string-literal-types-in-typescript&quot;&gt;string literal type&lt;/a&gt;. Each literal type describes precisely one value, e.g. a specific string, number, boolean value, or enum member. In our case, we&amp;#39;re dealing with the string value &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;, so our literal type is the string literal type &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;Notice that we&amp;#39;ve declared the &lt;code&gt;httpRequestMethod&lt;/code&gt; variable using the &lt;code&gt;const&lt;/code&gt; keyword. Therefore, we know that it&amp;#39;s impossible to reassign the variable later; it&amp;#39;ll always hold the value &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;. TypeScript understands that and automatically infers the string literal type &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; to represent this piece of information in the type system.&lt;/p&gt;
&lt;h2&gt;Literal Type Widening&lt;/h2&gt;
&lt;p&gt;Let&amp;#39;s now see what happens if we use the &lt;code&gt;let&lt;/code&gt; keyword (instead of &lt;code&gt;const&lt;/code&gt;) to declare the &lt;code&gt;httpRequestMethod&lt;/code&gt; variable:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: string
let httpRequestMethod = &amp;quot;GET&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;TypeScript now performs what&amp;#39;s known as &lt;a href=&quot;/blog/literal-type-widening-in-typescript&quot;&gt;literal type widening&lt;/a&gt;. The &lt;code&gt;httpRequestMethod&lt;/code&gt; variable is inferred to have type &lt;code&gt;string&lt;/code&gt;. We&amp;#39;re initializing &lt;code&gt;httpRequestMethod&lt;/code&gt; with the string &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;, but since the variable is declared using the &lt;code&gt;let&lt;/code&gt; keyword, we can assign another value to it later:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: string
let httpRequestMethod = &amp;quot;GET&amp;quot;;

// OK, no type error
httpRequestMethod = &amp;quot;POST&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The later assignment of the value &lt;code&gt;&amp;quot;POST&amp;quot;&lt;/code&gt; is type-correct since &lt;code&gt;httpRequestMethod&lt;/code&gt; has type &lt;code&gt;string&lt;/code&gt;. TypeScript inferred the type &lt;code&gt;string&lt;/code&gt; because we most likely want to change the value of a variable declared using the &lt;code&gt;let&lt;/code&gt; keyword later on. If we didn&amp;#39;t want to reassign the variable, we should&amp;#39;ve used the &lt;code&gt;const&lt;/code&gt; keyword instead.&lt;/p&gt;
&lt;p&gt;Let&amp;#39;s now look at our enum-style mapping object:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const HTTPRequestMethod = {
  CONNECT: &amp;quot;CONNECT&amp;quot;,
  DELETE: &amp;quot;DELETE&amp;quot;,
  GET: &amp;quot;GET&amp;quot;,
  HEAD: &amp;quot;HEAD&amp;quot;,
  OPTIONS: &amp;quot;OPTIONS&amp;quot;,
  PATCH: &amp;quot;PATCH&amp;quot;,
  POST: &amp;quot;POST&amp;quot;,
  PUT: &amp;quot;PUT&amp;quot;,
  TRACE: &amp;quot;TRACE&amp;quot;,
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What type does &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; have? Let&amp;#39;s find out:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: string
const httpRequestMethod = HTTPRequestMethod.GET;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;TypeScript infers the type &lt;code&gt;string&lt;/code&gt; for our &lt;code&gt;httpRequestMethod&lt;/code&gt; variable. This is because we&amp;#39;re initializing the variable with the value &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; (which has type &lt;code&gt;string&lt;/code&gt;), so type &lt;code&gt;string&lt;/code&gt; is inferred.&lt;/p&gt;
&lt;p&gt;So why does &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; have type &lt;code&gt;string&lt;/code&gt; and not type &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;? We&amp;#39;re initializing the &lt;code&gt;GET&lt;/code&gt; property with the string literal &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;, and the &lt;code&gt;HTTPRequestMethod&lt;/code&gt; object itself is defined using the &lt;code&gt;const&lt;/code&gt; keyword. Shouldn&amp;#39;t the resulting type be the string literal type &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;?&lt;/p&gt;
&lt;p&gt;The reason that TypeScript infers type &lt;code&gt;string&lt;/code&gt; for &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; (and all the other properties) is that we could assign another value to any of the properties later on. To us, this object with its ALL_UPPERCASE property names looks like an enum which defines string constants that won&amp;#39;t change over time. However, to TypeScript this is just a regular object with a few properties that happen to be initialized with string values.&lt;/p&gt;
&lt;p&gt;The following example makes it a bit more obvious why TypeScript shouldn&amp;#39;t infer a string literal type for object properties initialized with a string literal:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: { name: string, jobTitle: string }
const person = {
  name: &amp;quot;Marius Schulz&amp;quot;,
  jobTitle: &amp;quot;Software Engineer&amp;quot;,
};

// OK, no type error
person.jobTitle = &amp;quot;Front End Engineer&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If the &lt;code&gt;jobTitle&lt;/code&gt; property were inferred to have type &lt;code&gt;&amp;quot;Software Engineer&amp;quot;&lt;/code&gt;, it would be a type error if we tried to assign any string other than &lt;code&gt;&amp;quot;Software Engineer&amp;quot;&lt;/code&gt; later on. Our assignment of &lt;code&gt;&amp;quot;Front End Engineer&amp;quot;&lt;/code&gt; would not be type-correct. Object properties are mutable by default, so we wouldn&amp;#39;t want TypeScript to infer a type which restricts us from performing perfectly valid mutations.&lt;/p&gt;
&lt;p&gt;So how do we make the usage of our &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; property in the function call type-check correctly? We need to understand &lt;strong&gt;non-widening literal types&lt;/strong&gt; first.&lt;/p&gt;
&lt;h2&gt;Non-Widening Literal Types&lt;/h2&gt;
&lt;p&gt;TypeScript has a special kind of literal type that&amp;#39;s known as a &lt;a href=&quot;/blog/literal-type-widening-in-typescript#non-widening-literal-types&quot;&gt;non-widening literal type&lt;/a&gt;. As the name suggests, non-widening literal types will not be widened to a more generic type. For example, the non-widening string literal type &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; will not be widened to &lt;code&gt;string&lt;/code&gt; in cases where type widening would normally occur.&lt;/p&gt;
&lt;p&gt;We can make the properties of our &lt;code&gt;HTTPRequestMethod&lt;/code&gt; object receive a non-widening literal type by applying a type assertion of the corresponding string literal type to every property value:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const HTTPRequestMethod = {
  CONNECT: &amp;quot;CONNECT&amp;quot; as &amp;quot;CONNECT&amp;quot;,
  DELETE: &amp;quot;DELETE&amp;quot; as &amp;quot;DELETE&amp;quot;,
  GET: &amp;quot;GET&amp;quot; as &amp;quot;GET&amp;quot;,
  HEAD: &amp;quot;HEAD&amp;quot; as &amp;quot;HEAD&amp;quot;,
  OPTIONS: &amp;quot;OPTIONS&amp;quot; as &amp;quot;OPTIONS&amp;quot;,
  PATCH: &amp;quot;PATCH&amp;quot; as &amp;quot;PATCH&amp;quot;,
  POST: &amp;quot;POST&amp;quot; as &amp;quot;POST&amp;quot;,
  PUT: &amp;quot;PUT&amp;quot; as &amp;quot;PUT&amp;quot;,
  TRACE: &amp;quot;TRACE&amp;quot; as &amp;quot;TRACE&amp;quot;,
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now, let&amp;#39;s check the type of &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; again:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: &amp;quot;GET&amp;quot;
const httpRequestMethod = HTTPRequestMethod.GET;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And indeed, now the &lt;code&gt;httpRequestMethod&lt;/code&gt; variable has type &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt; rather than type &lt;code&gt;string&lt;/code&gt;. The type of &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; (which is &lt;code&gt;&amp;quot;GET&amp;quot;&lt;/code&gt;) is assignable to the type of the &lt;code&gt;method&lt;/code&gt; parameter (which is &lt;code&gt;&amp;quot;GET&amp;quot; | &amp;quot;POST&amp;quot;&lt;/code&gt;), and therefore the &lt;code&gt;fetchJSON&lt;/code&gt; function call will now type-check correctly:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// OK, no type error
fetchJSON(&amp;quot;https://example.com/&amp;quot;, HTTPRequestMethod.GET).then(data =&amp;gt; {
  // ...
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is great news, but take a look at the number of type assertions we had to write to get to this point. That is a lot of noise! Every key/value pair now contains the name of the HTTP request method three times. Can we simplify this definition? Using TypeScript&amp;#39;s &lt;code&gt;const&lt;/code&gt; assertions feature, we most certainly can!&lt;/p&gt;
&lt;h2&gt;&lt;code&gt;const&lt;/code&gt; Assertions for Literal Expressions&lt;/h2&gt;
&lt;p&gt;Our &lt;code&gt;HTTPRequestMethod&lt;/code&gt; variable is initialized with a &lt;strong&gt;literal expression&lt;/strong&gt; which is an &lt;strong&gt;object literal&lt;/strong&gt; with several properties, all of which are initialized with &lt;strong&gt;string literals&lt;/strong&gt;. As of TypeScript 3.4, we can apply a &lt;code&gt;const&lt;/code&gt; assertion to a literal expression:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const HTTPRequestMethod = {
  CONNECT: &amp;quot;CONNECT&amp;quot;,
  DELETE: &amp;quot;DELETE&amp;quot;,
  GET: &amp;quot;GET&amp;quot;,
  HEAD: &amp;quot;HEAD&amp;quot;,
  OPTIONS: &amp;quot;OPTIONS&amp;quot;,
  PATCH: &amp;quot;PATCH&amp;quot;,
  POST: &amp;quot;POST&amp;quot;,
  PUT: &amp;quot;PUT&amp;quot;,
  TRACE: &amp;quot;TRACE&amp;quot;,
} as const;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;const&lt;/code&gt; assertion is a special type assertion that uses the &lt;code&gt;const&lt;/code&gt; keyword instead of a specific type name. Using a &lt;code&gt;const&lt;/code&gt; assertion on a literal expression has the following effects:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;No literal types in the literal expression will be &lt;a href=&quot;/blog/literal-type-widening-in-typescript&quot;&gt;widened&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Object literals will get &lt;a href=&quot;/blog/read-only-properties-in-typescript&quot;&gt;&lt;code&gt;readonly&lt;/code&gt; properties&lt;/a&gt;.&lt;/li&gt;
&lt;li&gt;Array literals will become &lt;a href=&quot;/blog/read-only-array-and-tuple-types-in-typescript&quot;&gt;&lt;code&gt;readonly&lt;/code&gt; tuples&lt;/a&gt;.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;With the &lt;code&gt;const&lt;/code&gt; assertion in place, the above definition of &lt;code&gt;HTTPRequestMethod&lt;/code&gt; is equivalent to the following:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const HTTPRequestMethod: {
  readonly CONNECT: &amp;quot;CONNECT&amp;quot;;
  readonly DELETE: &amp;quot;DELETE&amp;quot;;
  readonly GET: &amp;quot;GET&amp;quot;;
  readonly HEAD: &amp;quot;HEAD&amp;quot;;
  readonly OPTIONS: &amp;quot;OPTIONS&amp;quot;;
  readonly PATCH: &amp;quot;PATCH&amp;quot;;
  readonly POST: &amp;quot;POST&amp;quot;;
  readonly PUT: &amp;quot;PUT&amp;quot;;
  readonly TRACE: &amp;quot;TRACE&amp;quot;;
} = {
  CONNECT: &amp;quot;CONNECT&amp;quot;,
  DELETE: &amp;quot;DELETE&amp;quot;,
  GET: &amp;quot;GET&amp;quot;,
  HEAD: &amp;quot;HEAD&amp;quot;,
  OPTIONS: &amp;quot;OPTIONS&amp;quot;,
  PATCH: &amp;quot;PATCH&amp;quot;,
  POST: &amp;quot;POST&amp;quot;,
  PUT: &amp;quot;PUT&amp;quot;,
  TRACE: &amp;quot;TRACE&amp;quot;,
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;We wouldn&amp;#39;t want to have to write this definition by hand. It&amp;#39;s verbose and contains a lot of repetition; notice that every HTTP request method is spelled out four times. The &lt;code&gt;const&lt;/code&gt; assertion &lt;code&gt;as const&lt;/code&gt;, on the other hand, is very succinct and the only bit of TypeScript-specific syntax in the entire example.&lt;/p&gt;
&lt;p&gt;Also, observe that every property is now typed as &lt;code&gt;readonly&lt;/code&gt;. If we try to assign a value to a &lt;a href=&quot;/blog/read-only-properties-in-typescript&quot;&gt;read-only property&lt;/a&gt;, TypeScript will product a type error:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Error: Cannot assign to &amp;#39;GET&amp;#39;
// because it is a read-only property.
HTTPRequestMethod.GET = &amp;quot;...&amp;quot;;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;With the &lt;code&gt;const&lt;/code&gt; assertion, we&amp;#39;ve given our &lt;code&gt;HTTPRequestMethod&lt;/code&gt; object enum-like characteristics. But what about proper TypeScript enums?&lt;/p&gt;
&lt;h2&gt;Using TypeScript Enums&lt;/h2&gt;
&lt;p&gt;Another possible solution would&amp;#39;ve been to use a &lt;a href=&quot;https://www.typescriptlang.org/docs/handbook/enums.html&quot;&gt;TypeScript enum&lt;/a&gt; instead of a plain object literal. We could&amp;#39;ve defined &lt;code&gt;HTTPRequestMethod&lt;/code&gt; using the &lt;code&gt;enum&lt;/code&gt; keyword like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;enum HTTPRequestMethod {
  CONNECT = &amp;quot;CONNECT&amp;quot;,
  DELETE = &amp;quot;DELETE&amp;quot;,
  GET = &amp;quot;GET&amp;quot;,
  HEAD = &amp;quot;HEAD&amp;quot;,
  OPTIONS = &amp;quot;OPTIONS&amp;quot;,
  PATCH = &amp;quot;PATCH&amp;quot;,
  POST = &amp;quot;POST&amp;quot;,
  PUT = &amp;quot;PUT&amp;quot;,
  TRACE = &amp;quot;TRACE&amp;quot;,
}
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;TypeScript enums are meant to describe named constants, which is why their members are always read-only. Members of a &lt;a href=&quot;/blog/string-enums-in-typescript&quot;&gt;string enum&lt;/a&gt; have a string literal type:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: &amp;quot;GET&amp;quot;
const httpRequestMethod = HTTPRequestMethod.GET;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This means our function call will type-check when we pass &lt;code&gt;HTTPRequestMethod.GET&lt;/code&gt; as an argument for the &lt;code&gt;method&lt;/code&gt; parameter:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// OK, no type error
fetchJSON(&amp;quot;https://example.com/&amp;quot;, HTTPRequestMethod.GET).then(data =&amp;gt; {
  // ...
});
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;However, some developers don&amp;#39;t like to use TypeScript enums in their code because the &lt;code&gt;enum&lt;/code&gt; syntax is not valid JavaScript on its own. The TypeScript compiler will emit the following JavaScript code for our &lt;code&gt;HTTPRequestMethod&lt;/code&gt; enum defined above:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-js&quot;&gt;var HTTPRequestMethod;
(function (HTTPRequestMethod) {
  HTTPRequestMethod[&amp;quot;CONNECT&amp;quot;] = &amp;quot;CONNECT&amp;quot;;
  HTTPRequestMethod[&amp;quot;DELETE&amp;quot;] = &amp;quot;DELETE&amp;quot;;
  HTTPRequestMethod[&amp;quot;GET&amp;quot;] = &amp;quot;GET&amp;quot;;
  HTTPRequestMethod[&amp;quot;HEAD&amp;quot;] = &amp;quot;HEAD&amp;quot;;
  HTTPRequestMethod[&amp;quot;OPTIONS&amp;quot;] = &amp;quot;OPTIONS&amp;quot;;
  HTTPRequestMethod[&amp;quot;PATCH&amp;quot;] = &amp;quot;PATCH&amp;quot;;
  HTTPRequestMethod[&amp;quot;POST&amp;quot;] = &amp;quot;POST&amp;quot;;
  HTTPRequestMethod[&amp;quot;PUT&amp;quot;] = &amp;quot;PUT&amp;quot;;
  HTTPRequestMethod[&amp;quot;TRACE&amp;quot;] = &amp;quot;TRACE&amp;quot;;
})(HTTPRequestMethod || (HTTPRequestMethod = {}));
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&amp;#39;s entirely up to you to decide whether you want to use plain object literals or proper TypeScript enums. If you want to stay as close to JavaScript as possible and only use TypeScript for type annotations, you can stick with plain object literals and &lt;code&gt;const&lt;/code&gt; assertions. If you don&amp;#39;t mind using non-standard syntax for defining enums and you like the brevity, TypeScript enums could be a good choice.&lt;/p&gt;
&lt;h2&gt;&lt;code&gt;const&lt;/code&gt; Assertions for Other Types&lt;/h2&gt;
&lt;p&gt;You can apply a &lt;code&gt;const&lt;/code&gt; assertion to …&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;string literals,&lt;/li&gt;
&lt;li&gt;numeric literals,&lt;/li&gt;
&lt;li&gt;boolean literals,&lt;/li&gt;
&lt;li&gt;array literals, and&lt;/li&gt;
&lt;li&gt;object literals.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;For example, you could define an &lt;code&gt;ORIGIN&lt;/code&gt; variable describing the origin in 2-dimensional space like this:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const ORIGIN = {
  x: 0,
  y: 0,
} as const;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is equivalent to (and much more succinct than) the following declaration:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;const ORIGIN: {
  readonly x: 0;
  readonly y: 0;
} = {
  x: 0,
  y: 0,
};
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Alternatively, you could&amp;#39;ve modeled the representation of a point as a tuple of the X and Y coordinates:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: readonly [0, 0]
const ORIGIN = [0, 0] as const;
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Because of the &lt;code&gt;const&lt;/code&gt; assertion, &lt;code&gt;ORIGIN&lt;/code&gt; is typed as &lt;code&gt;readonly [0, 0]&lt;/code&gt;. Without the assertion, &lt;code&gt;ORIGIN&lt;/code&gt; would&amp;#39;ve been inferred to have type &lt;code&gt;number[]&lt;/code&gt; instead:&lt;/p&gt;
&lt;pre&gt;&lt;code class=&quot;language-ts&quot;&gt;// Type: number[]
const ORIGIN = [0, 0];
&lt;/code&gt;&lt;/pre&gt;</content>
  <media:thumbnail url="https://mariusschulz.com/_astro/default_post_preview_image.D_c0mIAV.jpg" />
</entry>
</feed>