From 0be7f9fc4518f9cef03d2aface755790e166a288 Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Thu, 22 Feb 2024 12:47:20 -0500 Subject: [PATCH 1/6] Fix any usage in `BaseControllerV1` for runtime property assignment. Replace with `as unknown as` --- packages/base-controller/src/BaseControllerV1.ts | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/packages/base-controller/src/BaseControllerV1.ts b/packages/base-controller/src/BaseControllerV1.ts index 30efb44569..d4b0d5dcc1 100644 --- a/packages/base-controller/src/BaseControllerV1.ts +++ b/packages/base-controller/src/BaseControllerV1.ts @@ -128,21 +128,19 @@ export class BaseControllerV1 { ? (config as C) : Object.assign(this.internalConfig, config); - for (const [key, value] of Object.entries(this.internalConfig)) { + for (const key of Object.keys(this.internalConfig) as (keyof C)[]) { + const value = this.internalConfig[key]; if (value !== undefined) { - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (this as any)[key] = value; + (this as unknown as C)[key] = value; } } } else { for (const key of Object.keys(config) as (keyof C)[]) { /* istanbul ignore else */ - if (typeof this.internalConfig[key] !== 'undefined') { - this.internalConfig[key] = (config as C)[key]; - // TODO: Replace `any` with type - // eslint-disable-next-line @typescript-eslint/no-explicit-any - (this as any)[key] = config[key]; + if (this.internalConfig[key] !== undefined) { + const value = (config as C)[key]; + this.internalConfig[key] = value; + (this as unknown as C)[key] = value; } } } From 6fc37a143a4a1130f2e7299608ca7c4554e0c8c0 Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Thu, 22 Feb 2024 12:49:46 -0500 Subject: [PATCH 2/6] Convert `BaseConfig`, BaseState` interfaces to types --- packages/base-controller/CHANGELOG.md | 5 +++++ packages/base-controller/src/BaseControllerV1.ts | 14 ++++---------- 2 files changed, 9 insertions(+), 10 deletions(-) diff --git a/packages/base-controller/CHANGELOG.md b/packages/base-controller/CHANGELOG.md index 52ee840f1d..e3921a9df7 100644 --- a/packages/base-controller/CHANGELOG.md +++ b/packages/base-controller/CHANGELOG.md @@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- **BREAKING:** Convert `BaseConfig`, `BaseState` interfaces to types. + - As types, `BaseConfig`, `BaseState` now extend `Record` and have an index signature of `string`. + ## [4.1.1] ### Changed diff --git a/packages/base-controller/src/BaseControllerV1.ts b/packages/base-controller/src/BaseControllerV1.ts index d4b0d5dcc1..0c11e52bf6 100644 --- a/packages/base-controller/src/BaseControllerV1.ts +++ b/packages/base-controller/src/BaseControllerV1.ts @@ -9,12 +9,9 @@ export type Listener = (state: T) => void; * Base controller configuration * @property disabled - Determines if this controller is enabled */ -// This interface was created before this ESLint rule was added. -// Convert to a `type` in a future major version. -// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export interface BaseConfig { +export type BaseConfig = { disabled?: boolean; -} +}; /** * @type BaseState @@ -22,12 +19,9 @@ export interface BaseConfig { * Base state representation * @property name - Unique name for this controller */ -// This interface was created before this ESLint rule was added. -// Convert to a `type` in a future major version. -// eslint-disable-next-line @typescript-eslint/consistent-type-definitions -export interface BaseState { +export type BaseState = { name?: string; -} +}; /** * @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead. From 88d8c66ad43a5877bf4c4fd69038c881803c7f0c Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Thu, 22 Feb 2024 12:54:49 -0500 Subject: [PATCH 3/6] For empty objects, prefer no type assertions or `as never` - `never` is assignable to all types --- packages/base-controller/src/BaseControllerV1.ts | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/packages/base-controller/src/BaseControllerV1.ts b/packages/base-controller/src/BaseControllerV1.ts index 0c11e52bf6..35ce2223de 100644 --- a/packages/base-controller/src/BaseControllerV1.ts +++ b/packages/base-controller/src/BaseControllerV1.ts @@ -36,12 +36,12 @@ export class BaseControllerV1 { /** * Default options used to configure this controller */ - defaultConfig: C = {} as C; + defaultConfig: C = {} as never; /** * Default state set on this controller */ - defaultState: S = {} as S; + defaultState: S = {} as never; /** * Determines if listeners are notified of state changes @@ -70,10 +70,7 @@ export class BaseControllerV1 { * @param config - Initial options used to configure this controller. * @param state - Initial state to set on this controller. */ - constructor(config: Partial = {} as C, state: Partial = {} as S) { - // Use assign since generics can't be spread: https://git.io/vpRhY - this.initialState = state as S; - this.initialConfig = config as C; + constructor(config: Partial = {}, state: Partial = {}) { } /** From 538c5c1313eaa801d5f3edeeadb2419d3e569d2c Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Thu, 22 Feb 2024 13:47:03 -0500 Subject: [PATCH 4/6] Fix code written based on outdated TypeScript limitation - Generic spread expressions for object literals are supported by TypeScript: https://github.com/microsoft/TypeScript/pull/28234 --- packages/base-controller/src/BaseControllerV1.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/packages/base-controller/src/BaseControllerV1.ts b/packages/base-controller/src/BaseControllerV1.ts index 35ce2223de..b1863fcc0a 100644 --- a/packages/base-controller/src/BaseControllerV1.ts +++ b/packages/base-controller/src/BaseControllerV1.ts @@ -71,6 +71,8 @@ export class BaseControllerV1 { * @param state - Initial state to set on this controller. */ constructor(config: Partial = {}, state: Partial = {}) { + this.initialState = { ...(state as S) }; + this.initialConfig = { ...(config as C) }; } /** From 0a6698764b016a3ee87a4a45d4b6460360cbba43 Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Mon, 26 Feb 2024 08:53:18 -0500 Subject: [PATCH 5/6] Change the types for `BaseControllerV1` class fields `initialConfig`, `initialState` from `C`, `S` to `Partial`, `Partial` --- packages/base-controller/CHANGELOG.md | 2 +- packages/base-controller/src/BaseControllerV1.ts | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/base-controller/CHANGELOG.md b/packages/base-controller/CHANGELOG.md index e3921a9df7..c0880b2c7a 100644 --- a/packages/base-controller/CHANGELOG.md +++ b/packages/base-controller/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- **BREAKING:** Convert `BaseConfig`, `BaseState` interfaces to types. +- **BREAKING:** Convert `BaseConfig`, `BaseState` interfaces to types ([#3959](https://github.com/MetaMask/core/pull/3959)) - As types, `BaseConfig`, `BaseState` now extend `Record` and have an index signature of `string`. ## [4.1.1] diff --git a/packages/base-controller/src/BaseControllerV1.ts b/packages/base-controller/src/BaseControllerV1.ts index b1863fcc0a..71565c4b31 100644 --- a/packages/base-controller/src/BaseControllerV1.ts +++ b/packages/base-controller/src/BaseControllerV1.ts @@ -53,9 +53,9 @@ export class BaseControllerV1 { */ name = 'BaseController'; - private readonly initialConfig: C; + private readonly initialConfig: Partial; - private readonly initialState: S; + private readonly initialState: Partial; private internalConfig: C = this.defaultConfig; @@ -71,8 +71,8 @@ export class BaseControllerV1 { * @param state - Initial state to set on this controller. */ constructor(config: Partial = {}, state: Partial = {}) { - this.initialState = { ...(state as S) }; - this.initialConfig = { ...(config as C) }; + this.initialState = state; + this.initialConfig = config; } /** From 8347a2acb748fb291c84176dcbe10b9b44b0261f Mon Sep 17 00:00:00 2001 From: Jongsun Suh Date: Mon, 26 Feb 2024 08:54:00 -0500 Subject: [PATCH 6/6] Revert "Convert `BaseConfig`, BaseState` interfaces to types" This reverts commit 93a997bf1e30c797db8f4a9d964a2ad470db28db. --- packages/base-controller/CHANGELOG.md | 5 ----- packages/base-controller/src/BaseControllerV1.ts | 14 ++++++++++---- 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/packages/base-controller/CHANGELOG.md b/packages/base-controller/CHANGELOG.md index c0880b2c7a..52ee840f1d 100644 --- a/packages/base-controller/CHANGELOG.md +++ b/packages/base-controller/CHANGELOG.md @@ -7,11 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -### Changed - -- **BREAKING:** Convert `BaseConfig`, `BaseState` interfaces to types ([#3959](https://github.com/MetaMask/core/pull/3959)) - - As types, `BaseConfig`, `BaseState` now extend `Record` and have an index signature of `string`. - ## [4.1.1] ### Changed diff --git a/packages/base-controller/src/BaseControllerV1.ts b/packages/base-controller/src/BaseControllerV1.ts index 71565c4b31..c075684fa8 100644 --- a/packages/base-controller/src/BaseControllerV1.ts +++ b/packages/base-controller/src/BaseControllerV1.ts @@ -9,9 +9,12 @@ export type Listener = (state: T) => void; * Base controller configuration * @property disabled - Determines if this controller is enabled */ -export type BaseConfig = { +// This interface was created before this ESLint rule was added. +// Convert to a `type` in a future major version. +// eslint-disable-next-line @typescript-eslint/consistent-type-definitions +export interface BaseConfig { disabled?: boolean; -}; +} /** * @type BaseState @@ -19,9 +22,12 @@ export type BaseConfig = { * Base state representation * @property name - Unique name for this controller */ -export type BaseState = { +// This interface was created before this ESLint rule was added. +// Convert to a `type` in a future major version. +// eslint-disable-next-line @typescript-eslint/consistent-type-definitions +export interface BaseState { name?: string; -}; +} /** * @deprecated This class has been renamed to BaseControllerV1 and is no longer recommended for use for controllers. Please use BaseController (formerly BaseControllerV2) instead.