arcjet-js icon indicating copy to clipboard operation
arcjet-js copied to clipboard

Improve inference on "detect"-style functions

Open blaine-arcjet opened this issue 5 months ago • 0 comments

In #1446, I improved the type inference on the detect function by checking if it extends undefined; however, this means that the type inference only works when the detect property does not use the object-function shorthand syntax.

For example, this works:

const [rule] = sensitiveInfo({
        mode: "LIVE",
        allow: ["custom", "CREDIT_CARD_NUMBER"],
// Notice the `:` and arrow function
        detect: (tokens: string[]) => {
          return tokens.map((token) => {
            if (token === "[email protected]") {
              return "custom";
            }
          });
        },
        contextWindowSize: 1,
});

But this does not work:

const [rule] = sensitiveInfo({
        mode: "LIVE",
        allow: ["custom", "CREDIT_CARD_NUMBER"],
// Notice the shorthand syntax
        detect(tokens: string[]) {
          return tokens.map((token) => {
            if (token === "[email protected]") {
              return "custom";
            }
          });
        },
        contextWindowSize: 1,
});

I'm not good enough with TypeScript to know how to solve this inference for both cases.

Additionally, it seems that not specifying tokens: string[] causes the inference to not work either. I'm not sure why when it infers the arguments correctly for me.

blaine-arcjet avatar Aug 30 '24 18:08 blaine-arcjet