timjroberts/cucumber-js-tsflow

Auto-casting regex captured pattern

Kaijiro opened this issue · 4 comments

Hi,

Is it possible to "auto-cast" captured step definitions variables to their real types ?
With the example in the README :

@given(/I enter '(\d*)' and '(\d*)'"/)
public givenTwoNumbers(num1: string, num2: string): void {
    this.computedResult = parseInt(num1) + parseInt(num2);
}

num1 and num2 are currently string but the Regex makes them explicitly integers (number in Typescript). Is there a way to avoid to use parseInt in the step ?

I'm not sure I fully follow. Do you want them to be passed as numbers, or strings? Ultimately, what happens here is driven by cucumber-js core, I don't think this lib is doing anything with the params

I would like to avoid the parseInt part, so I would like to have them passed as numbers

With regex, there isn't much you can do. However with the cucumber style you can define your own types, among the built in ones, which do get type casted. Read more here: https://github.com/cucumber/cucumber-js/blob/master/docs/support_files/api_reference.md

e.g. @when("My number is {int}")

Didn't know the {int} thing would work here. It completely fits my needs. Thank you !