Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LDContext type is incorrect and errors when accessing kind property #94

Closed
johnnywang opened this issue Jul 21, 2023 · 4 comments
Closed
Labels
waiting for feedback Indicates LaunchDarkly is waiting for customer feedback before issue is closed due to staleness.

Comments

@johnnywang
Copy link

Describe the bug
If you have a variable of type LDContext and try to check/access the kind property, TS will error out at you saying that Property 'kind' does not exist on type 'LDUser'.

To reproduce
Trivial, but something like this should trigger the TS error:

const c: LDContext;
const test = c.kind === 'user';

Expected behavior
Accessing kind should not throw a TS error

SDK version
"launchdarkly-react-native-client-sdk": "^7.1.5"

Language version, developer tools
React Native

OS/platform
MacOS

Additional context
LDUser should probably extend LDSingleKindContext

@johnnywang johnnywang changed the title LDContext type is incorrect LDContext type is incorrect and errors when accessing kind property Jul 21, 2023
@louis-launchdarkly
Copy link
Contributor

Hello @johnnywang, thank you for reporting the issue. We will look into this and follow up once we investigated this.

Filed internally as 210517.

@yusinto
Copy link
Contributor

yusinto commented Jul 28, 2023

Hi @johnnywang, LDContext is a new concept in LaunchDarkly which is an evolution of the legacy LDUser type.

An LDContext can be a single-kind, a multi-kind or a legacy LDUser.

For backward compatibility for now, we allow using LDUser anywhere we accept a LDContext. The absence of kind is used to know that it is an LDUser. If you are only using LDSingleKindContexts or LDMultiKindContexts, then please use those types instead.

We are planning to deprecate and remove LDUser in the future.

@yusinto yusinto added the waiting for feedback Indicates LaunchDarkly is waiting for customer feedback before issue is closed due to staleness. label Jul 28, 2023
@johnnywang
Copy link
Author

Hey @yusinto! Did you try the repro example above? This TS error happens because there's the LDUser is present on the LDContext union type, and because it's missing the kind field, so even if we're NOT using the deprecated LDUser, it'll still throw that error since it's a possible type in the union

@yusinto
Copy link
Contributor

yusinto commented Aug 1, 2023

Hi @johnnywang currently, the LDUser is in the union type for backward compatibility reasons. The LDUser will be removed in the future and by then the kind property will be directly accessible via the union type.

In the meantime, to check for context kind, you can do the following:

function App() {
  const ldClient = useLDClient();
  const context = ldClient?.getContext();

  // detecting context kind
  if (context && 'kind' in context) {
    if (context.kind === 'multi') {
      console.log(`=== multi context`);
    } else {
      console.log(`=== single context: ${context.kind}`);
    }
  } else {
    console.log('=== Legacy LDUser');
  }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
waiting for feedback Indicates LaunchDarkly is waiting for customer feedback before issue is closed due to staleness.
Projects
None yet
Development

No branches or pull requests

4 participants