Error encountered in Typescript: The property 'prevUrl' is expected to be present in the props object, but it appears to be missing

When trying to access the props.prevUrl property, the following error is encountered: Property 'prevUrl' does not exist on type '{ nextUrl: string; } | { prevUrl: string; nextUrl: string; } | { prevUrl: string; confirm: () => void; }'. How can this be resolved?

function NavigationButtons({
  variant,
  ...props
}:
  | {
      variant: 'first';
      nextUrl: string;
    }
  | { variant: 'middle'; prevUrl: string; nextUrl: string }
  | { variant: 'last'; prevUrl: string; confirm: () => void }) {
  return (
    <nav
      className={`fixed bottom-0 left-0 right-0 flex bg-neutral-white p-4 ${
        variant === 'first' && 'justify-end'
      } lg:static lg:mt-auto`}
    >
      {(variant === 'middle' || variant === 'last') && (
        <Link href={props.prevUrl}>
          <button>Go Back</button>
        </Link>
      )}
      {(variant === 'first' || variant === 'middle') && (
        <button className='rounded bg-primary-marine-blue px-4 py-2 text-neutral-alabaster'>
          Next Step
        </button>
      )}
      {variant === 'last' && <button>Confirm</button>}
    </nav>
  );
}

View image here

Answer №1

By destructing the props at the beginning, you are isolating variant from the rest of the props, creating independence that prevents the compiler from narrowing down the props based on the value of variant. One way to avoid this issue is by not using destructuring, as shown below:

function NavigationButtons(props:
  | {
      variant: 'first';
      nextUrl: string;
    }
  | { variant: 'middle'; prevUrl: string; nextUrl: string }
  | { variant: 'last'; prevUrl: string; confirm: () => void }) {
  return (
    <div>
       {(props.variant === 'middle' || props.variant === 'last') && <a href={props.prevUrl} /> }
    </div>
  )
}

playground

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Show detailed information in a table cell containing various arrays using AngularJS

After integrating d3.js into my code, I now have an array with key-value pairs. Each team is assigned a key and its corresponding cost is the value. When I check the console log, it looks like this: Console.log for key and value Rate for current month [{ ...

What is the method for displaying an object as JSON on the console in Angular2?

I've been utilizing a service to input my form data into an array within my angular2 application. The information is organized in the following manner: arr = [] arr.push({title:name}) After executing console.log(arr), it displays as Object. However, ...

Angular8 does not recognize custom paths that have been added to the tsConfig file

Hey there! I'm facing an issue with Angular not recognizing a custom path I added to my tsConfig file. It seems like Angular already has a baseUrl property set to ./, starting from the current folder where the tsConfig file is located. The code snippe ...

Issue: The mandatory parameter (slug) was not provided in string format in the getStaticPaths method for the endpoint /posts/[slug]

In my project, I have a [slug].js file with the following code: import Head from "next/head"; import PostContent from "../../components/posts/post-detail/post-content"; import { getPostByName, getAllPosts } from "../../helpers/api- ...

Create a new project using Firebase Functions along with a Node.js backend and a React.js frontend

In the process of developing my application, I have chosen to utilize node.js, express.js, and Firebase with firebase functions, all coded in TypeScript. For the client side framework, I am interested in incorporating react.js. Currently, I have set up nod ...

What could be the reason for Next.js throwing the error message "Unhandled Runtime Error: Rendered more hooks than during the previous render."?

I recently encountered an issue with the client component in my Next.js application that tracks the time spent within the app. Below is the code snippet I am using for this purpose: 'use client'; import React, { useEffect, useState } from ' ...

Using Angular 6 to Share Data Among Components through Services

I am facing an issue in my home component, which is a child of the Dashboard component. The object connectedUser injected in layoutService appears to be undefined in the home component (home userID & home connectedUser in home component logs); Is there ...

Tips on selecting specific text from a drop-down menu

After struggling to find a solution for retrieving the text of a selected item from a dropdown, I decided to create a common directive. This directive would allow me to easily access the text for all dropdown items when used in my code. Below is the snippe ...

The definition of "regeneratorRuntime" is missing in the rete.js library

After encountering a problem, I managed to find a potential solution. My current challenge involves trying to implement Rete.js in Next.js while using Typescript. The specific error message that's appearing is: regeneratorRuntime is not defined Be ...

Received an error message stating: "Error: Uncaught TypeError: path.split is not a function". My assumption is that this error may be related to a recent update in react-hook-form. Can anyone provide insight on

Seeking innovative solutions through coding techniques is my current quest. I've been informed that the error I'm encountering might be linked to the recent update of react-hook-form, but I am unsure how to resolve it. Any assistance on this matt ...

Customize language settings on a multilingual site using next.js

Just recently, I embarked on my Next.js journey and dove into creating my inaugural website, aiming for a multilingual touch. To guide me along the way, I diligently followed the official documentation at https://nextjs.org/docs/app/building-your-applicat ...

Error encountered in Snap SVG combined with Typescript and Webpack: "Encountered the error 'Cannot read property 'on' of undefined'"

I am currently working on an Angular 4 app that utilizes Snap SVG, but I keep encountering the frustrating webpack issue "Cannot read property 'on' of undefined". One solution I found is to use snapsvg-cjs, however, this means losing out on the ...

Using the reduce method in JavaScript or TypeScript to manipulate arrays

Just exploring my culture. I have grasped the concept of the reduce principle var sumAll = function(...nums: number[]):void{ var sum = nums.reduce((a, b) => a + b , 0); document.write("sum: " + sum + "<br/>"); } sumAll(1,2,3,4,5); The r ...

What could be causing the malfunction of my token rotation feature in nextAuth?

I am developing a web application that involves working with an external API alongside my team member. We are making API requests using Next.js. I have implemented nextAuth for authentication, but I am facing issues with token rotation. After successful lo ...

Reduce the size of the JSON file located in the Assets folder during an Angular build

What is the most effective method to compress JSON files in an Angular production build? I currently have a JSON file in the assets folder that remains unchanged when the production build is completed. During development, the file appears as the Developme ...

Setting up Webpack for react-pdf in a Next.js application

In my Next.js application, I am utilizing the react-pdf library to generate and handle PDF files on the client side without involving the server. However, I am facing challenges in setting up Webpack for Next.js as I lack sufficient expertise in this area. ...

Issue with my TypeScript modules TS2307: Module not found

For my latest project, I decided to use the aurelia-typescript-skeleton as the foundation. To enhance it, I created a new file called hello.ts in the src folder. export class Hello { sayHello(name:string) : string { return 'Hello ' + name; ...

The 'subscribe' property is not available on the type '() => Observable<any>'

File for providing service: import { Observable } from 'rxjs/Rx'; import { Http, Response} from '@angular/http'; import { Injectable } from '@angular/core'; import 'rxjs/add/operator/Map'; @Injectable() export clas ...

Which specific files do I have to edit in order for Laravel to acknowledge a new data type?

Currently, I am honing my skills in Laravel by working on a Laravel Breeze application. One task that I have set for myself is to incorporate a postal code field into the existing User model, including the registration form. To tackle this challenge, I dec ...

I am experiencing difficulty with the function from flip.to not functioning properly on my Next.js project

I was given this script by flip.to <script> !function(b,e){ (b[e] = b[e] || []).push({ flipto: { bookingEngine: "Demo", companyCode: "XX", code: "YYYY", ...