Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BaseControllerV2 metadata is required for all the properties #4612

Merged
merged 9 commits into from
Aug 21, 2024

Conversation

kanthesha
Copy link
Contributor

Explanation

The StateMetadata type in BaseControllerV2 was intended to require each state property to have associated metadata. However, currently it doesn't require metadata for optional fields. This is problematic because all top-level state properties are expected to have metadata, and will throw an error at runtime if it's missing.

We should update the StateMetadata type to require metadata for all properties, including optional properties.

References

Changelog

@metamask/base-controller

  • FIXED: Type of metadata in constructor params has been wrapped in Required to make sure metadata required for all properties, including optional properties.

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 highlighted breaking changes using the "BREAKING" category above as appropriate

@@ -56,7 +56,7 @@ export type AuthenticationControllerState = {
export const defaultState: AuthenticationControllerState = {
isSignedIn: false,
};
const metadata: StateMetadata<AuthenticationControllerState> = {
const metadata = {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This can also be fixed with.

Suggested change
const metadata = {
const metadata: Required<StateMetadata<AuthenticationControllerState>> = {

Type has been removed to be inline with other v2 controllers.

@kanthesha kanthesha marked this pull request as ready for review August 15, 2024 15:30
@kanthesha kanthesha requested review from a team as code owners August 15, 2024 15:30
@MajorLift
Copy link
Contributor

MajorLift commented Aug 19, 2024

Maybe we could update the StateMetadata type itself in base-controller? That way StateMetadata doesn't need to be wrapped in Required everywhere it's used.

diff --git a/packages/base-controller/src/BaseControllerV2.ts b/packages/base-controller/src/BaseControllerV2.ts
index bda31c8cb..eb245bb70 100644
--- a/packages/base-controller/src/BaseControllerV2.ts
+++ b/packages/base-controller/src/BaseControllerV2.ts
@@ -51,7 +51,7 @@ export type StateDeriver<T extends Json> = (value: T) => Json;
 // TODO: Either fix this lint violation or explain why it's necessary to ignore.
 // eslint-disable-next-line @typescript-eslint/naming-convention
 export type StateMetadata<T extends StateConstraint> = {
-  [P in keyof T]: StatePropertyMetadata<T[P]>;
+  [P in keyof T]-?: StatePropertyMetadata<T[P]>;
 };
 
 /**

@kanthesha
Copy link
Contributor Author

Maybe we could update the StateMetadata type itself in base-controller? That way StateMetadata doesn't need to be wrapped in Required everywhere it's used.

diff --git a/packages/base-controller/src/BaseControllerV2.ts b/packages/base-controller/src/BaseControllerV2.ts
index bda31c8cb..eb245bb70 100644
--- a/packages/base-controller/src/BaseControllerV2.ts
+++ b/packages/base-controller/src/BaseControllerV2.ts
@@ -51,7 +51,7 @@ export type StateDeriver<T extends Json> = (value: T) => Json;
 // TODO: Either fix this lint violation or explain why it's necessary to ignore.
 // eslint-disable-next-line @typescript-eslint/naming-convention
 export type StateMetadata<T extends StateConstraint> = {
-  [P in keyof T]: StatePropertyMetadata<T[P]>;
+  [P in keyof T]-?: StatePropertyMetadata<T[P]>;
 };
 
 /**

Make sense. Included the suggestion in this commit.

Copy link
Contributor

@MajorLift MajorLift left a comment

Choose a reason for hiding this comment

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

LGTM!

@kanthesha kanthesha merged commit 6a72ae1 into main Aug 21, 2024
116 checks passed
@kanthesha kanthesha deleted the fix/require-metadata-base-controller-v2 branch August 21, 2024 11:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[base-controller] Require metadata for optional state properties
3 participants