<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Enodi Audu</title>
    <description>The latest articles on DEV Community by Enodi Audu (@enodi).</description>
    <link>https://dev.to/enodi</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1669854%2F574d49cf-6683-4ddb-85e0-b23ff946148a.png</url>
      <title>DEV Community: Enodi Audu</title>
      <link>https://dev.to/enodi</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/enodi"/>
    <language>en</language>
    <item>
      <title>How to Build an E-commerce Store with Sanity and Next.js</title>
      <dc:creator>Enodi Audu</dc:creator>
      <pubDate>Tue, 16 Jul 2024 11:01:24 +0000</pubDate>
      <link>https://dev.to/enodi/how-to-build-an-e-commerce-store-with-sanity-and-nextjs-4099</link>
      <guid>https://dev.to/enodi/how-to-build-an-e-commerce-store-with-sanity-and-nextjs-4099</guid>
      <description>&lt;p&gt;Building an e-commerce store can be daunting, especially when dealing with loads of data that don’t change often but still need to be readily available and up-to-date.&lt;/p&gt;

&lt;p&gt;That's where tools like Sanity and Next.js come in handy. Sanity is a powerful headless CMS that allows you to manage your content effortlessly, while Next.js is a React framework that makes it easy to create fast, dynamic web applications. Together, they offer a robust solution for building and maintaining an e-commerce store efficiently.&lt;/p&gt;

&lt;p&gt;In this article, we’ll walk through the step-by-step process of setting up an e-commerce store using Sanity and Next.js. We’ll cover everything from configuring your backend in Sanity to creating a responsive frontend with Next.js. By the end, you'll have a solid foundation for building scalable and maintainable online stores with ease.&lt;/p&gt;

&lt;p&gt;Here is a screenshot of what the e-commerce store will look like. You can also check out the live app &lt;a href="https://ecommerce-store-eight-green.vercel.app" rel="noopener noreferrer"&gt;here&lt;/a&gt;. You can also review the code &lt;a href="https://github.com/enodi/ecommerce-store" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Table of Contents&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Step 1: Install Nextjs&lt;/li&gt;
&lt;li&gt;Step 2: Setup Chakra UI&lt;/li&gt;
&lt;li&gt;Step 3: Setup Sanity Studio&lt;/li&gt;
&lt;li&gt;Step 4: Update Content Schemas&lt;/li&gt;
&lt;li&gt;Step 5: Query Data using GROQ&lt;/li&gt;
&lt;li&gt;Step 6: Display Content in your Ecommerce App&lt;/li&gt;
&lt;li&gt;Step 7: Deploy Sanity Studio&lt;/li&gt;
&lt;li&gt;Step 8: Deploy to Vercel&lt;/li&gt;
&lt;li&gt;Step 9: Next Steps&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  1. Install Nextjs
&lt;/h2&gt;

&lt;p&gt;Open a terminal and run this command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx create-next-app@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will install the latest version of Next.js.&lt;/p&gt;

