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

Type safety of Variant keys #967

Open
cefn opened this issue Jun 21, 2024 · 4 comments
Open

Type safety of Variant keys #967

cefn opened this issue Jun 21, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@cefn
Copy link

cefn commented Jun 21, 2024

Requirements

I was just examining the API to look at adopting a feature-flagging standard and surprised to find that after defining a strict set of variants, there is no Typescript compiler error when providing a defaultVariant which is not in the list of variants. Similarly it is not a compiler error to return a random value for the variant from contextEvaluator.

Can these be made type-safe, please? There should be two compiler errors in the code below, and there are none. See playground

import { InMemoryProvider } from '@openfeature/react-sdk';

const provider = new InMemoryProvider({
    something: {
        disabled: false,
        variants: {
            hey: true,
            yo: false,
        },
        defaultVariant: 'not_a_variant',
        contextEvaluator: (context) => {
            return 'also_not_a_variant'
        }
    }
})

A workaround for type safety is to use a helper function like createVariant below, but to make the most of this, then InMemoryProvider should be defined as generic, to allow the const features of the configuration object passed to it to drive other parts of the API it exposes - e.g. flag names and variants are typed correspondingly when retrieved...

import { InMemoryProvider, EvaluationContext } from "@openfeature/react-sdk";

function createVariant<const T extends Partial<Record<string, unknown>>>(o: {
  variants: T;
  defaultVariant: keyof T;
  contextEvaluator: (context: EvaluationContext) => keyof T;
  disabled: boolean;
}) {
  return o;
}

const provider = new InMemoryProvider({
  something: createVariant({
    disabled: false,
    variants: {
      hey: true,
      yo: false,
    },
    defaultVariant: "not_a_variant",
    contextEvaluator: (context) => {
      return "also_not_a_variant";
    },
  }),
});
@cefn cefn added the enhancement New feature or request label Jun 21, 2024
@beeme1mr
Copy link
Member

Hey @cefn, the InMemoryProvider was mainly built for demo and testing purposes. However, I can see the value of adding an additional type safety. I'll mark this as a Good First Issue.

@beeme1mr beeme1mr added good first issue Good for newcomers help wanted Extra attention is needed labels Jun 21, 2024
@wichopy
Copy link
Member

wichopy commented Sep 28, 2024

Im looking to contribute to this project, and will try and take a stab at this "good first issue" !

@toddbaert
Copy link
Member

@wichopy if you are still interested in this I can assign you.

@wichopy
Copy link
Member

wichopy commented Oct 18, 2024

sure assign me

@beeme1mr beeme1mr removed the help wanted Extra attention is needed label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

4 participants