Maaz Zindani

Loading the index…

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

TypeScript: Cannot assign to current because it is a read-only property

ErrorCannot assign to 'current' because it is a read-only property

Short answer

With React 18 types, useRef(null) returns a ref whose current cannot be assigned. That type exists for DOM refs, not for mutable values. Add | null to the generic, give a real initial value, or upgrade to React 19 types, where every ref is mutable.

  • Applies to TypeScript 5, React 18 types, React 19 types
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Check which @types/react version is installed: the behaviour differs between 18 and 19.

    npm ls @types/react react
  2. 2Match what you see, then jump to its fix

    1. 1ref.current = value fails for a ref created as useRef<number>(null).useRef<T>(null) returns a read-only RefObject under the React 18 typesFix
    2. 2npm ls shows @types/react 18, and the same code compiles in a React 19 project.Your project is on the React 18 types, and the React 19 types would fix itFix

What does the error look like?

  • Cannot assign to 'current' because it is a read-only property

Cause 1 of 2

useRef<T>(null) returns a read-only RefObject under the React 18 types

1Check

In the 18 types, an argument of type T | null returns RefObject<T>, whose current is readonly. A ref given as the ref prop of an element is meant to be written by React, not by you.

grep -rn "useRef<" src/

2Fix

Include | null in the generic argument so the ref is directly mutable. Or pass an initial value of type T.

const timer = useRef<number | null>(null);
timer.current = 1;

3Verify

Run the type check.

npx tsc --noEmit

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 2

Your project is on the React 18 types, and the React 19 types would fix it

1Check

In the React 19 types all refs are mutable and a single RefObject type is returned by useRef. useRef also now requires an argument.

npm ls @types/react

2Fix

Upgrade React and its types together, following the upgrade guide.

npm install react@19 react-dom@19 && npm install --save-dev @types/react@19 @types/react-dom@19

3Verify

Run the type check.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Use useRef<HTMLInputElement>(null) for DOM refs and let React write to current.
  • For values you write yourself, include | null (or a real initial value) in the type.
  • Upgrade React and @types/react in the same commit.

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

Web apps

TypeScript rejects MUI Data Grid columns that set a type

A TypeScript error inside a column's renderCell callback

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.

2 causes, each with a fix

Next step

Want this handled for good?

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