Skip to content

Commit 2ca9038

Browse files
committed
Implement better state metadata constraint types
1 parent 36f9cf4 commit 2ca9038

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

packages/composable-controller/src/ComposableController.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import type {
1010
ControllerStateChangeEvent,
1111
} from '@metamask/base-controller';
1212
import { BaseController } from '@metamask/base-controller';
13-
import type { PublicInterface } from '@metamask/utils';
13+
import type { Json, PublicInterface } from '@metamask/utils';
1414
import type { Patch } from 'immer';
1515

1616
export const controllerName = 'ComposableController';
@@ -39,6 +39,23 @@ type BaseControllerV1Instance = PublicInterface<
3939
BaseControllerV1<ConfigConstraintV1, StateConstraintV1>
4040
>;
4141

42+
/**
43+
* A universal supertype of functions that accept a piece of controller state and return some derivation of that state.
44+
*/
45+
type StateDeriverConstraint = (value: never) => Json;
46+
47+
/**
48+
* A universal supertype of metadata objects for individual state properties.
49+
*/
50+
type StatePropertyMetadataConstraint = {
51+
[P in 'anonymous' | 'persist']: boolean | StateDeriverConstraint;
52+
};
53+
54+
/**
55+
* A universal supertype of state metadata objects.
56+
*/
57+
type StateMetadataConstraint = Record<string, StatePropertyMetadataConstraint>;
58+
4259
/**
4360
* A universal subtype of all controller instances that extend from `BaseController` (formerly `BaseControllerV2`).
4461
* Any `BaseController` instance can be assigned to this type.
@@ -57,7 +74,9 @@ type BaseControllerInstance = Omit<
5774
>
5875
>,
5976
'metadata'
60-
> & { metadata: Record<string, unknown> };
77+
> & {
78+
metadata: StateMetadataConstraint;
79+
};
6180

6281
/**
6382
* A universal subtype of all controller instances that extend from `BaseController` (formerly `BaseControllerV2`) or `BaseControllerV1`.

0 commit comments

Comments
 (0)