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

Types could catch invalid contexts at build time #110

Open
coracuity opened this issue Aug 26, 2024 · 1 comment
Open

Types could catch invalid contexts at build time #110

coracuity opened this issue Aug 26, 2024 · 1 comment

Comments

@coracuity
Copy link

Describe the bug

The LDContextCommon type allows invalid contexts. The key field is required unless anonymous is set to true and this can be enforced at the type level. In the case key is not set and anonymous is undefined or false, the error may be missed during testing resulting in targeted features being unavailable.

This type would catch this issue for a multi-kind context:

interface LDMultiKindContext {
  kind: "multi";
  [kind: string]:
    | "multi"
    | (Omit<LDContextCommon, "key" | "anonymous"> & { key: string; anonymous?: false })
    | (Omit<LDContextCommon, "key" | "anonymous"> & { key?: string; anonymous: true });
}

To reproduce

In React:

const ldClient = useLDClient();
ldClient?.identify({ kind: "multi", user: { kind: "user", userId: "1234" } }, undefined, (error, flags) => {
  console.error("failed to initialize!", { error, flags });
});

Expected behavior

I would expect this to be caught by types.

Logs

N/A

SDK version

We're using js-sdk-common 5.2.0 at the moment, but the problem is present in js-sdk-common 5.3.0 as well.

Language version, developer tools

Node 20

OS/platform

macos, but this would happen on any platform.

Additional context

None

@coracuity
Copy link
Author

Would this be something you'd accept a PR for?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant