The NextJs error states that a string cannot be assigned to type Never

'use client'

import { FieldErrors, FieldValues, UseFormRegister } from "react-hook-form";
import { BiDollar } from "react-icons/bi";

interface InputProps {
    id: string;
    label: string;
    type?: string;
    disabled?: boolean;
    formatPrice?: boolean;
    required: boolean;
    register: UseFormRegister<FieldValues>;
    errors: FieldErrors;
}

const Input: React.FC<InputProps> = ({
    id,
    label,
    type = 'text',
    disabled,
    formatPrice,
    required,
    register,
    errors
}) => {
    return (
        <div className="w-full relative">
            {formatPrice && (
                <BiDollar
                    size={24}
                    className="text-neutral-700
                    absolute
                    top-5
                    left-2
                    "
                />
            )}
            <input
                id={id}
                disabled={disabled}
                {...register(id, { required })}
                placeholder=""
                type={type}
                className={`
                peer 
                w-full 
                p-4 
                pt-6 
                font-light
                 bg-white border-2 
                 rounded-md 
                 outline-none 
                 transition 
                 disabled:opacity-70
                 disabled:cursor-not-allowed
                 ${formatPrice ? 'pl-9' : 'pl-4'}
                 ${errors[id] ? 'border-rose-500' : 'border-neutral-300'}
                 ${errors[id] ? 'focus:border-rose-500' : 'focus:border-neutral-300'}`
                }
            />
            <label
                className={
                    `
                absolute
                text-base
                duration-150
                transform
                -translate-y-3
                top-5
                origin-[0]
                ${formatPrice ? 'left-9' : 'left-4'}
                peer-placeholder-shown:scale-100'
                peer-placeholder-shown:translate-y-0
                peer-focus:scale-75
                peer-focus:-translate-y-4
                ${errors[id] ? 'text-rose-500' : 'text-zinc-400'}
                `
                }

            >{label}</label>
        </div>
    )
}

export default Input;

An error is occurring during build-time for the component "Input" located in "app/components/Input/page.tsx". The error message states that the exported type "InputProps" is not valid.

The specific issue seems to be with the property 'id', which is causing an incompatibility with the index signature. The error message indicates that the type 'string' is not assignable to type 'never' within this context.

It's worth noting that the component works fine on localhost but encounters this error during the build process.

Answer №1

Using the file name app/components/Input/page.tsx in Next.js is reserved for routing purposes. When you use page.{js,jsx,ts,tsx} within the app directory, it automatically generates a route. This can lead to unintentionally creating a /components/Input route.

Since this is a Next.js reserved file, specific exports like default component, runtime, revalidate, dynamic, etc., are expected. The default export must be a react component that accepts { params, searchParams } props, but in your case, it's accepting InputProps which is invalid.

If your intention is to create a reusable component and not a route, it's important to rename the file to a non-reserved name such as Input.tsx or Input/index.tsx, avoiding names like page, layout, route, default, template, not-found.

To prevent conflicts in the future, it's recommended to move extra files (e.g., components) outside of the app directory, as it should only be responsible for routing.

Rather than structuring it as shown below:

.
└── app
    ├── components
    │   └── Input
    │       └── page.tsx
    ├── login
    │   └── page.tsx
    └── register
        └── page.tsx

It's advisable to organize it like this:

.
├── app
│   ├── login
│   │   └── page.tsx
│   └── register
│       └── page.tsx
└── components
    └── Input
        └── page.tsx

To prevent future conflicts similar to this one,

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

The loan component does not have a property called 'darkModeService' associated with it

I've been attempting to implement a dark theme for my Angular application, and although I've configured everything correctly, it doesn't seem to be working as expected. Here is the code snippet: constructor(private bookService: BookService ...

Issue encountered following deployment of Strapi 4.23.0 to Google App Engine using a PostgreSQL database

I've encountered an issue while trying to deploy my Strapi application on Google App Engine. After deployment, I see the following error in the logs: Error: ENOENT: no such file or directory, mkdir '/workspace/.tmp' at Object.mkdirSync (nod ...

I'm experiencing difficulties with the Open Graph video not displaying properly on my backend platform

I'm encountering an issue where videos from my own backend do not play in the player that has OpenGraph support, while videos from other sites work just fine. The frontend is built with NextJS and the backend is using .NET. ...

