Skip to content

Optimize type params for Messenger.delegate/revoke#8748

Merged
mcmire merged 2 commits intomainfrom
optimize-messenger-delegate-type-params
May 8, 2026
Merged

Optimize type params for Messenger.delegate/revoke#8748
mcmire merged 2 commits intomainfrom
optimize-messenger-delegate-type-params

Conversation

@mcmire
Copy link
Copy Markdown
Contributor

@mcmire mcmire commented May 8, 2026

Explanation

When TypeScript type-checks arguments to Messenger.delegate and Messenger.revoke it can produce the error "Expression produces a union type that is too complex to represent" if the delegatee contains a large number of capabilities and/or there are large number of capabilities being delegated or revoked. This happens because when TypeScript evaluates MessengerActions<Delegatee> & Action or MessengerEvents<Delegatee> & Event it needs to recursively compute every combination of action/event type — handlers, types used within handlers, everything. The possibility of this error has always been present, but was excerbated by recent changes to the InternalAccount type in keyring-api, which added support for Tron and Stellar.

All of this computing is wasteful. All we really need is an intersection of action/event type strings. This commit changes the types for DelegatedActions and DelegatedEvents to reflect this fact. These changes should be type-compatible since the end result is still the same.

References

Recently a change was made in Mobile to work around this problem. See: MetaMask/metamask-mobile#29621

Checklist

  • I've updated the test suite for new or updated code as appropriate
  • I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate
  • I've communicated my changes to consumers by updating changelogs for packages I've changed
  • I've introduced breaking changes in this PR and have prepared draft pull requests for clients and consumer packages to resolve them

Note

Medium Risk
Public TypeScript API typings for Messenger.delegate/Messenger.revoke change, which could affect downstream type-checking even though runtime behavior is untouched. Risk is limited to compile-time compatibility and inference/regression in complex consumer typings.

Overview
Reduces TypeScript type-checker blowups in Messenger.delegate and Messenger.revoke by narrowing DelegatedActions/DelegatedEvents to intersections of action/event type strings (rather than intersecting full action/event unions), aiming to avoid TS2590 “union type too complex” errors.

Updates the messenger package changelog to note the fix under Unreleased.

Reviewed by Cursor Bugbot for commit cdf1305. Bugbot is set up for automated code reviews on this repo. Configure here.

When TypeScript type-checks arguments to `Messenger.delegate` and
`Messenger.revoke` it can produce the error "Expression produces a union
type that is too complex to represent" if the delegatee contains a large
number of capabilities and/or there are large number of capabilities being
delegated or revoked. This happens because when TypeScript evaluates
`MessengerActions<Delegatee> & Action` or `MessengerEvents<Delegatee> &
Event` it needs to recursively compute every combination of action/event
type — handlers, types used within handlers, everything. The possibility
of this error has always been present, but was excerbated by recent
changes to the `InternalAccount` type in `keyring-api`, which added
support for Tron and Stellar.

All of this computing is wasteful. All we really need is an intersection
of action/event type strings. This commit changes the types for
`DelegatedActions` and `DelegatedEvents` to reflect this fact. These
changes should be type-compatible since the end result is still the
same.
@mcmire mcmire temporarily deployed to default-branch May 8, 2026 14:30 — with GitHub Actions Inactive
@mcmire mcmire marked this pull request as ready for review May 8, 2026 14:52
@mcmire mcmire requested a review from a team as a code owner May 8, 2026 14:52
@mcmire mcmire changed the title Optimize Messenger.delegate type parameters Optimize type params for Messenger.delegate/revoke May 8, 2026
Copy link
Copy Markdown
Member

@Gudahtt Gudahtt left a comment

Choose a reason for hiding this comment

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

LGTM!

@mcmire mcmire added this pull request to the merge queue May 8, 2026
Merged via the queue into main with commit 2740566 May 8, 2026
366 checks passed
@mcmire mcmire deleted the optimize-messenger-delegate-type-params branch May 8, 2026 15:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants