Skip to content

package.json exports should have priority over typesVersions#50890

Merged
andrewbranch merged 2 commits intomicrosoft:mainfrom
andrewbranch:bug/exports-should-block-types-versions
Sep 22, 2022
Merged

package.json exports should have priority over typesVersions#50890
andrewbranch merged 2 commits intomicrosoft:mainfrom
andrewbranch:bug/exports-should-block-types-versions

Conversation

@andrewbranch
Copy link
Copy Markdown
Member

@andrewbranch andrewbranch commented Sep 21, 2022

package.json exports is a superset of the features supported by typesVersions, but currently typesVersions is consulted first. This prevents TypeScript from recognizing the exports for a path that also matches a typesVersions path, which is a big problem if the package wants to use conditional exports that benefit --moduleResolution nodenext and typesVersions for --moduleResolution node. As far as I can tell, this was just an oversight, as no existing tests broke when I changed the order.

Thanks to @fictitious for noticing at andrewbranch/example-subpath-exports-ts-compat#1

Copy link
Copy Markdown
Member

@weswigham weswigham left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe a types@>=4 condition or the like would be useful as a test alongside the typesVersions object, too, if you're up for adding another test case.

@andrewbranch andrewbranch merged commit 221cf55 into microsoft:main Sep 22, 2022
@andrewbranch andrewbranch deleted the bug/exports-should-block-types-versions branch September 22, 2022 00:21
@Andarist
Copy link
Copy Markdown
Contributor

This probably needs a big call out in the docs in the typesVersions section (and somewhere in the moduleResolution stuff too).

@andrewbranch andrewbranch added the Breaking Change Would introduce errors in existing code label Sep 22, 2022
@DanielRosenwasser
Copy link
Copy Markdown
Member

DanielRosenwasser commented Sep 23, 2022

How's this for the release notes?

Previously, TypeScript incorrectly prioritized the typesVersions field over the exports field when resolving through a package.json under --moduleResolution node16.
If this change impacts your library, you may need to add types@ version selectors in your package.json's exports field.

  {
      "type": "module",
      "main": "./dist/main.js"
      "typesVersions": {
          "<4.8": { ".": ["4.8-types/main.d.ts"] },
          "*": { ".": ["modern-types/main.d.ts"] }
      },
      "exports": {
          ".": {
+             "types@<4.8": "4.8-types/main.d.ts",
+             "types": "modern-types/main.d.ts",
              "import": "./dist/main.js"
          }
      }
  }

@Andarist
Copy link
Copy Markdown
Contributor

q: shouldn't the presented diff only add the types@<4.8 key? With the logic from this PR in place - are typesVersions still handled with --moduleResolution node16 if there no types-related key in package.json#exports? Or are those completely ignored with this moduleResolution?

@andrewbranch
Copy link
Copy Markdown
Member Author

The presence of exports blocks any non-exports keys from contributing to package resolution. That has always been the intention and has been the case for the types and main fields.

@weswigham
Copy link
Copy Markdown
Member

    "exports": {
         ".": {
            "import": {
                "types@<4.8": "4.8-types/main.d.ts",
                "types": "modern-types/main.d.ts",
                "default": "./dist/main.js"
             }
         }
     }

Is how you should shape that export object, to accommodate types for a require condition or similar.

crapStone pushed a commit to Calciumdibromid/CaBr2 that referenced this pull request Jan 12, 2023
This PR contains the following updates:

| Package | Type | Update | Change |
|---|---|---|---|
| [typescript](https://www.typescriptlang.org/) ([source](https://github.com/Microsoft/TypeScript)) | devDependencies | minor | [`4.8.4` -> `4.9.4`](https://renovatebot.com/diffs/npm/typescript/4.8.4/4.9.4) |

---

### Release Notes

<details>
<summary>Microsoft/TypeScript</summary>

### [`v4.9.4`](https://github.com/microsoft/TypeScript/releases/tag/v4.9.4): TypeScript 4.9.4

[Compare Source](https://github.com/Microsoft/TypeScript/compare/v4.9.3...v4.9.4)

For release notes, check out the [release announcement](https://devblogs.microsoft.com/typescript/announcing-typescript-4-9).

For the complete list of fixed issues, check out the

-   [fixed issues query for Typescript v4.9.4](https://github.com/Microsoft/TypeScript/issues?utf8=%E2%9C%93\&q=is%3Aissue+milestone%3A%22TypeScript+4.9.4%22+is%3Aclosed+).

Downloads are available on:

-   [npm](https://www.npmjs.com/package/typescript)
-   [NuGet package](https://app