Skip to content

Commit 1c25d23

Browse files
authored
fix: exclude suppressImplicitAnyIndexErrors from tsconfig and fix type errors (#616)
* Exclude suppressImplicitAnyIndexErrors from tsconfig and fix type errors * Fix type error in logger * Fix type error in event-processor * Incorporate comments * Clean up tsconfig * Update StringInputs type * Update validate config method to not use key of unknown * Remove as 'keyof unknown' from validate method in user_profile_service_validator * Coerce item to type any in keyBy function * Incorporate comments * Define ObjectWithUnknownProperties type
1 parent 41f6c7e commit 1c25d23

File tree

9 files changed

+48
-25
lines changed

9 files changed

+48
-25
lines changed

packages/event-processor/__tests__/pendingEventsStore.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ describe('LocalStorageStore', () => {
9393
{ uuid: '3', timestamp: 3, value: 'third' },
9494
])
9595

96-
const newMap = {}
96+
const newMap: { [key: string]: TestEntry } = {}
9797
store.values().forEach(item => {
9898
newMap[item.uuid] = {
9999
...item,

packages/logging/src/logger.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,15 @@ import { isValidEnum, sprintf } from '@optimizely/js-sdk-utils'
1818

1919
import { LogLevel, LoggerFacade, LogManager, LogHandler } from './models'
2020

21-
const stringToLogLevel = {
21+
type StringToLogLevel = {
22+
NOTSET: number,
23+
DEBUG: number,
24+
INFO: number,
25+
WARNING: number,
26+
ERROR: number,
27+
}
28+
29+
const stringToLogLevel: StringToLogLevel = {
2230
NOTSET: 0,
2331
DEBUG: 1,
2432
INFO: 2,
@@ -36,11 +44,11 @@ function coerceLogLevel(level: any): LogLevel {
3644
level = 'WARNING'
3745
}
3846

39-
if (!stringToLogLevel[level]) {
47+
if (!stringToLogLevel[level as keyof StringToLogLevel]) {
4048
return level
4149
}
4250

43-
return stringToLogLevel[level]
51+
return stringToLogLevel[level as keyof StringToLogLevel]
4452
}
4553

4654
type LogData = {

packages/optimizely-sdk/lib/optimizely/index.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ export interface OptimizelyOptions {
7878
userProfileService?: UserProfileService | null;
7979
}
8080

81+
// TODO: Make feature_key, user_id, variable_key, experiment_key, event_key camelCase
82+
export type InputKey = 'feature_key' | 'user_id' | 'variable_key' | 'experiment_key' | 'event_key' | 'variation_id';
83+
84+
export type StringInputs = Partial<Record<InputKey, unknown>>;
85+
8186
/**
8287
* The Optimizely class
8388
* @param {OptimizelyOptions} config
@@ -581,15 +586,14 @@ export default class Optimizely {
581586

582587
/**
583588
* Validate string inputs, user attributes and event tags.
584-
* @param {unknown} stringInputs Map of string keys and associated values
585-
* @param {unknown} userAttributes Optional parameter for user's attributes
586-
* @param {unknown} eventTags Optional parameter for event tags
587-
* @return {boolean} True if inputs are valid
589+
* @param {StringInputs} stringInputs Map of string keys and associated values
590+
* @param {unknown} userAttributes Optional parameter for user's attributes
591+
* @param {unknown} eventTags Optional parameter for event tags
592+
* @return {boolean} True if inputs are valid
588593
*
589594
*/
590595
private validateInputs(
591-
// TODO: Make feature_key, user_id, variable_key, experiment_key camelCase
592-
stringInputs: Partial<Record<'feature_key' | 'user_id' | 'variable_key' | 'experiment_key' | 'event_key', unknown>>,
596+
stringInputs: StringInputs,
593597
userAttributes?: unknown,
594598
eventTags?: unknown
595599
): boolean {
@@ -603,7 +607,7 @@ export default class Optimizely {
603607
delete stringInputs['user_id'];
604608
}
605609
Object.keys(stringInputs).forEach(key => {
606-
if (!stringValidator.validate(stringInputs[key])) {
610+
if (!stringValidator.validate(stringInputs[key as InputKey])) {
607611
throw new Error(sprintf(ERROR_MESSAGES.INVALID_INPUT_FORMAT, MODULE_NAME, key));
608612
}
609613
})
@@ -1196,7 +1200,7 @@ export default class Optimizely {
11961200

11971201
const decisionObj = this.decisionService.getVariationForFeature(configObj, featureFlag, userId, attributes);
11981202
const featureEnabled = decision.getFeatureEnabledFromVariation(decisionObj);
1199-
const allVariables = {};
1203+
const allVariables: { [variableKey: string]: unknown } = {};
12001204

12011205
featureFlag.variables.forEach((variable: FeatureVariable) => {
12021206
allVariables[variable.key] = this.getFeatureVariableValueFromVariation(featureKey, featureEnabled, decisionObj.variation, variable, userId);

packages/optimizely-sdk/lib/shared_types.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export interface UserProfile {
1111
user_id: string;
1212
experiment_bucket_map: {
1313
[experiment_id: string]: {
14-
variation_id: string;
14+
variation_id: string;
1515
};
1616
};
1717
}
@@ -78,7 +78,7 @@ export interface Variation {
7878
export interface Experiment {
7979
id: string;
8080
key: string;
81-
variationKeyMap: {[key: string]: Variation}
81+
variationKeyMap: { [key: string]: Variation }
8282
}
8383

8484
export interface FeatureVariable {
@@ -94,7 +94,7 @@ export interface FeatureFlag {
9494
id: string;
9595
experimentIds: string[],
9696
variables: FeatureVariable[],
97-
variableKeyMap: {[key: string]: FeatureVariable}
97+
variableKeyMap: { [key: string]: FeatureVariable }
9898
}
9999

100100
export interface FeatureKeyMap {
@@ -106,6 +106,10 @@ export interface OnReadyResult {
106106
reason?: string;
107107
}
108108

109+
export type ObjectWithUnknownProperties = {
110+
[key: string]: unknown;
111+
}
112+
109113
/**
110114
* Optimizely Config Entities
111115
*/

packages/optimizely-sdk/lib/utils/attributes_validator/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { sprintf } from '@optimizely/js-sdk-utils';
17+
import { ObjectWithUnknownProperties } from '../../shared_types';
1718

1819
import fns from '../../utils/fns';
1920
import { ERROR_MESSAGES } from '../enums';
@@ -30,7 +31,7 @@ const MODULE_NAME = 'ATTRIBUTES_VALIDATOR';
3031
export function validate(attributes: unknown): boolean {
3132
if (typeof attributes === 'object' && !Array.isArray(attributes) && attributes !== null) {
3233
Object.keys(attributes).forEach(function(key) {
33-
if (typeof attributes[key] === 'undefined') {
34+
if (typeof (attributes as ObjectWithUnknownProperties)[key] === 'undefined') {
3435
throw new Error(sprintf(ERROR_MESSAGES.UNDEFINED_ATTRIBUTE, MODULE_NAME, key));
3536
}
3637
});

packages/optimizely-sdk/lib/utils/config_validator/index.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* limitations under the License.
1515
*/
1616
import { sprintf } from '@optimizely/js-sdk-utils';
17+
import { ObjectWithUnknownProperties } from '../../shared_types';
1718

1819
import {
1920
ERROR_MESSAGES,
@@ -34,13 +35,17 @@ const SUPPORTED_VERSIONS = [DATAFILE_VERSIONS.V2, DATAFILE_VERSIONS.V3, DATAFILE
3435
*/
3536
export const validate = function(config: unknown): boolean {
3637
if (typeof config === 'object' && config !== null) {
37-
if (config['errorHandler'] && typeof config['errorHandler'].handleError !== 'function') {
38+
const configObj = config as ObjectWithUnknownProperties;
39+
const errorHandler = configObj['errorHandler'];
40+
const eventDispatcher = configObj['eventDispatcher'];
41+
const logger = configObj['logger'];
42+
if (errorHandler && typeof (errorHandler as ObjectWithUnknownProperties)['handleError'] !== 'function') {
3843
throw new Error(sprintf(ERROR_MESSAGES.INVALID_ERROR_HANDLER, MODULE_NAME));
3944
}
40-
if (config['eventDispatcher'] && typeof config['eventDispatcher'].dispatchEvent !== 'function') {
45+
if (eventDispatcher && typeof (eventDispatcher as ObjectWithUnknownProperties)['dispatchEvent'] !== 'function') {
4146
throw new Error(sprintf(ERROR_MESSAGES.INVALID_EVENT_DISPATCHER, MODULE_NAME));
4247
}
43-
if (config['logger'] && typeof config['logger'].log !== 'function') {
48+
if (logger && typeof (logger as ObjectWithUnknownProperties)['log'] !== 'function') {
4449
throw new Error(sprintf(ERROR_MESSAGES.INVALID_LOGGER, MODULE_NAME));
4550
}
4651
return true;
@@ -71,8 +76,8 @@ export const validateDatafile = function(datafile: unknown): any {
7176
}
7277
}
7378
if (typeof datafile === 'object' && !Array.isArray(datafile) && datafile !== null) {
74-
if (SUPPORTED_VERSIONS.indexOf(datafile['version']) === -1) {
75-
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile['version']));
79+
if (SUPPORTED_VERSIONS.indexOf(datafile['version' as keyof unknown]) === -1) {
80+
throw new Error(sprintf(ERROR_MESSAGES.INVALID_DATAFILE_VERSION, MODULE_NAME, datafile['version' as keyof unknown]));
7681
}
7782
}
7883

packages/optimizely-sdk/lib/utils/fns/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,8 @@ function isSafeInteger(number: unknown): boolean {
5252
function keyBy<K>(arr: K[], key: string): { [key: string]: K } {
5353
if (!arr) return {};
5454
return keyByUtil(arr, function (item) {
55-
return item[key];
55+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
56+
return (item as any)[key];
5657
});
5758
}
5859

packages/optimizely-sdk/lib/utils/user_profile_service_validator/index.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
import { sprintf } from '@optimizely/js-sdk-utils';
22+
import { ObjectWithUnknownProperties } from '../../shared_types';
2223

2324
import { ERROR_MESSAGES } from '../enums';
2425

@@ -33,9 +34,9 @@ const MODULE_NAME = 'USER_PROFILE_SERVICE_VALIDATOR';
3334

3435
export function validate(userProfileServiceInstance: unknown): boolean {
3536
if (typeof userProfileServiceInstance === 'object' && userProfileServiceInstance !== null) {
36-
if (typeof userProfileServiceInstance['lookup'] !== 'function') {
37+
if (typeof (userProfileServiceInstance as ObjectWithUnknownProperties)['lookup'] !== 'function') {
3738
throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, MODULE_NAME, "Missing function 'lookup'"));
38-
} else if (typeof userProfileServiceInstance['save'] !== 'function') {
39+
} else if (typeof (userProfileServiceInstance as ObjectWithUnknownProperties)['save'] !== 'function') {
3940
throw new Error(sprintf(ERROR_MESSAGES.INVALID_USER_PROFILE_SERVICE, MODULE_NAME, "Missing function 'save'"));
4041
}
4142
return true;

tsconfig.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
// "composite": true, /* Enable project compilation */
2020
// "removeComments": true, /* Do not emit comments to output. */
2121
// "noEmit": true, /* Do not emit outputs. */
22-
"suppressImplicitAnyIndexErrors": true,
2322
// "importHelpers": true /* Import emit helpers from 'tslib'. */,
2423
// "downlevelIteration": true, /* Provide full support for iterables in 'for-of', spread, and destructuring when targeting 'ES5' or 'ES3'. */
2524
// "isolatedModules": true, /* Transpile each file as a separate module (similar to 'ts.transpileModule'). */

0 commit comments

Comments
 (0)