Skip to content

Always using cts declarations breaks declaration output in packages that use ESM #65

@patrickshipe

Description

@patrickshipe

Related Zod issue: colinhacks/zod#5686

The README states regarding always using cts:

This warning does not cause any resolution issues (unlike "Masquerading as ESM"). Technically, we're tricking TypeScript into thinking our code is CommonJS; when in fact it may be ESM. The ATTW tool is very rigorous and flags this; in practice, this has no real consequences and maximizes compatibility (Zod has relied on the CJS masquerading trick since it's earliest days.)

I believe this may no longer be true. Now that TypeScript has pushed for module writers to use nodenext module type, there is a real consequence for "masquerading as CJS" when operating in an ESM environment. TypeScript assumes the default export of a CJS always contains the entire module.exports, which means it will reference things like z.z.core.$strip in the declaration file that are incorrect. This causes breaking behavior when packages compiled with one module type are used in a TS environment with another (e.g. nodenext vs esnext).

The workaround is to never use the default export from a zshy package in ESM code, but that requires special enforcement/lint rules.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions