openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Type error: Private identifiers are only available when targeting ECMAScript 2015 and higher.

Open sagardwivedi opened this issue 1 year ago • 22 comments

Describe the bug I have "target": "ES2022" then also i am getting Type error: Private identifiers are only available when targeting ECMAScript 2015 and higher..

I am using NextJS

image

sagardwivedi avatar Jan 21 '24 18:01 sagardwivedi

This seems like a Typescript configuration issue on your side. Please check if your tsconfig with target ES2022 includes the generated folder

danilofuchs avatar Jan 22 '24 13:01 danilofuchs

I had set "target": "ES2022", but still encountered the same error. I removed the client generation and am now using it in the normal way.

sagardwivedi avatar Jan 24 '24 15:01 sagardwivedi

I'm also experiencing this issue when using npm run build in a Next.JS project.

@sagardwivedi can you share more about what you changed to make it work?

astromme avatar Feb 13 '24 16:02 astromme

Same here

Using the default next.js configuration from a brand new next.js app. I've also tried ES2022 and the same issue appears


{
  "compilerOptions": {
    "target": "es2017",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./src/*"]
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

Type error: Private identifiers are only available when targeting ECMAScript 2015 and higher.

  24 |
  25 | export class CancelablePromise<T> implements Promise<T> {
> 26 |     #isResolved: boolean;
"next": "14.1.0",
"openapi-typescript-codegen": "^0.27.0",
"typescript": "^5"

willtpwise avatar Feb 16 '24 01:02 willtpwise

I'm also experiencing this issue when using npm run build in a Next.JS project.

@sagardwivedi can you share more about what you changed to make it work?

I attempted to integrate FastAPI and Next.js for practice, but faced challenges and couldn't find satisfactory solutions. Consequently, I decided to switch to Supabase. That's why I closed the issue, not because I found a solution.

sagardwivedi avatar Feb 16 '24 09:02 sagardwivedi

Good to know. I also decided to switch to an alternative: https://openapi-generator.tech/ which worked better with FastAPI + NextJS

astromme avatar Feb 16 '24 16:02 astromme

Would love for this to be reopened as it's still an issue if you don't want to change to another library.

staklau avatar Mar 02 '24 14:03 staklau

Would love for this to be reopened as it's still an issue if you don't want to change to another library.

No Problem. I will reopen.

sagardwivedi avatar Mar 07 '24 05:03 sagardwivedi

Anyone able to post a CodeSandbox link? If I can reproduce it, I'll fix it

mrlubos avatar Mar 07 '24 06:03 mrlubos

@sagardwivedi I have the same problem. Have you fixed your problem?

I use Next.js 14 with TypeScript and I tried to exclude src/types/generated directory by adding this line is tsconfig.json file:

"exclude":["node_modules","src/types/generated/**/*.ts"]

but I still get the error when I run npm run build or npx tsc.

image

To temporarily fix the problem I added @ts-ignore but it's not ideal, I shouldn't have to do that.

    // @ts-ignore
    #isResolved: boolean;
    // @ts-ignore
    #isRejected: boolean;
    // @ts-ignore
    #isCancelled: boolean;
    // @ts-ignore
    readonly #cancelHandlers: (() => void)[];
    // @ts-ignore
    readonly #promise: Promise<T>;
    // @ts-ignore
    #resolve?: (value: T | PromiseLike<T>) => void;
    // @ts-ignore
    #reject?: (reason?: any) => void;

valentin-harrang avatar Mar 28 '24 16:03 valentin-harrang

cc @jordanshatford

mrlubos avatar Mar 28 '24 16:03 mrlubos

@valentin-harrang are you able to share a link to the repository? I can look into this. Check out our form that is more actively maintained if you’d like https://github.com/hey-api/openapi-ts

jordanshatford avatar Mar 28 '24 20:03 jordanshatford

@jordanshatford thank you for the answer. Unfortunately I can't share the repository, which is a private repository on the Gitlab of the company that employs me. Unfortunately I'm not authorised, but I can share some code files with you if you want. 😊

valentin-harrang avatar Mar 28 '24 20:03 valentin-harrang

@valentin-harrang if possible the next config file and your tsconfig. The issue likely tracks back to the tsconfig values so I just want to replicate it

jordanshatford avatar Mar 28 '24 20:03 jordanshatford

@jordanshatford Here are some additional elements:

tsconfig.json:

{
  "compilerOptions": {
    "baseUrl": "./src",
    "lib": ["dom", "dom.iterable", "esnext"],
    "allowJs": true,
    "skipLibCheck": true,
    "strict": true,
    "noEmit": true,
    "esModuleInterop": true,
    "module": "esnext",
    "moduleResolution": "bundler",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "jsx": "preserve",
    "incremental": true,
    "plugins": [
      {
        "name": "next"
      }
    ],
    "paths": {
      "@/*": ["./*"],
    }
  },
  "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"],
  "exclude": ["node_modules"]
}

next.config.mjs:

import createNextIntlPlugin from "next-intl/plugin";
import ms from "ms";

const withNextIntl = createNextIntlPlugin("./src/config/i18n.ts");

/** @type {import('next').NextConfig} */
const nextConfig = {
  images: {
    remotePatterns: [
      {
        protocol: 'https',
        hostname: 'loremflickr.com',
      },
    ],
  },
  headers() {
    return [
      {
        source: "/((?!_next|favicon.ico).*)",
        missing: [
          {
            type: "header",
            key: "Next-Router-Prefetch",
          },
        ],
        headers: [
          {
            key: "Cache-Control",
            value: [
              `s-maxage=` + ms("1d") / 1000,
              `stale-while-revalidate=` + ms("1y") / 1000,
            ].join(", "),
          },
        ],
      },
    ];
  },
  output: "standalone", // For self-hosting in a Docker container.
  transpilePackages: ["lucide-react"],
};

export default withNextIntl(nextConfig);

I added this script int package.json:

"codegen": "openapi --input ./openapi.json --output ./src/types/generated",

And I run the script with : npm run codegen

Versions:

Next.js: 14.1.3 openapi-typescript-codegen: 0.28.0 typescript: 5.4.2

valentin-harrang avatar Mar 28 '24 21:03 valentin-harrang

@jordanshatford I tried to generate my types thanks to https://github.com/hey-api/openapi-ts but I have the same problem in same file (CancelablePromise.ts)

valentin-harrang avatar Mar 28 '24 21:03 valentin-harrang

@valentin-harrang thanks for testing that. I should have some time later today to check it out and figure out what’s causing it.

jordanshatford avatar Mar 28 '24 21:03 jordanshatford

@jordanshatford thank you so much :)

valentin-harrang avatar Mar 28 '24 21:03 valentin-harrang

@valentin-harrang Try settings "target": "ES6", (or "target": "ES2015") in your tsconfig. I also found that replacing #isResolved with private isResolved (same for all other fields) fixes the problem, however I do not know if we want to make that change in the package. see for info.

I am discussing with the other maintainer to get his input.

NOTE: you can specific a more latest target, but those are the minimum

jordanshatford avatar Mar 28 '24 22:03 jordanshatford

@valentin-harrang Update: we are going to fix the issue in our repository. we dont see any issues converting those values to private instead of #.

jordanshatford avatar Mar 28 '24 23:03 jordanshatford

@valentin-harrang this has been fixed in v0.31.0 of @hey-api/openapi-ts

jordanshatford avatar Mar 29 '24 00:03 jordanshatford

@jordanshatford Thank you very much for your work and your responsiveness. I've just updated the package and it works perfectly ❤️

valentin-harrang avatar Mar 29 '24 08:03 valentin-harrang