Skip to content

Commit 79f4cc7

Browse files
fix: fixes importMap type in monorepo usage (#15203)
### What? Adds a `/** @type import("payload").ImportMap */` annotation to the generated importMap. ### Why? In a monorepo, it's common to use [Project References](https://www.typescriptlang.org/docs/handbook/project-references.html) so that Typescript can do dependency analysis on the packages in your project. Often, projects will have a single root `tsconfig.json` that `references` all of the packages, including any apps. To do so, all dependent `tsconfig.json`s must set `"composite": true`. When `"composite": true` is set in a package, Typescript does portability analysis on all exports to ensure that Typescript will be able to properly determine the type of those exports if they are imported into a consuming package as a dependency. This is true even if that package never ultimately exports that type through its `main` or `exports` fields, or is private, or is not depended on by any other projects. This portability check happens _even if checkJs is disabled_. Because `importMap` relies on type inference on transient dependencies, Typescript requires a type annotation to conclude that its type is portable. Ultimately, there's no reason for Payload consumers to care about or enforce the type of `importMap`. It's automatically generated by Payload, never hand edited, and only consumed by other auto-generated Payload code. Since Payload already has a type for `ImportMap`, we can type `importMap` with that type. It will satisfy the type, and is also considered portable. ### How? Updated the generateImportMap template. Fixes #9914 Co-authored-by: Paul Popus <paul@payloadcms.com>
1 parent b3d2054 commit 79f4cc7

1 file changed

Lines changed: 1 addition & 0 deletions

File tree

  • packages/payload/src/bin/generateImportMap

packages/payload/src/bin/generateImportMap/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ export async function writeImportMap({
155155

156156
const importMapOutputFile = `${imports.join('\n')}
157157
158+
/** @type import('payload').ImportMap */
158159
export const importMap = {
159160
${mapKeys.join(',\n')}
160161
}

0 commit comments

Comments
 (0)