Skip to content

Commit eda5b9d

Browse files
authored
[base-controller] Fix any usage in BaseControllerV1 (#3959)
## Explanation - For runtime property assignment, use `as unknown as` instead of `as any`. - Change the types for `BaseControllerV1` class fields `initialConfig`, `initialState` from `C`, `S` to `Partial<C>`, `Partial<S>`. - Initial user-supplied constructor options do not need to be complete `C`, `S` objects, since `internal{Config,State}` will be populated with `default{Config,State}`. - For empty objects, prefer no type assertions or `as never` (`never` is assignable to all types). - Fix code written based on outdated TypeScript limitation. - Generic spread expressions for object literals are supported by TypeScript: microsoft/TypeScript#28234 ## References - Closes #3715 ## Changelog ### [`@metamask/base-controller`](https://github.com/MetaMask/core/pull/3959/files#diff-a8212838da15b445582e5622bd4cc8195e4c52bcf87210af8074555f806706a9) ## Checklist - [x] I've updated the test suite for new or updated code as appropriate - [x] I've updated documentation (JSDoc, Markdown, etc.) for new or updated code as appropriate - [x] I've highlighted breaking changes using the "BREAKING" category above as appropriate
1 parent 26ae591 commit eda5b9d

File tree

1 file changed

+14
-17
lines changed

1 file changed

+14
-17
lines changed

packages/base-controller/src/BaseControllerV1.ts

+14-17
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ export class BaseControllerV1<C extends BaseConfig, S extends BaseState> {
4242
/**
4343
* Default options used to configure this controller
4444
*/
45-
defaultConfig: C = {} as C;
45+
defaultConfig: C = {} as never;
4646

4747
/**
4848
* Default state set on this controller
4949
*/
50-
defaultState: S = {} as S;
50+
defaultState: S = {} as never;
5151

5252
/**
5353
* Determines if listeners are notified of state changes
@@ -59,9 +59,9 @@ export class BaseControllerV1<C extends BaseConfig, S extends BaseState> {
5959
*/
6060
name = 'BaseController';
6161

62-
private readonly initialConfig: C;
62+
private readonly initialConfig: Partial<C>;
6363

64-
private readonly initialState: S;
64+
private readonly initialState: Partial<S>;
6565

6666
private internalConfig: C = this.defaultConfig;
6767

@@ -76,10 +76,9 @@ export class BaseControllerV1<C extends BaseConfig, S extends BaseState> {
7676
* @param config - Initial options used to configure this controller.
7777
* @param state - Initial state to set on this controller.
7878
*/
79-
constructor(config: Partial<C> = {} as C, state: Partial<S> = {} as S) {
80-
// Use assign since generics can't be spread: https://git.io/vpRhY
81-
this.initialState = state as S;
82-
this.initialConfig = config as C;
79+
constructor(config: Partial<C> = {}, state: Partial<S> = {}) {
80+
this.initialState = state;
81+
this.initialConfig = config;
8382
}
8483

8584
/**
@@ -128,21 +127,19 @@ export class BaseControllerV1<C extends BaseConfig, S extends BaseState> {
128127
? (config as C)
129128
: Object.assign(this.internalConfig, config);
130129

131-
for (const [key, value] of Object.entries(this.internalConfig)) {
130+
for (const key of Object.keys(this.internalConfig) as (keyof C)[]) {
131+
const value = this.internalConfig[key];
132132
if (value !== undefined) {
133-
// TODO: Replace `any` with type
134-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
135-
(this as any)[key] = value;
133+
(this as unknown as C)[key] = value;
136134
}
137135
}
138136
} else {
139137
for (const key of Object.keys(config) as (keyof C)[]) {
140138
/* istanbul ignore else */
141-
if (typeof this.internalConfig[key] !== 'undefined') {
142-
this.internalConfig[key] = (config as C)[key];
143-
// TODO: Replace `any` with type
144-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
145-
(this as any)[key] = config[key];
139+
if (this.internalConfig[key] !== undefined) {
140+
const value = (config as C)[key];
141+
this.internalConfig[key] = value;
142+
(this as unknown as C)[key] = value;
146143
}
147144
}
148145
}

0 commit comments

Comments
 (0)