launchdarkly / react-client-sdk

LaunchDarkly Client-side SDK for React.js
Other
79 stars 67 forks source link

Declaring an anonymous context doesn't seem possible. #188

Closed borisilic-ap closed 1 year ago

borisilic-ap commented 1 year ago

Is this a support request? No.

Describe the bug With regards to anonymous users there's a mismatch between what the docs say (https://docs.launchdarkly.com/sdk/client-side/javascript/migration-2-to-3#understanding-changes-to-anonymous-users)

Similarly, in the 3.0 version of the SDK, you can omit the context key when building an anonymous context, and the SDK sets the context key to a generated UUID.

and what the typescript interfaces enforce: (https://launchdarkly.github.io/js-client-sdk/interfaces/LDContextCommon.html).

key
key: string
A unique string identifying a context.
Defined in typings.d.ts:348

According to the docs to enable an anonymous user one would omit the key property from the context and set anonymous: true. But omitting the key property causes TS to complain due to it being required.

Screenshot 2023-03-20 at 3 29 42 pm

Thoughts?

Expected behavior Define an anonymous context by being able to omit the key property.

SDK version "launchdarkly-react-client-sdk": "^3.0.0"

Language version, developer tools React.

yusinto commented 1 year ago

@borisilic-ap thanks for reporting this. We are investigating this and will provide an update shortly. Logged internally as 193181.

yusinto commented 1 year ago

3.0.4 has been released and it should fix this issue.

borisilic-ap commented 1 year ago

Thank you!

borisilic-ap commented 1 year ago

I'm back. I'm a little wary of the solution as it is now. It depends on developers reading the documentation to know to specify the key unless anonymous is true rather than the behaviour being enforced by typescript.

Is it possible to implement something along these lines instead: https://stackoverflow.com/a/51412935

yusinto commented 1 year ago

Type aliases are great but is not trivial to change our code base right now to do that. Th anonymous and key props are defined in the common sdk as an interface which are extended elsewhere. Changing this to be a type mean we'll have to re-defined those other interfaces as well. As such, we'll have to stick to the current solution for now and depend on the documentation.