setState typescript signature is too restrictive.
See original GitHub issueThe typescript signature of setState
is too restrictive, since it assumes it’s parameter must be exactly the type of the component’s StateType
type parameter.
TypeScript has a builtin type Partial<T>
that has the same type as T
, but with all properties forced to be optional defined as:
type Partial<T> = {
[P in keyof T]?: T[P];
}
And define the signature of setState
to be setState(state: Partial<StateType>, opts?: any)
.
I’ll PR this in when I have a moment!
Issue Analytics
- State:
- Created 7 years ago
- Reactions:4
- Comments:7 (3 by maintainers)
Top Results From Across the Web
TypeScript Custom useStateIfMounted React Hook - Not all ...
The hook is fine, but the caller gets the error when trying to call setState(). How should I type the useCallback() so I...
Read more >React state meets Typescript's Union type - Level Up Coding
If you're not familiar with it, the Union type is the basic Typescript tool we use to describe a value that is “one...
Read more >Useful Patterns by Use Case - React TypeScript Cheatsheets
When a component can render differently based on their props, it's similar to how a function can be overloaded to have multiple call...
Read more >Freshness - TypeScript Deep Dive - Gitbook
TypeScript provides a concept of Freshness (also called strict object literal checking) to make it easier to type check object literals that would...
Read more >React and TypeScript: `useState` Hook - Educative.io
In order to fix this issue, we need to pass a type argument to setState . We treat pill as text in JSX,...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Also have the same problem.
If I describe my State as strong set of options:
I can’t set only one option in set:
But if I mark options with
?
:I have another problem – TypeScript think that options can be undefined (but I set them in constructor):
So wating for
After TS 2.1.5 comes out, we should be able to merge the react changes.