Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Next.js and React

TypeScript rejects MUI Data Grid columns that set a type

ErrorA TypeScript error on the columns prop of DataGrid that points at a column's type property

Short answer

A columns array declared without a type is inferred with type as a plain string, which the Data Grid's column type union does not accept. Annotate the array as GridColDef[] so each type value is checked as a literal. Type renderCell params with GridRenderCellParams when the cell callback errors.

  • Applies to MUI X Data Grid 7, TypeScript 5, React 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Find where the columns array is declared. If it has no type annotation and sits outside the component, the next cause applies.

    grep -rn "columns\b" src/ | grep -v GridColDef
  2. 2Match what you see, then jump to its fix

    1. 1The error mentions a property named type inside a columns item.The columns array has no GridColDef type, so type is widened to stringFix
    2. 2The error is inside renderCell or another callback, not on type.A render callback has untyped paramsFix

What does the error look like?

  • A TypeScript error on the columns prop of DataGrid that points at a column's type property
  • A TypeScript error inside a column's renderCell callback

Cause 1 of 2

The columns array has no GridColDef type, so type is widened to string

1Check

TypeScript infers an object's string properties as string, not as the literal, because the property might be reassigned. That is the same behaviour as the handbook's method example.

const columns = [
  { field: "age", type: "number" },
];

2Fix

Annotate the array with GridColDef[] so the literal is checked against the allowed values.

import { DataGrid, GridColDef } from "@mui/x-data-grid";

const columns: GridColDef[] = [
  { field: "age", headerName: "Age", type: "number" },
];

3Verify

Run the type check; the error on columns should be gone.

npx tsc --noEmit

Confirmed by typescriptlang.org

Did this fix it?

Cause 2 of 2

A render callback has untyped params

1Check

The Data Grid docs type renderCell params with GridRenderCellParams, where the second generic is the value type of the cell.

grep -rn "renderCell" src/

2Fix

Type the params. For a date column the docs use any for the row and Date for the value.

import { GridColDef, GridRenderCellParams } from "@mui/x-data-grid";

const columns: GridColDef[] = [
  {
    field: "createdAt",
    type: "date",
    renderCell: (params: GridRenderCellParams<any, Date>) =>
      params.value?.toLocaleDateString(),
  },
];

3Verify

Run the type check again.

npx tsc --noEmit

Confirmed by mui.com

Did this fix it?

How do I stop it coming back?

  • Annotate every shared columns array as GridColDef[] when you define it.
  • Keep the columns array outside the component or memoized; the docs ask for a stable reference between renders.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.