&lt;p&gt;We'll name our project &lt;code&gt;sanity-nextjs-ecommerce-store&lt;/code&gt; but you can pick any name you like. We'll use the following options to set up our Next.js app.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;✔ What is your project named? … nextjs-sanity-ecommerce-store
✔ Would you like to use TypeScript? … Yes
✔ Would you like to use ESLint? … Yes
✔ Would you like to use Tailwind CSS? … Yes
✔ Would you like to use `src/` directory? … Yes
✔ Would you like to use App Router? (recommended) … Yes
✔ Would you like to customize the default import alias (@/*)? … Yes
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will install the required dependencies including TailwindCSS which we will use to style our web app.&lt;/p&gt;

&lt;p&gt;To see the application, run the commands below:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;cd nextjs-sanity-ecommerce-store

npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This should start a server on port &lt;code&gt;3000&lt;/code&gt;. Open your browser and go to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;localhost:3000&lt;/a&gt; to see it in action.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Setup Chakra UI
&lt;/h2&gt;

&lt;p&gt;Chakra UI is a React component library that provides customizable, accessible, and reusable UI components to build responsive web applications.&lt;/p&gt;

&lt;p&gt;Chakra UI offers ready-to-use, reusable components, and we will be using some of them in our application, like the Drawer and Button components.&lt;/p&gt;

&lt;p&gt;In your root directory, run this command or follow &lt;a href="https://v2.chakra-ui.com/getting-started/nextjs-app-guide" rel="noopener noreferrer"&gt;this documentation&lt;/a&gt; to set up Chakra in your Next.js project (App Router).&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm i @chakra-ui/react @chakra-ui/next-js @emotion/react @emotion/styled framer-motion
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will install all the necessary dependencies to run Chakra UI.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setup Provider
&lt;/h3&gt;

&lt;p&gt;In the &lt;code&gt;src&lt;/code&gt; directory, create a folder named &lt;code&gt;lib&lt;/code&gt;, and inside it, create another folder named &lt;code&gt;chakra&lt;/code&gt;. Then, create a file called &lt;code&gt;ChakraProvider.tsx&lt;/code&gt; within the &lt;code&gt;chakra&lt;/code&gt; folder.&lt;/p&gt;

&lt;p&gt;Copy and paste this code into &lt;code&gt;ChakraProvider.tsx&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/lib/chakra/ChakraProvider.tsx

"use client";
import { ChakraProvider as Provider } from "@chakra-ui/react";

import { theme } from "@/lib/chakra/theme";

export function ChakraProvider({ children }: { children: React.ReactNode }) {
  return &amp;lt;Provider theme={theme}&amp;gt;{children}&amp;lt;/Provider&amp;gt;;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create a file named &lt;code&gt;theme.ts&lt;/code&gt; inside the &lt;code&gt;chakra&lt;/code&gt; folder, then copy and paste this code into &lt;code&gt;theme.ts&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/lib/chakra/theme.ts

import { extendTheme } from "@chakra-ui/react";

export const theme = extendTheme({
  fonts: {
    heading: 'var(--font-lato)',
    body: 'var(--font-lato)',
  }
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Create another file named &lt;code&gt;fonts.ts&lt;/code&gt; inside the &lt;code&gt;chakra&lt;/code&gt; folder, then copy and paste this code into &lt;code&gt;fonts.ts&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/lib/chakra/fonts.ts

import { Lato } from 'next/font/google'

const lato = Lato({
  weight: "400",
  subsets: ["latin"],
  variable: "--font-lato"
})

export const fonts = {
  lato
}

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

&lt;/div&gt;

&lt;p&gt;These files instruct Chakra to use the &lt;code&gt;Lato&lt;/code&gt; font as the default font for the application.&lt;/p&gt;

&lt;h3&gt;
  
  
  Use Chakra Provider
&lt;/h3&gt;

&lt;p&gt;Navigate to the &lt;code&gt;layout.tsx&lt;/code&gt; component within your &lt;code&gt;src/app&lt;/code&gt; folder and use Chakra Provider within the layout component.&lt;/p&gt;

&lt;p&gt;This is what it will look like:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/app/layout.tsx

import type { Metadata } from "next";

import "@/app/globals.css";
import { fonts } from "@/lib/chakra/fonts";
import { ChakraProvider } from "@/lib/chakra/ChakraProvider";

export const metadata: Metadata = {
  title: "Create Next App", // You can update this
  description: "Generated by create next app", // You can update this
};

export default function RootLayout({
  children,
}: Readonly&amp;lt;{
  children: React.ReactNode;
}&amp;gt;) {
  return (
    &amp;lt;html lang="en" className={fonts.lato.variable}&amp;gt;
      &amp;lt;body&amp;gt;
        &amp;lt;ChakraProvider&amp;gt;{children}&amp;lt;/ChakraProvider&amp;gt;
      &amp;lt;/body&amp;gt;
    &amp;lt;/html&amp;gt;
  );
}

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

&lt;/div&gt;
&lt;h2&gt;
  
  
  3. Setup Sanity Studio
&lt;/h2&gt;

&lt;p&gt;Sanity Studio is Sanity's open-source UI tool for managing website content. It allows you to add, edit, and delete data like text, images etc within Sanity.&lt;/p&gt;

&lt;p&gt;If you don't have a Sanity account, create one &lt;a href="https://www.sanity.io" rel="noopener noreferrer"&gt;here&lt;/a&gt; and create a new project.&lt;/p&gt;
&lt;h3&gt;
  
  
  Log into Sanity from your terminal
&lt;/h3&gt;

&lt;p&gt;If you already have an account and have logged in previously from your terminal, you can skip this step. Otherwise, run this command in your terminal:&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npx sanity login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;This will authenticate you using the sanity.io API.&lt;/p&gt;

&lt;h3&gt;
  
  
  Install Sanity Studio
&lt;/h3&gt;

&lt;p&gt;Within your project directory, run this command:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;npm create sanity@latest
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;If you created a project during your account setup, you can continue using that project or create a new one from your terminal. In this article, we'll use an existing project that was set up when creating a Sanity account. You can configure your project using the options provided below.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;? Select project to use: **ecommerce-store**
? Select dataset to use: **production**
? Would you like to add configuration files for a Sanity project in this Next.js folder? **No**
? Project output path: /Users/USER/Desktop/nextjs-sanity-ecommerce-store/**studio**
? Select project template Clean project with no predefined schema types
? Do you want to use TypeScript? **Yes**
? Package manager to use for installing dependencies? **npm**
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;After completing the setup, Sanity Studio will be installed locally. To view the studio, run these commands:&lt;/p&gt;

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

npm run dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Navigate to &lt;code&gt;localhost:3333&lt;/code&gt;. Log in using the same method you used to create your account, and you should see the studio running locally.&lt;/p&gt;

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

&lt;h2&gt;
  
  
  4. Update Content Schemas
&lt;/h2&gt;

&lt;p&gt;If you've successfully set up and logged in to Sanity Studio locally, you should see a display similar to the screenshot below:&lt;/p&gt;

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

&lt;p&gt;Currently, our studio is empty because we haven't defined any schemas yet. Next, we'll define our schemas.&lt;/p&gt;

&lt;p&gt;Schemas are like blueprints that outline how different types of content should be structured. They describe what fields each piece of content can have and what kind of information those fields can hold. This helps keep content organized and manageable.&lt;/p&gt;

&lt;p&gt;For this e-commerce store, we create two schemas: one for &lt;code&gt;Product&lt;/code&gt; and another for &lt;code&gt;Category&lt;/code&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Product Schema&lt;/code&gt;: This schema defines how a product is structured within our store.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Category Schema&lt;/code&gt;: This schema defines how product categories are structured within our store.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Product Schema
&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;code&gt;product.ts&lt;/code&gt; inside the &lt;code&gt;schemaTypes&lt;/code&gt; directory located within the &lt;code&gt;studio&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Copy and paste the code below into &lt;code&gt;product.ts&lt;/code&gt; file&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//studio/schemaTypes/products.ts

import { defineType, defineField } from "sanity";

export const productType = defineType({
  title: "Product",
  name: "product",
  type: "document",
  fields: [
    defineField({
      title: "Product Name",
      name: "name",
      type: "string",
      validation: (Rule) =&amp;gt; Rule.required()
    }),
    defineField({
      title: "Product Images",
      name: "images",
      type: "array",
      of: [
        {
          type: "image",
          fields: [
            {
              name: "alt",
              title: "Alt Text",
              type: "string",
            },
          ],
        },
      ],
    }),
    defineField({
      title: "Product Description",
      name: "description",
      type: "text",
      validation: (Rule) =&amp;gt; Rule.required()
    }),
    defineField({
      title: "Product Slug",
      name: "slug",
      type: "slug",
      validation: (Rule) =&amp;gt; Rule.required(),
      options: {
        source: "name"
      }
    }),
    defineField({
      title: "Product Price",
      name: "price",
      type: "number",
      validation: (Rule) =&amp;gt; Rule.required()
    }),
    defineField({
      title: "Product Category",
      name: "category",
      type: "reference",
      to: [{ type: "category" }]
    })
  ]
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  Category Schema
&lt;/h3&gt;

&lt;p&gt;Create a file named &lt;code&gt;category.ts&lt;/code&gt; inside the &lt;code&gt;schemaTypes&lt;/code&gt; directory located within the &lt;code&gt;studio&lt;/code&gt; directory.&lt;/p&gt;

&lt;p&gt;Copy and paste the code below into &lt;code&gt;category.ts&lt;/code&gt; file&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//studio/schemaTypes/category.ts

import { defineType, defineField } from "sanity";

export const categoryType = defineType({
  title: "Category",
  name: "category",
  type: "document",
  fields: [
    defineField({
      title: "Category Name",
      name: "name",
      type: "string",
      validation: (Rule) =&amp;gt; Rule.required()
    })
  ]
})
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Update &lt;code&gt;index.ts&lt;/code&gt; to look like this:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//studio/schemaTypes/index.ts

import { productType } from "./product"
import { categoryType } from "./category"

export const schemaTypes = [
  productType,
  categoryType
]

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

&lt;/div&gt;

&lt;p&gt;Each schema and field needs to include the &lt;code&gt;name&lt;/code&gt;, and &lt;code&gt;type&lt;/code&gt; properties. Here's a quick overview of each property's role:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The &lt;strong&gt;name&lt;/strong&gt; property serves as the identifier for referencing the schema in query language contexts. It must be unique to prevent schema conflicts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type&lt;/strong&gt; indicates the specific schema type being defined. Setting it to &lt;code&gt;document&lt;/code&gt; instructs the studio to enable the creation of new documents.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Visit your studio
&lt;/h3&gt;

&lt;p&gt;Navigate to the &lt;code&gt;studio&lt;/code&gt; directory from your terminal and run &lt;code&gt;npm run dev&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Open your browser and go to &lt;a href="http://localhost:3333" rel="noopener noreferrer"&gt;localhost:3333&lt;/a&gt;. &lt;/p&gt;

&lt;p&gt;This is what it should now look like:&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Populate your Schemas
&lt;/h3&gt;

&lt;p&gt;Begin by creating categories, followed by creating products within those categories. Create as many categories and products as you want.&lt;/p&gt;

&lt;p&gt;This is what it should now look like:&lt;/p&gt;

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

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

&lt;p&gt;Next, we will query the product and category data to use within the application.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Query Data using GROQ
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.sanity.io/docs/groq" rel="noopener noreferrer"&gt;GROQ (Graph-Relational Object Queries)&lt;/a&gt; is the query language developed by Sanity for querying structured content in their backend. It enables retrieval and manipulation of data stored in Sanity's content lake.&lt;/p&gt;

&lt;p&gt;To query product and category data, we'll use a library named &lt;code&gt;@sanity/client&lt;/code&gt;. This library offers methods for querying, creating, updating, and deleting documents in Sanity. It is designed for use in both server-side and client-side JavaScript applications.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;lib&lt;/code&gt; directory, create a new directory named &lt;code&gt;sanity&lt;/code&gt;, and within it, create a file called &lt;code&gt;client.ts&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Then, copy and paste the code below into &lt;code&gt;client.ts&lt;/code&gt;.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/lib/sanity/client.ts

import { createClient, type ClientConfig } from "@sanity/client";

const config: ClientConfig = {
  projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID,
  dataset: process.env.NEXT_PUBLIC_SANITY_DATASET,
  useCdn: false
};

const client = createClient(config);

export default client;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Replace &lt;code&gt;process.env.NEXT_PUBLIC_SANITY_PROJECT_ID&lt;/code&gt; and &lt;code&gt;process.env.NEXT_PUBLIC_SANITY_DATASET&lt;/code&gt; with the correct &lt;code&gt;projectId&lt;/code&gt; and &lt;code&gt;dataset&lt;/code&gt; values.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;projectId&lt;/code&gt;: This is your project's ID. You can obtain it by running the command &lt;code&gt;npx sanity projects list&lt;/code&gt; in your terminal or by visiting the projects page in your Sanity account. See the screenshot below.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;dataset&lt;/code&gt;: This is the name of your project's dataset. By default, it is &lt;code&gt;production&lt;/code&gt;, but if you create a new dataset, you should update it to reflect your dataset name.&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Create 3 new files named &lt;code&gt;product-query.ts&lt;/code&gt;, &lt;code&gt;category-query.ts&lt;/code&gt;, and &lt;code&gt;types.ts&lt;/code&gt;. Copy and paste the following code into it:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/lib/sanity/product-query.ts

import { groq } from "next-sanity";
import client from "./client";

export async function getProducts() {
  return client.fetch(
    groq`*[_type == "product"] {
      _id,
      "categoryName": category-&amp;gt;name,
      description,
      name,
      price,
      "productImage": {"alt": images[0].alt, "imageUrl": images[0].asset-&amp;gt;url},
      "slug": slug.current
    }`
  );
}

export async function getSelectedProducts(selectedCategory: string) {
  return client.fetch(
    groq`*[_type == "product" &amp;amp;&amp;amp; category-&amp;gt;name == $selectedCategory] {
      _id,
      "categoryName": category-&amp;gt;name,
      description,
      name,
      price,
      "productImage": {"alt": images[0].alt, "imageUrl": images[0].asset-&amp;gt;url},
      "slug": slug.current
    }`,
    {selectedCategory}
  );
}

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

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;getProducts&lt;/code&gt; function retrieves all products, whereas the &lt;code&gt;getSelectedProducts&lt;/code&gt; function fetches specific products based on the chosen category.&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/lib/sanity/category-query.ts

import { groq } from "next-sanity";
import client from "./client";

export async function getCategories() {
  return client.fetch(
    groq`*[_type == "category"] {
      _id,
      name,
    }`
  );
}

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

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;getCategories&lt;/code&gt; function retrieves all categories.&lt;/p&gt;

&lt;p&gt;You'll notice that the GROQ query begins with an asterisk (*), representing all documents in your dataset, followed by a filter in brackets. The filter used here returns documents with a &lt;code&gt;_type&lt;/code&gt; of "product" or "category".&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/lib/sanity/types.ts

export type ProductType = {
  _id: string,
  name: string,
  productImage: {
    alt: string,
    imageUrl: string
  },
  slu: string,
  categoryName: string,
  description: string,
  price: number,
};

export type CategoryType = {
  _id: string,
  name: string,
};

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

&lt;/div&gt;

&lt;p&gt;The &lt;code&gt;ProductType&lt;/code&gt; and &lt;code&gt;CategoryType&lt;/code&gt; define the structure of the &lt;code&gt;Product&lt;/code&gt; and &lt;code&gt;Category&lt;/code&gt; objects.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. Display Content in your E-commerce App
&lt;/h2&gt;

&lt;p&gt;It's now time for us to display the content within the e-commerce application.&lt;/p&gt;

&lt;p&gt;Begin by stripping all styles from the &lt;code&gt;globals.css&lt;/code&gt; file, leaving only essential Tailwind imports at the beginning. Next, erase the contents of your root &lt;code&gt;page.tsx&lt;/code&gt; file in your Next.js application and replace it with the following code:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/app/page.tsx

"use client";

import { useDisclosure } from "@chakra-ui/react";
import { Fragment, useEffect, useState } from "react";

import Hero from "@/app/components/hero/Hero";
import Cart from "@/app/components/cart/Cart";
import Navbar from "@/app/components/navbar/Navbar";
import Products from "@/app/components/products/Products";
import { getCategories } from "@/lib/sanity/category-query";
import { ProductType, CategoryType } from "@/lib/sanity/types";
import { getProducts, getSelectedProducts } from "@/lib/sanity/product-query";

export default function Home() {
  const { isOpen, onOpen, onClose } = useDisclosure();
  const [products, setProducts] = useState&amp;lt;ProductType[]&amp;gt;([]);
  const [categories, setCategories] = useState&amp;lt;CategoryType[]&amp;gt;([]);
  const [selectedCategory, setSelectedCategory] = useState&amp;lt;string&amp;gt;("");

  const [cartItems, setCartItems] = useState&amp;lt;ProductType[]&amp;gt;([]);
  const [cartItemsCount, setCartItemsCount] = useState&amp;lt;number&amp;gt;(0);

  const localStorageCartItem =
    typeof window !== "undefined" &amp;amp;&amp;amp; localStorage.getItem("cart");
  const parsedCartItems =
    localStorageCartItem &amp;amp;&amp;amp; JSON.parse(localStorageCartItem);
  const itemsInCart = cartItems.length &amp;gt; 0 ? cartItems : parsedCartItems;

  const localStorageCartItemCount =
    typeof window !== "undefined" &amp;amp;&amp;amp; localStorage.getItem("cartCount");
  const cartCount: number =
    localStorageCartItemCount &amp;amp;&amp;amp; JSON.parse(localStorageCartItemCount);
  const itemCount = cartItemsCount || cartCount;

  useEffect(() =&amp;gt; {
    async function fetchProducts() {
      const allProducts: ProductType[] = await getProducts();
      setProducts(allProducts);
    }
    fetchProducts();
  }, []);

  useEffect(() =&amp;gt; {
    async function fetchCategories() {
      const allCategories: CategoryType[] = await getCategories();
      setCategories(allCategories);
    }
    fetchCategories();
  }, []);

  const handleDrawerOpen = () =&amp;gt; onOpen();

  const handleProductFilter = async (category: string) =&amp;gt; {
    let product: ProductType[] = [];
    if (!!category) {
      product = await getSelectedProducts(category);
    } else {
      product = await getProducts();
    }
    setProducts(product);
    setSelectedCategory(category);
  };

  const addCartItem = (product: ProductType) =&amp;gt; {
    let cart: ProductType[] = [];
    const count = cartCount + 1;
    const products = [];
    products.push(product);

    if (!!itemsInCart) {
      cart = [...itemsInCart, ...products];
    } else {
      cart = [...products];
    }

    setCartItems(cart);
    setCartItemsCount(count);

    updateLocalStorage(count, cart);
  };

  const removeItemFromCart = (product: ProductType) =&amp;gt; {
    const count = cartCount - 1;
    const filteredItems = itemsInCart.filter(
      (item: ProductType) =&amp;gt; item._id !== product._id
    );

    setCartItems(filteredItems);
    setCartItemsCount(count);

    updateLocalStorage(count, filteredItems);
  };

  const updateLocalStorage = (count: number, cart: ProductType[]) =&amp;gt; {
    if (typeof window !== "undefined") {
      localStorage.setItem("cartCount", JSON.stringify(count));
      localStorage.setItem("cart", JSON.stringify(cart));
    }
  };

  return (
    &amp;lt;Fragment&amp;gt;
      &amp;lt;Navbar handleDrawerOpen={handleDrawerOpen} itemCount={itemCount} /&amp;gt;
      &amp;lt;main&amp;gt;
        &amp;lt;Hero
          categories={categories}
          handleProductFilter={handleProductFilter}
        /&amp;gt;
        &amp;lt;Products
          products={products}
          selectedCategory={selectedCategory}
          addCartItem={addCartItem}
        /&amp;gt;
      &amp;lt;/main&amp;gt;
      &amp;lt;Cart
        isOpen={isOpen}
        onClose={onClose}
        itemsInCart={itemsInCart}
        removeItemFromCart={removeItemFromCart}
      /&amp;gt;
    &amp;lt;/Fragment&amp;gt;
  );
}

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

&lt;/div&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//src/app/globals.css

@tailwind base;
@tailwind components;
@tailwind utilities;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;Please review &lt;a href="https://github.com/enodi/ecommerce-store" rel="noopener noreferrer"&gt;this repository&lt;/a&gt; for additional components like &lt;code&gt;Navbar&lt;/code&gt;, &lt;code&gt;Hero&lt;/code&gt;, &lt;code&gt;Products&lt;/code&gt;, and &lt;code&gt;Cart&lt;/code&gt; used in &lt;code&gt;page.tsx&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Add &lt;code&gt;localhost:3000&lt;/code&gt; to CORS Origin
&lt;/h3&gt;

&lt;p&gt;After setting up your frontend application and including all necessary components, launch your Next.js application with &lt;code&gt;npm run dev&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This will start your Next.js app on port &lt;code&gt;3000&lt;/code&gt;. Open your browser and navigate to &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;localhost:3000&lt;/a&gt;. You'll encounter a CORS error preventing your application from loading.&lt;/p&gt;

&lt;p&gt;To resolve this issue, include &lt;a href="http://localhost:3000" rel="noopener noreferrer"&gt;localhost:3000&lt;/a&gt; to the list of allowed hosts that can connect to your project's API.&lt;/p&gt;

&lt;p&gt;You can achieve this via the terminal using &lt;code&gt;npx sanity cors add http://localhost:3000&lt;/code&gt;, or by logging into your Sanity account and adding &lt;code&gt;localhost:3000&lt;/code&gt; to the CORS origin list. Refer to the screenshot below for guidance.&lt;/p&gt;

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

&lt;p&gt;Now, restart your application, and you should see a list of products that were added through your Sanity Studio.&lt;/p&gt;

&lt;p&gt;You have the option to add products to your cart, remove them, and filter the product list by category.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. Deploy Sanity Studio
&lt;/h2&gt;

&lt;p&gt;Once your application is up and running locally, you can synchronize your schemas with your remote Sanity Studio by running &lt;code&gt;npx sanity deploy&lt;/code&gt;. This ensures that your remote studio reflects the latest changes made locally.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. Deploy to Vercel
&lt;/h2&gt;

&lt;p&gt;To make your e-commerce store accessible online, deploy it using &lt;a href="https://vercel.com" rel="noopener noreferrer"&gt;Vercel&lt;/a&gt;. Vercel provides seamless deployment for Next.js applications, ensuring your site is fast and reliable. Simply link your GitHub repository to Vercel and trigger automatic deployments with every push to your main branch. Once deployed, your store will be live and accessible to users worldwide.&lt;/p&gt;

&lt;h2&gt;
  
  
  9. Next Steps
&lt;/h2&gt;

&lt;p&gt;While this tutorial has provided a solid foundation, there are numerous ways to further enhance your e-commerce store using Sanity.&lt;/p&gt;

&lt;p&gt;Thank you for reading! Remember to like, share, and follow for future updates and more insightful content. Until next time, happy coding!&lt;/p&gt;

</description>
      <category>sanity</category>
      <category>cms</category>
      <category>headless</category>
      <category>nextjs</category>
    </item>
    <item>
      <title>Docker Cheatsheet: Essential Commands You Need to Know</title>
      <dc:creator>Enodi Audu</dc:creator>
      <pubDate>Tue, 09 Jul 2024 15:16:50 +0000</pubDate>
      <link>https://dev.to/enodi/docker-cheatsheet-essential-commands-you-need-to-know-16gk</link>
      <guid>https://dev.to/enodi/docker-cheatsheet-essential-commands-you-need-to-know-16gk</guid>
      <description>&lt;p&gt;Docker has become a go-to tool for developers, simplifying the process of creating, deploying, and running applications using containers. Here’s a handy cheat sheet of essential Docker commands to help you navigate Docker like a pro.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Docker Basics
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker --version&lt;/strong&gt;
This shows you the current version of Docker you have installed.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker info&lt;/strong&gt;
This will list detailed information about your Docker setup.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  2. Working with Images
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker pull [image]&lt;/strong&gt;
This command downloads an image from the Docker Hub.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker pull [image_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker images&lt;/strong&gt;
List all the images you have on your system.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker rmi [image]&lt;/strong&gt;
Remove an image from your system.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker rmi [image_name] e.g docker rmi postgres
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Managing Containers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker run [image]&lt;/strong&gt;
Create and start a new container from an image.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker run [image_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker ps&lt;/strong&gt;
List all running containers.
&lt;/li&gt;
&lt;/ul&gt;

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

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker ps -a&lt;/strong&gt;
List all containers, including those that are stopped.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker ps -a
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker stop [container_id]&lt;/strong&gt;
Stop a running container.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker stop [container_id]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker start [container_id]&lt;/strong&gt;
Start a stopped container.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker start [container_id]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker rm [container_id]&lt;/strong&gt;
Remove a stopped container.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker rm [container_id]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4. Accessing Containers
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker exec -it [container_id] /bin/bash&lt;/strong&gt;
Access a running container with an interactive shell.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker exec -it [container_id] /bin/bash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  5. Building Images
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker build -t [name:tag] [path]&lt;/strong&gt;
Build a new image from a Dockerfile.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker build -t [name:tag] [path] e.g docker build -t app:latest .
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  6. Docker Networks
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker network ls&lt;/strong&gt;
List all networks.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker network ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker network create [network_name]&lt;/strong&gt;
Create a new network.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker network create [network_name] e.g docker network create 
  backend_default
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker network rm [network_name]&lt;/strong&gt;
Remove a network.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker network rm [network_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  7. Docker Volumes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker volume ls&lt;/strong&gt;
List all volumes.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker volume ls
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker volume create [volume_name]&lt;/strong&gt;
Create a new volume.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker volume create [volume_name] e.g docker volume create 
  backend_pgdata
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker volume rm [volume_name]&lt;/strong&gt;
Remove a volume.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker volume rm [volume_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  8. Docker Compose
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker-compose up&lt;/strong&gt;
Start and run your Docker Compose services.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker-compose up
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker-compose down&lt;/strong&gt;
Stop and remove Docker Compose services.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker-compose down
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;docker-compose build&lt;/strong&gt;
Build or rebuild services.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  docker-compose build
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This cheatsheet covers the most essential Docker commands to get you started. Whether you’re managing images, containers, networks, or volumes, these commands will help you streamline your workflow and make the most out of Docker.&lt;/p&gt;

&lt;p&gt;If you found this article helpful, please like and share it. Comment below with any other Docker commands or tips you’ve found useful. Happy coding :)&lt;/p&gt;

</description>
      <category>docker</category>
      <category>devops</category>
      <category>productivity</category>
      <category>devrel</category>
    </item>
    <item>
      <title>Top 5 AI-Powered VSCode Extensions Every Developer Needs</title>
      <dc:creator>Enodi Audu</dc:creator>
      <pubDate>Tue, 09 Jul 2024 12:46:20 +0000</pubDate>
      <link>https://dev.to/enodi/top-5-ai-powered-vscode-extensions-every-developer-needs-59cf</link>
      <guid>https://dev.to/enodi/top-5-ai-powered-vscode-extensions-every-developer-needs-59cf</guid>
      <description>&lt;p&gt;In the ever-evolving world of development, finding tools that streamline your workflow is essential. AI is transforming how we code, enhancing productivity and efficiency. In this article, we'll dive into 5 must-have AI-powered VSCode extensions that can help you write better code faster.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. GitHub Copilot
&lt;/h3&gt;

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

&lt;p&gt;If you're a developer, you've likely heard of &lt;a href="https://github.com/features/copilot" rel="noopener noreferrer"&gt;GitHub Copilot&lt;/a&gt;. This AI-powered code completion tool from GitHub acts as an AI pair programmer, providing smart auto-complete suggestions to boost your productivity. Simply describe what you need in plain English, and Copilot will generate the code for you.&lt;/p&gt;

&lt;p&gt;While it’s a paid service, GitHub offers a 30-day free trial to test its capabilities. Some features of GitHub Copilot include:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Code completions:&lt;/strong&gt; Copilot provides autocomplete-style suggestions as you code, offering relevant suggestions as you type.&lt;/p&gt;

&lt;p&gt;b. &lt;strong&gt;Chat:&lt;/strong&gt; A chat interface for asking questions, useful for troubleshooting bugs or building new features.&lt;/p&gt;

&lt;p&gt;c. &lt;strong&gt;Participants:&lt;/strong&gt; The ability to tag domain experts in a chat, ask questions, and get responses.&lt;/p&gt;

&lt;p&gt;d. &lt;strong&gt;Slash commands:&lt;/strong&gt; Shortcuts to specific functionality via slash commands.&lt;/p&gt;

&lt;p&gt;e. &lt;strong&gt;Language support:&lt;/strong&gt; Compatible with languages like Java, PHP, Python, JavaScript, Ruby, Go, C#, C++, and more.&lt;/p&gt;

&lt;p&gt;As of June 2024, GitHub Copilot has &lt;a href="https://www.ciodive.com/news/github-copilot-subscriber-count-revenue-growth/706201/#:~:text=The%20AI%2Dpowered%20coding%20assistant,to%2050%2C000%20developers%20this%20year" rel="noopener noreferrer"&gt;1.3 million paid subscribers&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Tabnine
&lt;/h3&gt;

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

&lt;p&gt;&lt;a href="https://www.tabnine.com" rel="noopener noreferrer"&gt;Tabnine&lt;/a&gt; is another powerful AI code assistant that provides AI chat and code completion for various languages such as JavaScript, Python, TypeScript, PHP, Go, and more.&lt;/p&gt;

&lt;p&gt;Tabnine is also a paid product but offers a generous 90-day free trial. Some features of Tabnine include:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Code Completion:&lt;/strong&gt; Speeds up coding by suggesting the next line or block of code, making your workflow smoother and more efficient.&lt;/p&gt;

&lt;p&gt;b. &lt;strong&gt;Write Test Cases:&lt;/strong&gt; Ask Tabnine to create tests for a specific function or piece of code in your project. It can generate actual test cases, implementations, and assertions. Tabnine can also use existing tests in your project and suggest new tests that align with your testing framework.&lt;/p&gt;

&lt;p&gt;c. &lt;strong&gt;Write Documentation:&lt;/strong&gt; Generate documentation for specific sections of your code to enhance readability and make it easier for other team members to understand.&lt;/p&gt;

&lt;p&gt;d. &lt;strong&gt;Chat:&lt;/strong&gt; Allows developers to generate code by asking questions in natural language, making it easy to get help and suggestions.&lt;/p&gt;

&lt;p&gt;e. &lt;strong&gt;Custom Model:&lt;/strong&gt; Ability to choose your own model tailored to your specific needs and preferences. Some models available to choose from include: the Tabnine model, Codestral, GPT-4o, Claude 3.5 Sonnet etc&lt;/p&gt;

&lt;p&gt;f. &lt;strong&gt;Language/Library Support:&lt;/strong&gt; Tabnine provides extensive support for languages and libraries, including JavaScript, TypeScript, Python, Java, C, C++, C#, Go, PHP, Ruby, Kotlin, Dart, Rust, React/Vue, HTML 5, CSS, Lua, Perl, YAML, Cuda, SQL, Scala, Shell (bash), Swift, R, Julia, VB, Groovy, Matlab, Terraform, ABAP, and more.&lt;/p&gt;

&lt;p&gt;g. &lt;strong&gt;Supported IDEs:&lt;/strong&gt; Tabnine works with various IDEs such as VS Code, JetBrains IDEs (IntelliJ, PyCharm, WebStorm, PhpStorm, Android Studio, GoLand, CLion, Rider, DataGrip, RustRover, RubyMine, DataSpell, Aqua, AppCode), Eclipse, Visual Studio 2022, and others.&lt;/p&gt;

&lt;p&gt;Tabnine has over 7 million downloads.&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Mintlify Doc Writer
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpi76ha5nab0bn1gr24z6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fpi76ha5nab0bn1gr24z6.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://www.mintlify.com/" rel="noopener noreferrer"&gt;Mintlify Doc Writer&lt;/a&gt; is a tool designed to simplify the process of writing documentation. It uses AI to generate clear, concise documentation directly from your code, saving you time and effort.&lt;/p&gt;

&lt;p&gt;Mintlify Doc Writer is available as a paid service, but it offers a free trial to get you started. Key features include:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Automated Documentation:&lt;/strong&gt; Generates documentation for your functions, classes, and modules by analyzing your code, ensuring that your documentation is always up-to-date.&lt;br&gt;&lt;br&gt;
b. &lt;strong&gt;Code Comments:&lt;/strong&gt; Converts your inline code comments into well-structured documentation, making it easier for others to understand your code.&lt;br&gt;&lt;br&gt;
c. &lt;strong&gt;Customization:&lt;/strong&gt; Allows you to customize the generated documentation to match your project's style and requirements.&lt;br&gt;&lt;br&gt;
d. &lt;strong&gt;Integrations:&lt;/strong&gt; Works seamlessly with popular IDEs and code editors, allowing you to generate documentation directly within your development environment.&lt;br&gt;&lt;br&gt;
e. &lt;strong&gt;Multi-language Support:&lt;/strong&gt; Supports a variety of programming languages, including JavaScript, Python, Java, C#, and more.&lt;/p&gt;

&lt;p&gt;Mintlify Doc Writer helps you maintain high-quality documentation with minimal effort, improving code readability and team collaboration.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Red Hat Dependency Analytics
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=redhat.fabric8-analytics" rel="noopener noreferrer"&gt;Red Hat Dependency Analytics&lt;/a&gt; is an AI-powered extension that helps developers manage dependencies and security vulnerabilities in their projects. It provides detailed insights and recommendations to ensure your code is secure and up-to-date.&lt;/p&gt;

&lt;p&gt;Red Hat Dependency Analytics offers a range of features, including:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Vulnerability Detection:&lt;/strong&gt; Automatically scans your project for known vulnerabilities in dependencies and provides suggestions for safer alternatives.&lt;br&gt;
b. &lt;strong&gt;Dependency Management:&lt;/strong&gt; Analyzes your project's dependencies and suggests updates to keep your software secure and compliant.&lt;br&gt;
c. &lt;strong&gt;Actionable Insights:&lt;/strong&gt; Offers detailed reports on the security and health of your dependencies, helping you make informed decisions.&lt;br&gt;
d. &lt;strong&gt;Integration:&lt;/strong&gt; Seamlessly integrates with popular IDEs like VS Code, allowing you to manage dependencies directly within your development environment.&lt;br&gt;
e. &lt;strong&gt;Multi-language Support:&lt;/strong&gt; Supports various programming languages and ecosystems, including JavaScript, Python, Java, and more.&lt;/p&gt;

&lt;p&gt;Red Hat Dependency Analytics helps you maintain a secure and reliable codebase by providing critical insights into your project's dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  5. GPT Commit
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://marketplace.visualstudio.com/items?itemName=DmytroBaida.gpt-commit" rel="noopener noreferrer"&gt;GPT Commit&lt;/a&gt; is an AI tool that assists developers in writing better commit messages. It leverages machine learning to analyze your code changes and suggest clear, concise, and informative commit messages.&lt;/p&gt;

&lt;p&gt;GPT Commit is available as a paid service with a free trial option. Key features include:&lt;/p&gt;

&lt;p&gt;a. &lt;strong&gt;Commit Message Suggestions:&lt;/strong&gt; Automatically generates commit messages based on your code changes, ensuring that your commit history is clear and informative.&lt;br&gt;&lt;br&gt;
b. &lt;strong&gt;Consistency:&lt;/strong&gt; Helps maintain consistency in your commit messages, making your project's history easier to understand.&lt;br&gt;&lt;br&gt;
c. &lt;strong&gt;Customization:&lt;/strong&gt; Allows you to customize the format and style of the generated commit messages to match your project's conventions.&lt;br&gt;&lt;br&gt;
d. &lt;strong&gt;Integration:&lt;/strong&gt; Works with popular version control systems like Git, integrating seamlessly into your existing workflow.&lt;br&gt;&lt;br&gt;
e. &lt;strong&gt;Language Support:&lt;/strong&gt; Supports commit message generation for projects written in various programming languages, including JavaScript, Python, Java, C#, and more.&lt;/p&gt;

&lt;p&gt;GPT Commit enhances your version control practices by ensuring that your commit messages are always well-written and informative, improving the overall quality of your project's history.&lt;/p&gt;

&lt;p&gt;These tools showcase the power of AI in enhancing productivity and efficiency in software development. By integrating these AI-powered extensions into your workflow, you can streamline your coding process and produce high-quality code more efficiently.&lt;/p&gt;

&lt;p&gt;I hope you've enjoyed this article. Comment below with any other tools you’ve found helpful. Happy coding :)&lt;/p&gt;

</description>
      <category>ai</category>
      <category>productivity</category>
      <category>programming</category>
      <category>vscode</category>
    </item>
    <item>
      <title>TypeScript 5.5: Exciting New Features</title>
      <dc:creator>Enodi Audu</dc:creator>
      <pubDate>Wed, 03 Jul 2024 14:53:19 +0000</pubDate>
      <link>https://dev.to/enodi/typescript-55-new-features-27ef</link>
      <guid>https://dev.to/enodi/typescript-55-new-features-27ef</guid>
      <description>&lt;p&gt;TypeScript has become increasingly popular among developers as a more structured alternative to JavaScript. It helps define types within your code, making it easier to catch errors like typos etc on time.&lt;/p&gt;

&lt;p&gt;Recently, TypeScript rolled out version 5.5, introducing several new features. In this article, we’ll take a closer look at four(4) of these new features and explain them in a simple, easy-to-understand manner.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. &lt;strong&gt;Inferred Type Predicates&lt;/strong&gt;
&lt;/h3&gt;

&lt;p&gt;One of the key improvements in TypeScript 5.5 is better type inference, especially with arrays and filtering.&lt;/p&gt;

&lt;p&gt;What does this mean? As your code progresses, the type of a variable can change. With Inferred Type Predicates, TypeScript now adjusts the type definitions accordingly.&lt;/p&gt;

&lt;p&gt;Let's look at an example:&lt;/p&gt;

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

const namesAndAges = ["Elijah", "Sophia", "Liam", "Isabella", "Mason", 23, 24];

const ages = namesAndAges.filter(age =&amp;gt; typeof age === 'number');

ages.forEach((age) =&amp;gt; console.log(age + 1));


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

&lt;/div&gt;

&lt;p&gt;In TypeScript 5.0(Previous Versions):&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd6muhn4evx6nhp0904n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzd6muhn4evx6nhp0904n.png" alt="Inferred Type in TypeScript 5.0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, &lt;code&gt;namesAndAges&lt;/code&gt; is an array of type &lt;code&gt;(string|number)[]&lt;/code&gt;. We're filtering out non-numeric values, leaving us with an array of numbers (ages). However, TypeScript 5.0 still sees the array &lt;code&gt;ages&lt;/code&gt; as &lt;code&gt;(string|number)[]&lt;/code&gt;, causing an error when trying to add 1 to &lt;code&gt;age&lt;/code&gt; due to potential string types.&lt;/p&gt;

&lt;p&gt;Previously, we might have needed to explicitly cast &lt;code&gt;age&lt;/code&gt; to &lt;code&gt;number&lt;/code&gt; like this:&lt;/p&gt;

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

ages.forEach((age) =&amp;gt; console.log(age as number + 1));


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

&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;TypeScript 5.5&lt;/strong&gt; simplifies this process:&lt;br&gt;
With TypeScript 5.5, TypeScript handles this type of inference more accurately, as shown below:&lt;/p&gt;

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

&lt;p&gt;This improvement makes type inference a lot better making TypeScript more intuitive and effective in catching potential errors early.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Control Flow Narrowing for Constant Indexed Accesses
&lt;/h3&gt;

&lt;p&gt;Another significant enhancement in TypeScript 5.5 is better type narrowing for accessing object properties.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does this mean?&lt;/strong&gt;&lt;br&gt;
Let's break it down with an example:&lt;/p&gt;

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

type ObjType = Record&amp;lt;string, number | string&amp;gt;;

function downCase(obj: ObjType, key: string) {
    if (typeof obj[key] === "string") {
        console.log(obj[key].toLowerCase());
    }
}


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

&lt;/div&gt;

&lt;p&gt;In TypeScript 5.0(Previous Versions):&lt;/p&gt;

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

&lt;p&gt;In this example, the &lt;code&gt;downCase&lt;/code&gt; function checks if the property's value is of type string, and if so, converts it to lowercase. However, the previous TypeScript versions can’t be sure if &lt;code&gt;obj[key]&lt;/code&gt; is a number or a string, leading to an error when trying to use the &lt;code&gt;toLowerCase&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;To avoid this in previous versions, we could do something like this:&lt;/p&gt;

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

type ObjType = Record&amp;lt;string, number | string&amp;gt;;

function downCase(obj: ObjType, key: string) {
    const value = obj[key];
    if (typeof value === "string") {
        console.log(value.toLowerCase());
    }
}


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

&lt;/div&gt;

&lt;p&gt;Here, an intermediary variable &lt;code&gt;value&lt;/code&gt; is used to help TypeScript understand the type.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With TypeScript 5.5&lt;/strong&gt;, this workaround is no longer needed. The new version automatically narrows the type correctly based on the condition &lt;code&gt;typeof obj[key] === "string"&lt;/code&gt; as shown below:&lt;/p&gt;

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

&lt;p&gt;Because Typescript is aware that the property’s value is of type &lt;code&gt;string&lt;/code&gt;, all string methods are available for use as shown below&lt;/p&gt;

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

&lt;h3&gt;
  
  
  3. Regular Expression Syntax Checking
&lt;/h3&gt;

&lt;p&gt;TypeScript 5.5 also brings a useful feature for developers working with regular expressions: syntax checking on regular expressions.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does this mean?&lt;/strong&gt;&lt;br&gt;
Before this update, TypeScript would skip over regular expressions and not validate their syntax, potentially allowing errors to slip through unnoticed. With TypeScript 5.5, basic syntax checks are now performed on regular expressions.&lt;/p&gt;

&lt;p&gt;Let’s explain this with an example:&lt;/p&gt;

&lt;p&gt;In TypeScript 5.0(Previous Versions):&lt;/p&gt;

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

const regex = /hello(world/;


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

&lt;/div&gt;

&lt;p&gt;In the example above, the regular expression &lt;code&gt;/hello(world/&lt;/code&gt; has an unclosed parenthesis &lt;code&gt;(&lt;/code&gt;. Regular expressions require that every opening parenthesis has a corresponding closing parenthesis.&lt;/p&gt;

&lt;p&gt;In previous versions of TypeScript, this mistake would go unnoticed, and no error would be flagged.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;No error was flagged in Typescript v5.0&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdjpkmzue7yb98spon82.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frdjpkmzue7yb98spon82.png" alt="Regular Expression in TypeScript 5.0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With TypeScript 5.5, this regular expression will be flagged as an error because the syntax is incorrect due to the unmatched parenthesis.&lt;/p&gt;

&lt;p&gt;This improvement helps catch common mistakes in regular expressions early, making your code more reliable.&lt;/p&gt;

&lt;p&gt;An error was flagged in TypeScript 5.5&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ex7c93u2w9973wxfefi.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7ex7c93u2w9973wxfefi.png" alt="Regular Expression in Syntax Checking TypeScript 5.5"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The fixed code in TypeScript 5.5&lt;/p&gt;

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

&lt;p&gt;&lt;strong&gt;Note&lt;/strong&gt;: TypeScript’s regular expression support is limited to regular expression literals. If you use &lt;code&gt;new RegExp&lt;/code&gt; with a string literal, TypeScript will not check the provided string's syntax. You can read more &lt;a href="https://devblogs.microsoft.com/typescript/announcing-typescript-5-5/#regular-expression-syntax-checking" rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Support for New ECMAScript Set Methods
&lt;/h3&gt;

&lt;p&gt;TypeScript 5.5 has added support for the new methods introduced to the &lt;code&gt;Set&lt;/code&gt; object in JavaScript. These methods include &lt;code&gt;union&lt;/code&gt;, &lt;code&gt;intersection&lt;/code&gt;, &lt;code&gt;difference&lt;/code&gt;, &lt;code&gt;symmetricDifference&lt;/code&gt;, and more, expanding how you can work with Sets.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What does this mean?&lt;/strong&gt;&lt;br&gt;
These new &lt;code&gt;Set&lt;/code&gt; methods allow for more powerful and concise operations on sets, such as combining or finding common elements between them.&lt;/p&gt;

&lt;p&gt;Let’s break this down with an example:&lt;/p&gt;

&lt;p&gt;In previous TypeScript versions:&lt;/p&gt;

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

let primaryColors = new Set(["red", "blue", "yellow"]);
let secondaryColors = new Set(["green", "orange", "purple"]);

// Trying to use the new Set methods would result in an error
// The previous versions of TypeScript would not recognize these methods
console.log(primaryColors.union(secondaryColors)); // Error: Property 'union' does not exist on type 'Set&amp;lt;string&amp;gt;'
console.log(primaryColors.intersection(secondaryColors)); // Error: Property 'intersection' does not exist on type 'Set&amp;lt;string&amp;gt;'


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

&lt;/div&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0s2wv9pu3ip9y8vrtxbc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0s2wv9pu3ip9y8vrtxbc.png" alt="No Support for New ECMAScript Set Methods in TypeScript 5.0"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before TypeScript 5.5, attempting to use these methods would result in errors because TypeScript didn’t recognize them. This was because these methods were newly introduced in the latest ECMAScript specification and weren’t yet supported by TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;With TypeScript 5.5&lt;/strong&gt;, these new methods are now fully supported. You can use them directly without TypeScript flagging any errors.&lt;/p&gt;

&lt;p&gt;In TypeScript 5.5:&lt;/p&gt;

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

let primaryColors = new Set(["red", "blue", "yellow"]);
let secondaryColors = new Set(["green", "orange", "purple"]);

console.log(primaryColors.union(secondaryColors)); // Combines both sets
console.log(primaryColors.intersection(secondaryColors)); // Finds common elements


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

&lt;/div&gt;

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

&lt;p&gt;TypeScript 5.5 brings significant enhancements like improved type inference, support for new ECMAScript Set methods, and better regular expression syntax checking. Part 2 of this article will cover additional features introduced in version 5.5.&lt;/p&gt;

&lt;p&gt;In case you're wondering, I used TypeScript Playground to test and demonstrate these updates. Explore TypeScript 5.5 features yourself on the &lt;a href="https://www.typescriptlang.org/play" rel="noopener noreferrer"&gt;TypeScript Playground&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;If you're excited about TypeScript 5.5 like I am, drop a like or comment below to share your thoughts on these awesome new features! &lt;/p&gt;

&lt;p&gt;Stay tuned for more updates in Part 2! Until then, happy coding!!! :)&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>frontend</category>
      <category>backend</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
