Skip to content

Improve portability of resolution-mode assertions automatically emitted in nodenext #55579

Closed
@andrewbranch

Description

@andrewbranch

In the last week, I’ve gotten two separate requests for help with solving an issue where a resolution-mode assertion in a triple-slash reference:

/// <reference types="chai" resolution-mode="require" />

was automatically inserted into a declaration file. The implementation file was an ES module that referenced a global defined in chai, which only ships as a CommonJS module. Presumably, the resolution-mode assertion gets added in declaration emit to increase the likelihood that, if the user installs a version of chai that contains separate ESM and CJS types, the ESM-format .d.ts file here will still resolve to the CJS types, as it did when it was created by the library’s compilation. (Note that if the user does have the same version of chai that the library had during compilation, the resolution-mode assertion will not actually be needed.)

The reason this was an issue for the two folks who reached out to me was that some of their users who consumed these declaration files were using --moduleResolution bundler or maybe even --moduleResolution node10, and consequently got the error:

error TS1452: 'resolution-mode' assertions are only supported when `moduleResolution` is `node16` or `nodenext`.

I feel pretty confident that non-node16/nodenext modes could simply ignore these assertions when they occur in declaration files. While we can’t guarantee that users in other modes will resolve these references the same way the library did, this is no different from any import statement the user wrote, or indeed any import() type automatically inserted by the declaration emitter.

Maybe better, we could also consider supporting resolution-mode assertions and the way they affect conditional package.json "exports" lookups in every moduleResolution mode in type reference directive lookups. They don’t reflect a runtime module resolution feature, so it feels like all we should care about is doing our best to resolve to the same global types in every moduleResolution mode.

It should be noted that in the meantime, a workaround for library authors is to manually write a /// <reference types="chai" /> with no resolution-mode assertion in the implementation file that generated the reference that caused the problem.

Metadata

Metadata

Assignees

Labels

In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions