-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
[DataGrid] Narrow down GridColDef['type']
#11270
Conversation
Deploy preview: https://deploy-preview-11270--material-ui-x.netlify.app/ Updated pages: |
```tsx | ||
// π `type` is casted to `string` which is too wide | ||
const columns = [{ type: 'number', id: 'field' }]; | ||
<DataGrid columns={columns} />; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any ideas how we can improve this for TS users?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want autocomplete without this problem, you can use field: GridColType | string
instead of field: GridColType
.
But of course you are loosing the actual type checking.
Otherwise, people need to set type: 'number' as const
or columns: GridColDef
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just realized that the code snippet above was wrong.
What I wanted to show here is this error:
Otherwise, people need to set type: 'number' as const or columns: GridColDef.
Yes, this is fine, my concern is the error when the type
is inferred as string
.
I find this part confusing:
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"singleSelect"'.
Ideally, it should be:
Types of property 'type' are incompatible.
Type 'string' is not assignable to type "string" | "number" | "date" | "dateTime" | "boolean" | "singleSelect" | "actions".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh right,
AFAIK, it's due to the structure of your type
You are doing:
GridColDef = { type: 'number', ...rest } | { type: 'string', ...rest }
To have the correct autocompletion you would need to have something like:
GridColDef<T extends 'number' | 'string'> = { type: T' } & T extends 'string' ? { ...restString } : { ...restNumber }
But I suppose it's not a trivial change.
Other than that, they might be some hack you could do, but I don't know them :p
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I guess this is just the nature of unions in TS.
When columns
is not explicitly typed, TS requires it to be compatible with all the subtypes of the GridColDef
union.
Here's a minimal TS playground reproducing the issue: https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgOJWAEwEJwM4QDCA9gDYAiEMyA3gFDKPJgCeADhAFzIDkeYGEAHMeAbgZMYwCKUzd+goeIC+dOqEixEKdFgCCCMMGIg8JClVoTGrDtx6IjJvGOvIpMucgWglboRBgBk6m3AAUAJTIALwAfMhwICwA2gC6Kmq2OhiY5pTU0Wg5uAR5lgA+RfqGxqZlMOJ0mBAIpHBQKAjOYO4g4V2kAK4AtqFVuWT5UXEJSY1dpj0DI6YxtMzsXLw+wjwANO7Ssvb4mKc8yMriMCBhy6N4EUA
@@ -9,7 +9,7 @@ export const GRID_DETAIL_PANEL_TOGGLE_FIELD = '__detail_panel_toggle__'; | |||
export const GRID_DETAIL_PANEL_TOGGLE_COL_DEF: GridColDef = { | |||
...GRID_STRING_COL_DEF, | |||
field: GRID_DETAIL_PANEL_TOGGLE_FIELD, | |||
type: 'detailPanelToggle', | |||
type: 'string', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it make sense to have a type dedicated to all the interactive columns (all the toggles, the actions, etc...)
Because "string" is not super coherent IMHO
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They're extending the string
column definition anyway, it's just that they don't render the string as cell content π
Adding another type would make it appear in the autocomplete for the users, I would rather not add it until there are more reasons to do so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we've already debated on it, but I'll repeat anyway here for the record: I think we should add a "custom"
type for the columns that don't fit with any real datatype. It's more explicit than having our special columns be "string"
. I don't mind that it appears on autocomplete, we can add a note about it on our types table, some users may also have custom columns that don't represent a datatype.
We can also assign it more sensible default behaviors, such as ignoring it for filtering & quickfiltering purposes, and that can improve performance a bit.
edit: btw if we extend GRID_STRING_COL_DEF
, the type: 'string'
here is redundant with the same field declared in GRID_STRING_COL_DEF
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we've already debated on it, but I'll repeat anyway here for the record: I think we should add a "custom" type for the columns that don't fit with any real datatype. It's more explicit than having our special columns be "string"
Yes that's basically what I had in mind
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've added the custom
column type, but did not create GRID_CUSTOM_COL_DEF
:
- Every custom column type has a different set of flags and there's no general rule for that: some columns have the column menu, some columns have row grouping disabled, etc. It's better to have all the flags explicitly provided in each column definition.
- Not all custom columns extend the string column definition - see
mui-x/packages/grid/x-data-grid/src/colDef/gridCheckboxSelectionColDef.tsx
Lines 10 to 12 in 8dff180
export const GRID_CHECKBOX_SELECTION_COL_DEF: GridColDef = { ...GRID_BOOLEAN_COL_DEF, field: GRID_CHECKBOX_SELECTION_FIELD,
What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, that makes sense π
```tsx | ||
// π `type` is casted to `string` which is too wide | ||
const columns = [{ type: 'number', id: 'field' }]; | ||
<DataGrid columns={columns} />; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want autocomplete without this problem, you can use field: GridColType | string
instead of field: GridColType
.
But of course you are loosing the actual type checking.
Otherwise, people need to set type: 'number' as const
or columns: GridColDef
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Closes #9735
Changelog
The
GridColDef['type']
has been narrowed down to only accept the built-in column types.TypeScript users need to use the
GridColDef
interface when defining columns: