What steps are involved in implementing ts-transformer-keys in a Next.js project?

I am trying to extract keys from an interface as a string array so that I can iterate over them. After doing some research on stackoverflow, I found out that I need to use a library called 'ts-transformer-keys'. In my Next.js project, which involves both webpack and typescript, I have configured the webpack and tsconfig.json files according to the instructions provided in the link below:

https://github.com/iqraabdulrauf/ts-transformer-keys/blob/master/README.md

However, I am encountering an issue where the project fails to compile when using both 'ts-loader' and 'awesome-typescript-loader'. It seems to be unable to recognize the _document.tsx files located under the pages folder within the Next.js project.

Answer №1

Explore ts-patch and ts-transformer-keys

// tsconfig.json
{
  "compilerOptions": {
    // ...
    "plugins": [
      { "transform": "ts-transformer-keys/transformer" }
    ]
  },
  // ...
}

UPDATE If the above does not work, modify the next.config.js:

const keysTransformer = require('ts-transformer-keys/transformer').default;
const enumerateTransformer = require('ts-transformer-enumerate/transformer').default;
const isTransformer = require('typescript-is/lib/transform-inline/transformer').default;

const { PHASE_DEVELOPMENT_SERVER } = require('next/constants');
module.exports = (phase, { defaultConfig }) => {

    const config = {
        webpack: (config, options) => {
            config.module.rules.push({
                test: /\.ts$/,
                loader: 'ts-loader', // or 'next-babel-loader'
                options: {
                    // ensure `transpileOnly: true` is not set here, as it will not work
                    getCustomTransformers: program => ({
                        before: [
                            keysTransformer(program),
                            enumerateTransformer(program),
                            isTransformer(program)
                        ]
                    })
                }
            });

            return config
        },
    };
    if (phase === PHASE_DEVELOPMENT_SERVER) {
         // additional configurations for development server
    }

    return Object.assign (config, {
        // constants mapped to process.env.
    });
};

Make sure to install ts-loader if it is not already installed. Alternatively, you can utilize next-babel-loader.

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

Having trouble with text decoration not working for a link in Material-UI. Any ideas for a solution?

Currently, I am working on a React/Next.js project and incorporating MUI for the design elements. <Link href="/products"> <ListItem disablePadding> <ListItemButton> <ListItemIcon> ...

Best practices for presenting various header details based on the current page in Next.js

I am currently facing a challenge in displaying a Language switch button on the top navigation bar of my website, which is rendered within a Layout component. The issue at hand is determining the switching URL based on the current page. Given the constrai ...

Error: The element "FaceMesh" in _mediapipe_face_mesh__WEBPACK_IMPORTED_MODULE_3__ is not recognized as a valid

I am currently working on integrating mediapipe into a hybrid NextJS + Electron application. However, when I try to import and use the FaceMesh module from @mediapipe/face_mesh, I encounter an error that is halting my progress. To provide some context, I ...

Using an Angular interface for an HTTP request: The statusText message reads as "Error: Unable to Determine."

I've been working on calling an API as an example in Angular using an interface. The API I'm trying to access is located at https://jsonplaceholder.typicode.com/posts. Unfortunately, I encountered the following error message: ERROR HttpErrorResp ...

Unable to set value using React Hook Form for Material-UI Autocomplete is not functioning

Currently, I am in the process of constructing a form using React-Hook-Form paired with Material-UI. To ensure seamless integration, each Material-UI form component is encapsulated within a react-hook-form Controller component. One interesting feature I a ...

Creating dynamic JSX content in NextJS/JSX without relying on the use of dangerouslySetInnerHTML

I have a string that goes like "Foo #bar baz #fuzz". I'm looking to create a "Caption" component in NextJS where the hashtags become clickable links. Here's my current approach: import Link from "next/link"; const handleHashTag = str => st ...

Refreshing Components upon updates to session storage - Angular

