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

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

Merged
merged 4 commits into from Mar 28, 2023

Conversation

Terence625
Copy link
Contributor

πŸ€” What's changed?

⚑️ What's your motivation?

🏷️ What kind of change is this?

  • 🏦 Refactoring/debt/DX (improvement to code design, tooling, documentation etc. without changing behaviour)
  • πŸ› Bug fix (non-breaking change which fixes a defect)
  • ⚑ New feature (non-breaking change which adds new behaviour)
  • πŸ’₯ Breaking change (incompatible changes to the API)

♻️ Anything particular you want feedback on?

πŸ“‹ Checklist:

  • I agree to respect and uphold the Cucumber Community Code of Conduct
  • I've changed the behaviour of the code
    • I have added/updated tests to cover my changes.
  • My change requires a change to the documentation.
    • I have updated the documentation accordingly.
  • Users should know about my change
    • I have added an entry to the "Unreleased" section of the CHANGELOG, linking to this pull request.

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

@coveralls
Copy link

coveralls commented Mar 26, 2023

Coverage Status

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

Copy link
Contributor

@davidjgoss davidjgoss left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for doing this! Would you mind adding an entry to the unreleased section of CHANGELOG.md? Although this is a small change, it will improve types for end users so we should include it.

@unional
Copy link

unional commented Mar 26, 2023

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
Copy link
Contributor Author

@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
Copy link
Contributor Author

@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.

@davidjgoss davidjgoss merged commit aae711c into cucumber:main Mar 28, 2023
10 checks passed
@aslakhellesoy
Copy link
Contributor

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:

  • βœ… Continue to use branches and pull requests. When someone on the core team approves a pull request (yours or someone else's), you're welcome to merge it yourself.
  • πŸ’š Commit to setting a good example by following and upholding our code of conduct in your interactions with other collaborators and users.
  • πŸ’¬ Join the community Slack channel to meet the rest of the team and make yourself at home.
  • ℹ️ Don't feel obliged to help, just do what you can if you have the time and the energy.
  • πŸ™‹ Ask if you need anything. We're looking for feedback about how to make the project more welcoming, so please tell us!

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

@davidjgoss
Copy link
Contributor

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

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

Successfully merging this pull request may close these issues.

None yet

5 participants