Skip to content

[BUG] Type soundness issue with Provider.resolveObjectEvaluation #1158

Open
@adams85

Description

@adams85

Observed behavior

Currently the TypeScript method signature of resolveObjectEvaluation makes a promise that the SDK cannot keep at run-time, which allows the caller to make mistakes.

For example:

const result = await provider.resolveObjectEvaluation('jsonFlag', { "prop": "default" }, { targetingKey }));
// At this point, the inferred compile-time type of the `result` variable is `{ "prop": string }`, which may not be in sync with the actual run-time type.
console.log(result.prop.length); // throws "TypeError: result.prop is undefined"

Since the default value type and the return value type is connected via the T type parameter, the inferred type of result will be { "prop": string } in the example above. However, when the value resolution is successful and an actual value is returned, nothing guarantees that the resolved value has a string property named "prop". Some of the providers not even guarantee that the returned value is an object.

Expected Behavior

In my opinion, the inferred type of result should be JsonValue, that is, the correct method signature should look like this:

resolveObjectEvaluation(flagKey: string, defaultValue: JsonValue, context: EvaluationContext, logger: Logger): Promise<ResolutionDetails<JsonValue>>;

This way the SDK wouldn't make a "false promise" and would force the caller to think about the return value type.

For example:

const result = await provider.resolveObjectEvaluation('jsonFlag', { "prop": "default" }, { targetingKey }));
// At this point, the inferred compile-time type of the `result` variable is `JsonValue`, that is, caller needs to make checks or assertions on the actual run-time type.
if (typeof result === "object" && typeof result.prop === "string") {
  console.log(result.prop.length);
}

Steps to reproduce

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions