Next.js Discord

Discord Forum

Is `HeadersInit` expected when using `fetch`? (v13.4.13)

Unanswered
Milkfish posted this in #help-forum
Open in Discord
MilkfishOP
Looking at a really basic example for a Route Handler with the App Router, I get the following error (screenshot attached).

This example is took directly from the Next.js docs.

I understand that I can create my own headers (via new Headers()).
I'm just wondering why there is a mismatch between what is shown in the docs and what the types expect? Which is correct?

2 Replies

you can't assign an undefined value to the header values. check the type of the variable being assigned to the API-Key header, it is string | undefined which is causing the error
MilkfishOP
I understand, just wondering because this is just copy-pasted from the docs - nearly every example in the docs that is using headers has this problem of using some header field with undefined/null values. So when you copy-paste them, you will get an error.