DetailsList.types.d.ts incorrect typed getKey() to return a string
Environment Information
- Package version(s): (fill this out)
- Browser and OS versions: (fill this out if relevant)
Please provide a reproduction of the bug in a codepen:
Actual behavior:
https://github.com/OfficeDev/office-ui-fabric-react/blob/48ef2097fcaf941b71e9f2173751d1f6971f4f02/packages/office-ui-fabric-react/src/components/DetailsList/DetailsList.types.ts#L229
is typed the function need to be getKey(item: any, index?: number) => string.
But the implementation in the following will allow null, undefined as return, (note that falsy implemetnation have danger of exclude 0 as valid.)
https://github.com/OfficeDev/office-ui-fabric-react/blob/4c90a78d365e575d5bcaa26f20d3b302352c9949/packages/office-ui-fabric-react/src/components/DetailsList/DetailsList.base.tsx#L949
it expected the typed to be getKey(item: any, index?: number) => string | number| undefined.
But now this cause Typescript compiler error

In addition, it have slightest diff in the implemetnation for the getKey as compare to List.tsx (which seems to be more correct ( specific Falsy check will exclude 0 out as valid key.)
for the reference. https://github.com/OfficeDev/office-ui-fabric-react/blob/31a01f10e45b044960899e3ba7da1d14f3bb5eb8/packages/office-ui-fabric-react/src/components/List/List.tsx#L462
Expected behavior:
private getKey: IDetailsListProps['getKey'] = (item, index): string | number | undefined => { doesn't have compiler error .
Priorities and help requested:
Are you willing to submit a PR to fix? (Yes, No)
Requested priority: (Blocking, High, Normal, Low)
Products/sites affected: (if applicable)
@KevinTCoughlin have you run into this before?
@KevinTCoughlin have you run into this before?
Not personally, but I know this typing is lax and error-prone. It is definitely worth fixing if there's a backwards-compatible change for 6.x, if not then definitely for 7.x branch. It seems like @LiangMingChen is proposing something that would be backwards-compatible.
I would certainly review a PR if @LiangMingChen was willing to raise one before I had a chance to investigate myself.
Long-term, I think it would be ideal if we could type key as keyof T to not arbitrarily enforce items have key property. Or we enforce DetailsList item be of type IObjectWithKey as we have elsewhere. Partner teams will need to update their code (for the better) as part of making DetailsList type-safe. That coordination has been the barrier.
@LiangMingChen, is my understanding correct that you are proposing:
- The following interface be changed to:
getKey(item: any, index?: number) => string | number| undefined
- That
DetailsListimplementation be modified to handle the falsey case asListdoes?
Yes, on both, getKey(item: any, index?: number) => string | number| undefined | null (since some coding standard is null is intentional and undefined as missing.)
Yes, List is more correct, (or better yet, share the implementation from both. -- there doesn't seems to have reason for different version of it since it basically implemented the same thing.)
The test cases should be cover is 0, '' (empty string), as both are legit (currently DetailList will ignore 0 and '' empty string from the this.props.getKey's return. you can argue that '' empty string should not be valid ) -- in that case, then we need to fix up List's implementation. to ignore ''.
Thus Share the code so they can have same behavior?
I took a look into this (sorry for late response, I am new to this codebase). I agree that the api should allow for number. I question allowing for undefined. There are two options I can think of:
- to lax interface but provide solid fallback logic such that the key always has a value.
- to allow for valid final value, string or number, and passing full responsibility to the user which is how it is now.
I think option 1 make sense. Or even the combination of two.
@KevinTCoughlin , do you have an opinion?
I agree that this typing can be improved, but given the larger outstanding issues we have with List component(s) and potential interface changes, I think we should put this on the backlog.
cc: @aditima
Because this issue has not had activity for over 150 days, we're automatically closing it for house-keeping purposes.
Still require assistance? Please, create a new issue with up-to date details.