Interpret information in Angular 2 using Typescript

Just starting with Angular (IONIC) and need help. How can I extract the userId or id from this code? his.response = data. //Looking for guidance on accessing Json keys Response : { "userId": 1, "id": 1, "title": "sunt aut facere repellat providen ...

Issue with updating Angular list reference when deleting an item

My current task involves implementing a feature that displays selected items from a hierarchical structure on the right side. slice.component.ts : import { Component, Input, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core&a ...

Using the Ajax method from a separate class in TypeScript: A step-by-step guide

Recently, I started learning about typescript and ajax. One of the challenges I encountered was while creating a method in typescript for making ajax calls that can be used across classes: myFunc(value: string): JQueryPromise<any> { var dfd = $. ...

Obtain the outcome of HTML5 FileReader by utilizing promises within an asynchronous function

I am encountering a challenge in my Angular 4 application where I am working with an image. I am trying to pass the base64 string to another variable, but due to the asynchronous nature of this process, the image.src ends up being empty. As a result, the ...

Learning how to utilize environment variables in API within AWS Amplify and Next.js

I am using an Amplify app (frontend only) to serve my Next.js application. Within the /api/ folder, there is a file that generates a Stripe session to redirect users to Stripe for payment processing. When creating the stripe session, I must provide my STR ...

Accessing data from Firebase Database Object

I am currently facing a challenge in extracting a username value from my firebase database and then displaying it in a console log statement. The issue lies in fetching the child value instead of just the object. How can I retrieve the child value and prin ...

What is the best way to determine the type of `rootReducer`?

My project is set up with a combination of React, Redux, Immutable.js, and TypeScript. As I worked on implementing it, I made an effort to declare types wherever possible which led me to discover an interesting issue. A code example illustrating the proble ...

Extracting and retrieving information from a complex data structure obtained through an API call

Struggling with this one. Can't seem to locate a similar situation after searching extensively... My goal is to determine the author of each collection associated with a user. I wrote a function to fetch data from an API for a specific user, in this ...

Presenting information on the user interface

Recently, I have been working on an API endpoint that retrieves comments by ID, using the endpoint get/comments/:id. When I tested this endpoint using Postman, the response I received was as follows: { "id": 401478, "page": 1, "results": [ ...

Is it possible to override Material UI styles with css/scss?

Currently, I am using SCSS because it integrates well with NextJS. I find the SCSS module system easy to work with and would like to continue using it with Material-UI. However, Material-UI uses JSS which involves a lot of boilerplate code. I prefer not to ...

Convert markdown images to Next.js images in real time

I am currently working on a markdown editor with a viewer (side by side). However, I have encountered an issue while trying to type the syntax for inserting an image in markdown. Unhandled Runtime Error Error: Image is missing required "src" pro ...

The documentation for integrating tag manager with Next.js can be quite perplexing

I'm currently integrating tag manager into my next.js site (version 12.0.4) and I came across the documentation available here https://nextjs.org/docs/basic-features/script. It mentions that I can utilize next/script in the _document.js file. However ...

Ensure that the output of a function in Typescript matches the specified data type

Looking for a way to inform TypeScript that the output of a function will always meet the type requirements of a variable, therefore avoiding any error messages? Type 'string | Date' is not assignable to type? For example: const getStringOrDat ...

After adding a new class, NextJS fails to style the div appropriately

Currently, I am working on the front-end using the NextJS framework. When a user clicks a specific button, a class is supposed to be added to a particular div which will then change the style of the div based on unique styles defined in my CSS file. The o ...

Exploring the Possibilities of OpenLayers with Scalable Vector

Trying to create a webpage with an image that can be navigated using drag and scroll events, similar to Google Maps. Instead of building it from scratch, I attempted to achieve this using OpenLayers, with the intention of using the image in place of a map. ...

What is the best way to add an item to an array with distinct properties?

I am currently working on creating an array with different properties for each day of the week. Here is what I have so far: const [fullData, setFullData] = useState([{index:-1,exercise:''}]) My goal is to allow users to choose exercises for a sp ...

Is there a TypeScript type that represents a subset of the keys of another type?

When given an object, is it possible to create a second typed object that contains only a subset of the original keys with different value types? I attempted to use Partial<keyof ...>, but it did not have the desired effect. Is there another approach ...