reactjs / reactjs/react.dev

useState of the Tuple type is not safe

Open
#5,940 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
11.8k
Forks
7.9k
Avg merge
1d 11h
Merged PRs (30d)
11

Description

I'm reading TS and noticed a vulnerability like UseState, as described in the documentation, we get a tuple of two elements.

The tuple in TS has a vulnerability because you can use array methods like pop, push on them. Which isn't safe because TS doesn't compute the type after that, it stays.

I suggest adding a readonly modifier to avoid these situations

  const state = useState();

  state.pop();

  const [value, setValue] = state;
  
  // setValue is undefined, but ts shows that the value is of type React.Dispatch<React.SetStateAction<undefined>

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The issue names no files, tests, or entry points in react.dev. First determine whether the requested readonly useState tuple belongs in the documentation or in React's TypeScript type definitions, then inspect the existing useState examples and type behavior. Done means the tuple cannot be mutated unsafely and the documented TypeScript behavior is verified.

Written by the indexing model from the issue text.

Assessment

Tech stack
react, typescript
Domain
documentation
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.