NextImage's ImageProps is overriding src and alt properties

I've created a wrapper called IllustrationWrapper that I'm utilizing in different components.

import Image, { ImageProps } from 'next/image';

const getImageUrl = (illustrationName: string) => {
  return `https://my-link.com/illustrations/${illustrationName}.svg`;
};

interface IllustrationProps extends ImageProps {
  illustrationName: string;
}

const IllustrationWrapper = ({ illustrationName, ...props }: IllustrationProps) => {
  const imageUrl = getImageUrl(illustrationName);
  const altText = imageUrl.split(/(?=[A-Z])/).join(' ');

  return <Image {...props} src={imageUrl} alt={altText} unoptimized />;
};

export default IllustrationWrapper;

This wrapper is being used in the following component as an example:


import Link from 'next/link';
import IllustrationWrapper from '~/common/components/IllustrationWrapper';


const DesktopNav = () => (
    <div className="flex-shrink basis-[14.125rem]">
      <Link href="/" className="inline-block" aria-label="To main site">
        <IllustrationWrapper illustrationName="LogoLight" width={80} height={28} />
      </Link>
    </div>
)

export default DesktopNav;

However, when using it in DesktopNav, I encounter an error saying

Type '{ illustrationName: string; width: number; height: number; }' is missing the following properties from type 'IllustrationProps': src, alt

It seems like ImageProps from next is overriding my src and alt attributes in IllustrationWrapper. How can I resolve this?

Answer №1

You have created the IllustrationProps interface, which inherits all properties from ImageProps (such as alt and src) and adds a new property illustrationName,

To define this interface, you can use the following code:

interface IllustrationProps extends Omit<ImageProps, 'src' | 'alt'> {
  illustrationName: string;
}

This will exclude the specified properties.

TS 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

Arranging the output of a Typescript project

I'm currently advocating for Typescript to be implemented in our web application. Can anyone provide insight on the best method for structuring Javascript output files? Should they be excluded from source control? And when it comes to testing, is it p ...

Creating a time-limited personal link with Django Rest Framework and React

I have a frontend application built with React Framework that I want to provide access to via a time-limited personal link, without the need for additional authentication functions. With approximately 1-2 new users per day, my proposed implementation is as ...

The typescript compiler encounters an error when processing code that includes a generator function

Recently, I started learning about TypeScript and came across the concept of generators. In order to experiment with them, I decided to test out the following code snippet: function* infiniteSequence() { var i = 0; while(true) { yield i++ ...

The function service.foo is not recognized in Angular

My service function is not being recognized by my component import { Injectable } from '@angular/core'; import { ToastController } from '@ionic/angular'; @Injectable({ providedIn: 'root' }) export class LocationService { ...

Convert a fresh Date() to the format: E MMM dd yyyy HH:mm:ss 'GMT'z

The date and time on my website is currently being shown using "new date()". Currently, it appears as: Thu May 17 2018 18:52:26 GMT+0530 (India Standard Time) I would like it to be displayed as: Thu May 17 2018 18:43:42 GMTIST ...

Error in ie 11 caused by socket.io-client library in Next.js app

After transitioning my project from CRA to Next.js, I encountered an issue in IE while using socket io for the chat feature. When clicking on the error message, it refers to a syntax error in "_app.js" at line 24050, column 23. /***/ "./node_modules/d ...

When attempting to create, an error occurs: Uncaught TypeError: Unable to access properties of undefined (retrieving 'id')

In the process of creating a public prayer journal that allows users to include their favorite Bible verses, I encountered an issue while trying to add a prayer or verse. The error message "caught (in promise) TypeError: Cannot read properties of undefined ...

Is it possible to store the code reflection from _app.js in Next.js within a designated directory?

How can I maintain the code reflection from _app.js in Next.js within a particular directory? I am particularly interested in reflecting the code specified in _app.js only for files located under the "Afolda" folder within the "pages/Afolda" directory. ...

Determine through programming whether an ng-content slot has been filled in Angular

I have developed a dynamic Angular component that utilizes content projection and slots in the following way: <div class="content-wrapper"> <div class="short-wrapper" [style.opacity]="expanded ? 0 : 1"> ...

Having trouble retrieving the JSON data from the getNutrition() service method using a post request to the Nutritionix API. Just started exploring APIs and using Angular

When attempting to contact the service, this.food is recognized as a string import { Component, OnInit } from '@angular/core'; import { ClientService } from '../../services/client.service'; import { Client } from '../../models/Cli ...

You are unable to utilize the header as a JSX component

'I attempted to utilize an async function for the Header component in JSX, but encountered an error stating that 'Header cannot be used as a JSX component. Its return type 'Promise' is invalid because it is missing required properties s ...

How to access an element through the router-outlet in Angular 6?

<side-navigation [navigationTitle]="navTitle"></side-navigation> <router-outlet> </router-outlet> Within my project, I have a navigation bar located in the root component. I have set up [navigationTitle] as an @Input Decorator wit ...

Even though there is data stored in the array, the React Native array.length appears to be returning a value

I am struggling with what appears to be a simple issue, and it's frustrating that I've had to seek help for this. The problem lies in iterating through an array messages: Message[] = [...]. No matter what method of iteration I try, it doesn&apos ...

Steps to make pop-up iframe function on the same page within a react nextjs application

My vanilla Html app has a pop-up feature that functions perfectly. When the button is clicked, the pop-up opens and everything works as expected. However, I am encountering an issue when trying to implement this same functionality in my React, NextJS app. ...

Using Angular BehaviorSubject in different routed components always results in null values when accessing with .getValue or .subscribe

I am facing an issue in my Angular application where the JSON object saved in the service is not being retrieved properly. When I navigate to another page, the BehaviorSubject .getValue() always returns empty. I have tried using .subscribe but without succ ...

The seamless pairing of Cucumber and Playwright: Cucumber's inability to retain cookies results in a login attempt with every scenario

I am currently facing an issue with integrating cucumber and playwright into my framework. When attempting to execute various features or multiple scenarios within one feature, I encounter a problem where if one scenario logs into a site, the other scenari ...

The React error message "Unrecognized property key detected: react/no-unknown-property"

I encountered an error while uploading my Next.js app to Vercel. Can someone please assist me in resolving this issue? https://i.sstatic.net/6qxOF.png ...

List out the decorators

One query is bothering me - I am attempting to create my own version of Injectable and I need to determine if a specific decorator exists in my Class. Is there a way to list all decorators of a class? Let's take the example below. All I want to know i ...

The category has been defined but remains inaccessible. What could be the reason for this limitation?

I've been utilizing this bson library along with the corresponding declaration found here. Within this library, there is a method called serialize():Buffer which returns a Buffer. When I execute the following code: let data:Buffer = this.serializer.s ...

Encountering incorrect month while utilizing the new Date() object

My Objective: I am looking to instantiate a new Date object. Snippet of My Code: checkDates (currentRecSec: RecommendedSection){ var currActiveFrom = new Date(currentRecSec.activeFrom.year,currentRecSec.activeFrom.month,currentRecSec.activeFrom.day ...