Skip to content

Commit 43b41bd

Browse files
rubennortemeta-codesync[bot]
authored andcommitted
Make Settings modules Flow strict-local (#57736)
Summary: Pull Request resolved: #57736 Upgrade the Settings modules from `flow` to `flow strict-local`, preserving public type signatures so consumers are unaffected. Values read from the settings store are of arbitrary shape, so those internal `any` usages are marked with scoped `$FlowFixMe[unclear-type]`. Changelog: [Internal] Reviewed By: javache Differential Revision: D113763793
1 parent 4060b78 commit 43b41bd

3 files changed

Lines changed: 38 additions & 29 deletions

File tree

packages/react-native/Libraries/Settings/Settings.ios.js

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,25 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

1111
import RCTDeviceEventEmitter from '../EventEmitter/RCTDeviceEventEmitter';
1212
import NativeSettingsManager from './NativeSettingsManager';
1313
import invariant from 'invariant';
1414

15+
type SettingsValues = {[string]: unknown, ...};
16+
type SettingsCallback = () => void;
17+
18+
declare function castSetting<T>(value: unknown): T;
19+
function castSetting(value: unknown) {
20+
return value;
21+
}
22+
1523
const subscriptions: Array<{
1624
keys: Array<string>,
17-
callback: ?Function,
25+
callback: ?SettingsCallback,
1826
...
1927
}> = [];
2028

@@ -25,26 +33,31 @@ const subscriptions: Array<{
2533
* @see https://reactnative.dev/docs/settings
2634
* @platform ios
2735
*/
28-
const Settings = {
29-
_settings: (NativeSettingsManager &&
30-
NativeSettingsManager.getConstants().settings) as any,
36+
const Settings: {
37+
_settings: SettingsValues,
38+
get<T = unknown>(key: string): ?T,
39+
set(settings: SettingsValues): void,
40+
watchKeys(keys: string | Array<string>, callback: SettingsCallback): number,
41+
clearWatch(watchId: number): void,
42+
_sendObservations(body: SettingsValues): void,
43+
} = {
44+
_settings: NativeSettingsManager.getConstants().settings,
3145

3246
/**
3347
* Get the current value for the given key.
3448
*/
35-
get(key: string): unknown {
36-
// $FlowFixMe[object-this-reference]
37-
return this._settings[key];
49+
get<T = unknown>(key: string): ?T {
50+
return castSetting(Settings._settings[key]);
3851
},
3952

4053
/**
4154
* Set one or more values by merging the provided object into the current
4255
* settings.
4356
*/
44-
set(settings: Object) {
45-
// $FlowFixMe[object-this-reference]
46-
// $FlowFixMe[unsafe-object-assign]
47-
this._settings = Object.assign(this._settings, settings);
57+
set(settings: SettingsValues) {
58+
Object.keys(settings).forEach(key => {
59+
Settings._settings[key] = settings[key];
60+
});
4861
NativeSettingsManager.setValues(settings);
4962
},
5063

@@ -53,18 +66,16 @@ const Settings = {
5366
* whenever a watched key's value changes. Returns a `watchId` that can be
5467
* passed to `clearWatch` to unsubscribe.
5568
*/
56-
watchKeys(keys: string | Array<string>, callback: Function): number {
57-
if (typeof keys === 'string') {
58-
keys = [keys];
59-
}
69+
watchKeys(keys: string | Array<string>, callback: SettingsCallback): number {
70+
const watchedKeys = typeof keys === 'string' ? [keys] : keys;
6071

6172
invariant(
62-
Array.isArray(keys),
73+
Array.isArray(watchedKeys),
6374
'keys should be a string or array of strings',
6475
);
6576

6677
const sid = subscriptions.length;
67-
subscriptions.push({keys: keys, callback: callback});
78+
subscriptions.push({keys: watchedKeys, callback});
6879
return sid;
6980
},
7081

@@ -77,13 +88,11 @@ const Settings = {
7788
}
7889
},
7990

80-
_sendObservations(body: Object) {
91+
_sendObservations(body: SettingsValues) {
8192
Object.keys(body).forEach(key => {
8293
const newValue = body[key];
83-
// $FlowFixMe[object-this-reference]
84-
const didChange = this._settings[key] !== newValue;
85-
// $FlowFixMe[object-this-reference]
86-
this._settings[key] = newValue;
94+
const didChange = Settings._settings[key] !== newValue;
95+
Settings._settings[key] = newValue;
8796

8897
if (didChange) {
8998
subscriptions.forEach(sub => {

packages/react-native/Libraries/Settings/Settings.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

1111
import Platform from '../Utilities/Platform';
1212

1313
let Settings: {
14-
get(key: string): any,
15-
set(settings: Object): void,
14+
get<T = unknown>(key: string): ?T,
15+
set(settings: {[string]: unknown, ...}): void,
1616
watchKeys(keys: string | Array<string>, callback: () => void): number,
1717
clearWatch(watchId: number): void,
1818
...

packages/react-native/Libraries/Settings/SettingsFallback.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,19 @@
44
* This source code is licensed under the MIT license found in the
55
* LICENSE file in the root directory of this source tree.
66
*
7-
* @flow
7+
* @flow strict-local
88
* @format
99
*/
1010

1111
'use strict';
1212

1313
const Settings = {
14-
get(key: string): any {
14+
get<T = unknown>(key: string): ?T {
1515
console.warn('Settings is not yet supported on this platform.');
1616
return null;
1717
},
1818

19-
set(settings: Object) {
19+
set(settings: {[string]: unknown, ...}) {
2020
console.warn('Settings is not yet supported on this platform.');
2121
},
2222

0 commit comments

Comments
 (0)