Skip to content

Commit 9c34e4b

Browse files
GudahttMajorLift
authored andcommitted
Use unknown rather than any for BaseController state (#365)
* Use `unknown` rather than `any` for BaseController state The BaseController state now uses `unknown` rather than `any` as the type for state properties. `unknown` is more type-safe than `any` in cases like this where we don't know what type to expect. See here for details [1]. This was suggested by @rekmarks during review of #362 [2]. [1]: microsoft/TypeScript#24439 [2]: #362 (comment) * Use type alias for controller state rather than interface The mock controller state in the base controller tests now uses a type alias for the controller state rather than an interface. This was required to get around an incompatibility between `Record<string, unknown>` and interfaces[1]. The `@typescript-eslint/consistent-type-definitions` ESLint rule has been disabled, as this problem will be encountered fairly frequently. [1]: microsoft/TypeScript#15300 (comment)
1 parent 2cf1d78 commit 9c34e4b

File tree

3 files changed

+6
-3
lines changed

3 files changed

+6
-3
lines changed

.eslintrc.js

+3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ module.exports = {
2525
'no-shadow': 'off',
2626
'@typescript-eslint/no-shadow': 'error',
2727
'@typescript-eslint/indent': 'off',
28+
// disabled due to incompatibility with Record<string, unknown>
29+
// See https://github.com/Microsoft/TypeScript/issues/15300#issuecomment-702872440
30+
'@typescript-eslint/consistent-type-definitions': 'off',
2831

2932
// TODO re-enable most of these rules
3033
'@typescript-eslint/no-non-null-assertion': 'off',

src/BaseControllerV2.test.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import * as sinon from 'sinon';
33

44
import { BaseController } from './BaseControllerV2';
55

6-
interface MockControllerState {
6+
type MockControllerState = {
77
count: number;
8-
}
8+
};
99

1010
class MockController extends BaseController<MockControllerState> {
1111
update(callback: (state: Draft<MockControllerState>) => void | MockControllerState) {

src/BaseControllerV2.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export type Listener<T> = (state: T, patches: Patch[]) => void;
1414
/**
1515
* Controller class that provides state management and subscriptions
1616
*/
17-
export class BaseController<S extends Record<string, any>> {
17+
export class BaseController<S extends Record<string, unknown>> {
1818
private internalState: S;
1919

2020
private internalListeners: Set<Listener<S>> = new Set();

0 commit comments

Comments
 (0)