Assert a string as being apart of a subset of strings in typescript

37 Views Asked by At

In one of my scenarios in cucumber, I have a regex which matches either button or link and puts that as a parameter in the callback function

Given(
  /^I click the (button|link) with text "([^"]+)" exactly$/,
  async function (this: ICustomWorld, role: string, text: string) {
    const page = this.page!;
    await page.getByRole(role, { name: text, exact: true }).click();
  },
);

However it gives me an error saying that Argument of type string is not assignable to parameter of type "alert" | "alertdialog"| "application" ....

I wonder if there is a way to maybe say role = 'button' | 'link' to tell the interpreter that role will only have the value button or link?

0

There are 0 best solutions below