martpie / next-transpile-modules

Next.js plugin to transpile code from node_modules. Please see: https://github.com/martpie/next-transpile-modules/issues/291
MIT License
1.13k stars 85 forks source link

feat(typings): allow to preserve config type #274

Closed belgattitude closed 2 years ago

belgattitude commented 2 years ago

When composing multiple plugins, the type of nextConfig is lost.

A typecheck error will be throw for user setting // @ts-check in strict mode in the next.config.[js|mjs].

Found the cause in the withTMInitializer typings

Changed

After a npm run typings, the src/next-transpile-modules.d.ts will be

Before

declare function withTmInitializer(modules?: string[], options?: {
    resolveSymlinks?: boolean;
    debug?: boolean;
    __unstable_matcher?: (path: string) => boolean;
   // THE OLD ONE
}): (nextConfig?: {}) => {};

After

declare function withTmInitializer(modules?: string[], options?: {
    resolveSymlinks?: boolean;
    debug?: boolean;
    __unstable_matcher?: (path: string) => boolean;
   // THE NEW ONE, T IS INFERRED FROM ARGUMENT (nextConfig). Return is the same
}): <T>(nextConfig?: T) => T;

A patch

Used a patch to see if all works and it seems to be okay

diff --git a/src/next-transpile-modules.d.ts b/src/next-transpile-modules.d.ts
index 9fc7d0a28176b649fcadb6d1bd6859c72bca9595..c0ba96230af8dcc669f06af99da6d5226800c7b8 100644
--- a/src/next-transpile-modules.d.ts
+++ b/src/next-transpile-modules.d.ts
@@ -8,4 +8,4 @@ declare function withTmInitializer(modules?: string[], options?: {
     resolveSymlinks?: boolean;
     debug?: boolean;
     __unstable_matcher?: (path: string) => boolean;
-}): (nextConfig?: {}) => {};
+}): <T>(nextConfig?: T) => T;

Discovered in that P/R https://github.com/belgattitude/perso/pull/1015

martpie commented 2 years ago

Good reminder I should re-write this plugin in TypeScript one day, thanks!

belgattitude commented 1 year ago

Is it possible to make a small release ? Thx

martpie commented 1 year ago

Completely went over my mind, sorry for that! Released 9.1.0