Currently, I am in the process of developing a build-a-burger website using Angular. The ingredients and inventory need to be updated dynamically based on the selected location. Users can choose the location from a dropdown menu in the navigation bar. The ...

Exploring the world of Next.js API routing

In my work with Next.js 13.4, I am attempting to pass the id of a product using query parameters in this format - http://localhost:3000/api/abc/abc/update/89?id=2 Here is the code snippet I have written: export async function PUT(req,res){ console.log(r ...

The login process in Next-auth is currently halted on the /api/auth/providers endpoint when attempting to log in with the

My Next-auth logIn() function appears to be stuck endlessly on /api/auth/providers, as shown in this image. It seems that the async authorize(credentials) part is not being executed at all, as none of the console.log statements are working. /pages/api/au ...

Encountering errors in Typescript build due to issues in the node_modules directory

While running a typescript build, I encountered errors in the node_modules folder. Despite having it listed in the exclude section of my tsconfig.json file, the errors persist. What's puzzling is that another project with identical gulpfile.js, tsconf ...

Utilize an alias to define the SCSS path in an Angular-CLI library project

I am in the process of developing a library project using angular-cli. I have been following the guidelines outlined in the angular documentation. This has resulted in the creation of two main folders: one is located at ./root/src/app, where I can showcase ...

Oops! An issue occurred at ./node_modules/web3-eth-contract/node_modules/web3-providers-http/lib/index.js:26:0 where a module cannot be located. The module in question is 'http'

I have been troubleshooting an issue with Next.js The error I am encountering => error - ./node_modules/web3-eth-contract/node_modules/web3-providers-http/lib/index.js:26:0 Module not found: Can't resolve 'http' Import trace for req ...

Issue with Next.js 14 favicon.ico not displaying on Chrome browser

When I tried to replace the favicon.ico, Chrome was unable to display it. It appears to be a bug related to using app router. /-app /-favicon.ico I noticed that many others have encountered this issue as well, but I found a solution: 1. Rename favic ...

Experience feelings of bewilderment when encountering TypeScript and Angular as you navigate through the

Exploring Angular and TypeScript for an Ionic project, I am working on a simple functionality. A button click changes the text displayed, followed by another change after a short delay. I'm facing confusion around why "this.text" does not work as exp ...

The appearance and functionality of the app undergo a noticeable transformation after being bundled with Webpack

After successfully migrating my Angular 2 project from SystemJS to Webpack using the latest version of Angular2-CLI, I noticed some unexpected changes in the design of the page. Despite minimal adjustments to the project files and Angular2 code during the ...

Triggering a class in Angular when another class is activated through JavaScript

My goal is to apply the class "xyz" when the class "xy" is activated using ngClass. I am looking to achieve the following scenario: If the class "xyz" is present in the tag, then activate the class "xy" Using ngClass="'xyz', 'xy'" ...

Connecting conversations in react

When working with jQuery, I often utilize the modal dialog chaining technique. For example: $.Deferred().resolve().promise() .then(function () { return runDialog1(someProps); // return promise }) .then(function (runDialog1Result) ...

Delivery Guy: Error: JSON parsing issue on line 1 – Unexpected number detected

I am currently learning web development and experimenting with Postman to send a POST request to one of my application's APIs. The website I am building is based on the Next.JS framework. Below is the code for my API: import type { NextApiRequest, Ne ...

Encountering a Lint No Nested Ternary Error while utilizing the ternary operator

Is there a way to prevent the occurrence of the "no nested ternary" error in TypeScript? disablePortal options={ // eslint-disable-next-line no-nested-ternary units=== "mm&quo ...

What is the best way to bring in styles for a custom UI library in Angular 2?

In the realm of UI libraries, I find myself facing a dilemma. Upon importing SCSS styles into my components (such as a button), I noticed that if I instantiate the component button twice in the client app (which has my UI library as a dependency), the SCSS ...