Next.js Discord

Discord Forum

Importing modules from "next/dist/compiled/..."

Answered
Welsh Springer Spaniel posted this in #help-forum
Open in Discord
Welsh Springer SpanielOP
Hey guys. I'm trying to type some helper functions that wrap the cookies function from "next/headers" and I'm trying to type my parameters. Copilot suggested I import these types...

import { RequestCookie } from "next/dist/compiled/@edge-runtime/cookies";

Is it ok to import from next/dist/compiled or should I be retrieving this type through a different path or through some other means?

Thanks!
Answered by Giant panda
Don't rely on these internals and leverage TypeScript instead using Parameters<typeof cookies> or ReturnType<typeof cookies> or similar approaches.
View full answer

4 Replies

Giant panda
Don't rely on these internals and leverage TypeScript instead using Parameters<typeof cookies> or ReturnType<typeof cookies> or similar approaches.
Answer
Welsh Springer SpanielOP
Ooo nice, that's exactly the sort of thing I was looking for! Thank you so much! @Giant panda
Welsh Springer SpanielOP
Ah so, not so easy, it seems. The ReturnType for cookies looks like this:

type ReadonlyRequestCookies = Omit<RequestCookies, 'set' | 'clear' | 'delete'> & Pick<ResponseCookies, 'set' | 'delete'>;

Being "readonly", it's specifically omitting the types I'm hoping to use...
Welsh Springer SpanielOP
Oh i'm an idiot, ignore me...