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

Improve inference on "detect"-style functions #1455

Open
blaine-arcjet opened this issue Aug 30, 2024 · 0 comments
Open

Improve inference on "detect"-style functions #1455

blaine-arcjet opened this issue Aug 30, 2024 · 0 comments

Comments

@blaine-arcjet
Copy link
Contributor

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.

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