#5686·zod

在与 ESM 结合使用时,Zod 会错误地指向 CommonJS 声明

作者: patrickshipe创建于 2026年2月4日更新于 2026年9月16日

In Zod's package.json exports setting, the main entry point for both CJS and ESM resolves to the same index.d.cts declarations file. In most cases, this does not cause issues. However, there is an issue that arises when declaration files are compiled for an ESM project with module nodenext. If I use this output in the frontend with module ESNext, the declaration file will not be legible and all inferred types will be any. This is because based on the cts extension, TypeScript assumes Zod to be a CommonJS module and treats it differently based on Node module interop rules. The package.json exports field should be modified to point to the correct declarations file (index.d.ts) for the import condition.