Closed Terence625 closed 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[]
.
@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
}
)
@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.
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
@unional I think the array-simple
flavour of this ESLint rule strikes a good balance, I'd approve a PR to turn that on.
π€ 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.