cucumber / cucumber-js

Cucumber for JavaScript
https://cucumber.io
MIT License
5.04k stars 1.09k forks source link

change hashes type from any to Record<string, string> #2270

Closed Terence625 closed 1 year ago

Terence625 commented 1 year ago

πŸ€” What's changed?

⚑️ What's your motivation?

🏷️ What kind of change is this?

♻️ Anything particular you want feedback on?

πŸ“‹ Checklist:


This text was originally generated from a template, then edited by hand. You can modify the template here.

coveralls commented 1 year ago

Coverage Status

Coverage: 98.566% (+0.04%) from 98.525% when pulling a1f109a32286fc542d9ffd7d55f3e672d010c727 on Terence625:add-datatable-generic-type into f59ea9f239d98d5b9fdc1abb71bb8378767c181c on cucumber:main.

unional commented 1 year ago

Should it styled as Array<Record<string, string>> instead of Record<string, string>[]? For complex type, IMO Array<T> is more readable than T[].

Terence625 commented 1 year ago

@davidjgoss Thanks for reviewing it, I intended to add generic type for hashes() in DataTable like following:

export default class DataTable<T extends Record<string,string> = {}> {

  hashes(): T[] {
    const copy = this.raw()
    const keys = copy[0]
    const valuesArray = copy.slice(1)
    return valuesArray.map((values) => {
      const rowObject = {} as T
      keys.forEach((key, index) => (rowObject[key as keyof T] = values[index]))
      return rowObject
    })
  }
}

But it throws typescript error: 'string' is assignable to the constraint of type 'T[keyof T]', but 'T[keyof T]' could be instantiated with a different subtype of constraint 'string'.(2322). It seems to be some tricky typescript issue and seems over-engineered.

I'll just do type assertion in my code which will do the same trick:

//feature file
some step definition
inputValue | inputType
test1 | test2
test3 | test4

//step definition file
Then('some step definition', function(datatable: DataTable) {
   const data = datatable.hashes() as {
   inputValue: string,
   inputType: string
 }
)
Terence625 commented 1 year ago

@unional I think they don't have any difference. I just see the original codebase is using [] for array type in other places, I just intended to keep them in the same style.

aslakhellesoy commented 1 year ago

Hi @Terence625,

Thanks for your making your first contribution to Cucumber, and welcome to the Cucumber committers team! You can now push directly to this repo and all other repos under the cucumber organization! 🍾

In return for this generous offer we hope you will:

On behalf of the Cucumber core team, Aslak HellesΓΈy Creator of Cucumber

davidjgoss commented 1 year ago

@unional I think the array-simple flavour of this ESLint rule strikes a good balance, I'd approve a PR to turn that on.