question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Type safety violation by undefined default value for component state.

See original GitHub issue

Unless a value is explicitly assigned in the constructor, the property state in React.Component<P, S, SS> has the default value undefined, regardless of the specific type passed to the relevant type parameter S. This leads to a violation of type safety, as shown by the following code example. Furthermore, the value undefined is not actually assignable to the empty object literal type {}, the default value for the type parameter S.

React version: 17.0.1

Minimal code example:

import React from 'react';
class MyComponent extends React.Component<{}, {value: string}> {
  constructor(props: {}) {
    super(props);
    console.log(this.state.value);
  }
}
new MyComponent({}); // Compiles OK, TypeError at runtime.

The current behavior

The minimal code example compiles without error but raises TypeError at runtime, with error message "Cannot read property 'value' of undefined".

The expected behavior

The minimal code example should (ideally) raise error at compile time.

Additional information

For the sake of completeness, the example was run using react-scripts and the following tsconfig.json (but the issue is not related to react-scripts):

{
  "compilerOptions": {
    "target": "esnext",
    "lib": [
      "dom",
      "dom.iterable",
      "esnext"
    ],
    "noImplicitReturns": true,
    "noImplicitThis": true,
    "allowJs": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "strict": true,
    "forceConsistentCasingInFileNames": true,
    "noFallthroughCasesInSwitch": true,
    "module": "esnext",
    "moduleResolution": "node",
    "resolveJsonModule": true,
    "isolatedModules": true,
    "noEmit": true,
    "jsx": "react"
  },
  "include": [
    "src"
  ]
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
bvaughncommented, Nov 17, 2020

Yup! I think I initially misunderstood what you were suggesting, but given the clarification above– this sounds like something that should be discussed in DefinitelyTyped 😄 Good luck!

1reaction
bvaughncommented, Nov 16, 2020

It seems like this issue is requesting new functionality from the TypeScript compiler. The React repro does not have any relationship to the TypeScript compiler. You might want to try filing an issue in that repo: https://github.com/microsoft/TypeScript

Read more comments on GitHub >

github_iconTop Results From Across the Web

Getting a child's default props to set the state in the parent in ...
If I attempt to get around this by setting up a default initial state to an empty object (each with nested empty objects)...
Read more >
Testing a simple component with React Testing Library
In this post join me as I use React Testing Library to test a simple React component. Apart from applying the testing library...
Read more >
Understanding Dependencies in useEffect | Bits and Pieces
A deep dive into the dependencies array in React.useEffect(). What causes stale values from previous renders in React.
Read more >
cannot be used as a jsx component. its return type 'element
It need to have a default value in your switch case. If nothing match, it will be undefined . import React from "react";...
Read more >
Troubleshooting common React Native bugs - LogRocket Blog
Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found