Maaz Zindani

Loading the index…

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

TypeScript says a property is missing on Readonly<{}> in a React class

ErrorProperty 'value' does not exist on type 'Readonly<{}>'

Short answer

A React class component takes two type parameters: props and state. When you leave them out, both default to an empty object, so this.props and this.state are Readonly<{}> and have no 'value'. Declare an interface for each and pass them as Component<Props, State>.

  • Applies to TypeScript 5.x, React 19, @types/react 19
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Open the class declaration and count the type arguments after Component. If there are none, both props and state are empty objects.

    grep -rn "extends \(React\.\)\?\(Pure\)\?Component" src/
  2. 2Match what you see, then jump to its fix

    1. 1The error appears on this.state.valueThe state type is not passed to ComponentFix
    2. 2The error appears on this.props.valueThe props type is not passed to ComponentFix
    3. 3The component only holds state and rendersA function component is a simpler fitFix

What does the error look like?

  • Property 'value' does not exist on type 'Readonly<{}>'

Cause 1 of 3

The state type is not passed to Component

1Check

In the React typings the Component interface has defaults of {} for the props and the state, and the state is declared Readonly<S>. A class with no second type argument has state typed Readonly<{}>.

grep -rn "this.state\." src/

2Fix

Describe the state in an interface and pass it as the second type argument.

import { Component, type ChangeEvent } from 'react';

interface Props {}
interface State { value: string }

class Search extends Component<Props, State> {
  state: State = { value: '' };

  handleChange = (e: ChangeEvent<HTMLInputElement>) => {
    this.setState({ value: e.target.value });
  };

  render() {
    return <input value={this.state.value} onChange={this.handleChange} />;
  }
}

3Verify

The compiler accepts this.state.value.

npx tsc --noEmit

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 2 of 3

The props type is not passed to Component

1Check

Props are declared readonly props: Readonly<P>, and P defaults to {} too. A prop you pass in JSX is not known to the class until you declare it.

grep -rn "this.props\." src/

2Fix

Declare the props in an interface and pass it as the first type argument.

interface Props { value: string }

class Label extends Component<Props> {
  render() {
    return <span>{this.props.value}</span>;
  }
}

3Verify

The compiler accepts this.props.value, and a call without the prop now fails to compile.

npx tsc --noEmit

Confirmed by raw.githubusercontent.com

Did this fix it?

Cause 3 of 3

A function component is a simpler fit

1Check

The React TypeScript guide types props of a function component with an interface, and state with a hook, without class type parameters.

grep -rn "setState" src/

2Fix

Convert the class to a function component and type its props with an interface.

interface SearchProps { initial: string }

function Search({ initial }: SearchProps) {
  const [value, setValue] = useState(initial);
  return <input value={value} onChange={e => setValue(e.target.value)} />;
}

3Verify

The component compiles without any Readonly<{}> error.

npx tsc --noEmit

Confirmed by react.dev

Did this fix it?

How do I stop it coming back?

  • Write the Props and State interfaces before the class body.
  • Prefer function components for new code.
  • Turn on strict mode in tsconfig so missing types show up while writing.

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.