feat(messenger): add delegateAll with exhaustive type checking - #8338
Conversation
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
5bd4a95 to
f0e25c9
Compare
`delegate` accepts partial action/event lists without compile-time enforcement that all required items are present. `delegateAll` uses a branded intersection type to produce a clear TS error showing exactly which actions or events are missing.
f0e25c9 to
0bc2ac9
Compare
|
@metamaskbot publish-preview |
|
Preview builds have been published. Learn how to use preview builds in other projects. Expand for full list of packages and versions. |
mcmire
left a comment
There was a problem hiding this comment.
Overall strategy seems sound, but I had some suggestions.
Align delegateAll type params with delegate/revoke, drop casts, and rename the missing-delegations brand for clearer type errors.
Move exhaustiveness checks out of Jest into compile-time tests so missing delegations are verified by the type checker.
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
All alerts resolved. Learn more about Socket for GitHub. This PR previously contained dependency changes with security issues that have been resolved, removed, or ignored. Ignoring alerts on:
|
Keep Jest test files out of the build when excluding TSTyche files, format misc lint failures, and allow messenger's combined test script.
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, have a team admin enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 0f82791. Configure here.
Require every external delegatee action/event in the exhaustiveness check, even when the source type omits them, and cover missing events plus unsupported source deps in tstyche.
Mirror the Jest layout with one Messenger type test file and nested describes instead of a per-method file.
Intentional type errors in Messenger.tst.ts fail project typecheck; keep them under yarn test:types only.
|
@SocketSecurity ignore npm/tstyche@5.0.2 |

Explanation
Messenger.delegate()accepts a partial list of actions/events without compile-time enforcement that all required items are present. This means missing a delegation (e.g., a newly addedAllowedAction) silently compiles and only fails at runtime.delegateAll()wrapsdelegate()with aRequireExhaustivebranded intersection type that produces a clear TypeScript error showing exactly which actions or events are missing from the list. Own-namespace actions of the delegatee are automatically excluded from the check.Example error when an action is missing:
References
Changelog
@metamask/messengerdelegateAll()method — strict alternative todelegate()that requires all external actions and events to be listedMessengerNamespace<M>utility type — extracts the namespace string literal from a Messenger typeRequireExhaustive<Required, Provided>utility type — validates tuple exhaustiveness at compile timeChecklist
Note
Low Risk
API addition only (
delegateAlldelegates to existingdelegate); main risk is consumers adopting stricter typing, not runtime behavior changes.Overview
Adds
Messenger.delegateAll, a typed wrapper arounddelegatethat requires every external action and event on the delegatee messenger to be listed (own-namespace items are excluded). Missing entries fail at compile time via aRequireExhaustivebranded type that surfaces__MISSING_DELEGATIONS__, and the source messenger must also type-check as able to provide those items.Also exports
MessengerNamespacefor extracting a messenger’s namespace literal from its type.Testing/tooling: Jest unit tests for runtime behavior, new TSTyche (
*.tst.ts) compile-time tests,testsplit intotest:unit+test:types, coverage/build/lint configs updated to exclude type-test files, and yarn.config allows the messenger package’s custom test script.Reviewed by Cursor Bugbot for commit 22ed38b. Bugbot is set up for automated code reviews on this repo. Configure here.