-
-
Notifications
You must be signed in to change notification settings - Fork 148
Fix TypeScript type for Column render
function
#711
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
Fix TypeScript type for Column render
function
#711
Conversation
This problem and solution is described at length here: https://stackoverflow.com/a/58632009/4543977 It avoids `Parameter 'rowData' implicitly has an 'any' type.` TypeScript errors which were caused by the previous function union (rather than intersection).
Yes this looks better, was was working on it and am trying to get the correct type narrowing. It would be possible by using an object like: render: |
Thanks for discussing @Domino987! I'd be open to the object-based argument and wouldn't mind it being a breaking change, personally. Either way works for me! By the way, I believe there's a somewhat similar typing issue for customSort?: (
data1: RowData,
data2: RowData,
type: 'row' | 'group',
sortDirection?: 'desc' | 'asc'
) => number; However, when |
Yes that sounds like a reasonable thing, since the current implementation is years old from before the merge, its time to update it and to get more concise and stable params, will change it. But ill keep it as two params, since we just move it to be an object for the first and no narrowing is needed. |
@sjdemartini There is one more question, which data object should be passed to the grouped row render function, the first one? |
Hm, it seems to me like it would always be easier to get |
Yes I am aware but which rowdata would you expect. Which item from data should be passed |
Ah, I see. I don't personally use the grouping functionality, so I'm not very familiar with the nuances/use-case. I guess when {
data: RowData;
type: 'row';
} | {
data: any;
type: 'group';
} but I'll defer to you. Passing a particular row's data does feel a bit strange/arbitrary in that case. I don't have any strong opinion about the grouping functionality, just want to make sure these functions can more easily be written and be valid in TS. Maybe another option would be to have |
Related Issue
n/a
Description
This avoids
Parameter 'rowData' implicitly has an 'any' type.
TypeScript errors which were caused by the previous function union (rather than intersection), where no validrender
function could be supplied by users of theMaterialTable
component.This problem and solution are described at length here: https://stackoverflow.com/a/58632009/4543977
Example
For instance:
results in
Parameter 'rowData' implicitly has an 'any' type.ts
error andParameter 'type' implicitly has an 'any' type.
.Whereas the following does not have those issues:
A union isn't appropriate since we need
render
to be able to handle both versions of the function, not one or the other (so&
rather than|
). Note that it would be nice if the parameters were more constrained to be tied to one another so that ifrenderType==="row"
then TS would infer thatrowData
is of typeRowData
, but it seems that's not possible in TS based on the above linked Stack Overflow explanation and my own testing (without using the variable arguments approach, which doesn't seem appropriate). But at least this approach works and doesn't result in a TS error no matter what you do for yourrender
method.Related PRs
n/a
Impacted Areas in Application
Fixes the main
columns
prop when using a customrender
function, when using TypeScript.Additional Notes
n/a