Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App/Route issue "Promise<unknown>" #241

Open
albertpeiro opened this issue Jan 7, 2024 · 4 comments · May be fixed by #243
Open

App/Route issue "Promise<unknown>" #241

albertpeiro opened this issue Jan 7, 2024 · 4 comments · May be fixed by #243

Comments

@albertpeiro
Copy link

albertpeiro commented Jan 7, 2024

This does not work as documented (in Typescript)

const handler = (): Response => {
  console.log("Hello world!");
  const data = { hello: "world" };
  return Response.json({ data });
};

const api = Api();
api.post(handler);

export async function POST(request: NextRequest, ctx: ApiRequestContext) {
  return api.run(request, ctx);
}

When I run production npm run build I get:

Type error: Route "src/app/api/waitlist/route.ts" has an invalid export:
  "Promise<unknown>" is not a valid POST return type:
    Expected "void | Response | Promise<void | Response>", got "Promise<unknown>".
      Expected "Promise<void | Response>", got "Promise<unknown>".
        Expected "void | Response", got "unknown".

to fix you'll have to as Promise<any>

export async function POST(request: NextRequest, ctx: ApiRequestContext) {
   return api.run(request, ctx) as Promise<any>;
}

▲ Next.js 14.0.4

@Siddharth2212
Copy link

I am facing same issue after update to next 14

@OgDev-01
Copy link

export async function POST(request: NextRequest, ctx: ApiRequestContext) { return api.run(request, ctx) as Promise; }

Have you considered Promise<NextResponse>. Using any ulters the main purpose of typescript 🤌

@OgDev-01
Copy link

I am facing same issue after update to next 14

This fixed the error for me... Thanks @albertpeiro for this opening this isuse 👍 .

const router = createEdgeRouter<NextRequest, NextResponse>();

router
  .use((req, res, next) => AuthGuard(req, next))
  .post((req, res, next) => {
    return createProject(req, res);
  });

export async function POST(req: NextRequest, res: NextResponse) {
  return router.run(req, res) as Promise<NextResponse>;
}

I'll open a PR that updates the Readme for new users to save them from days of debugging 😃

@ravidyoeun
Copy link

I am also facing the same issue after upgrading to next 14. Thank you @albertpeiro for showing how to resolve this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants