In React, the Component definition looks something like this:
class Component {
state:S;
setState(state:S):void;
}
And you
The react.d.ts definition is indeed wrong, and cannot be fixed until Partial Types are supported.
The problem is that setState takes a partial state, which is a different type from normal state. You can solve this by manually specifying both types:
interface MyState {
name: string;
age: number;
}
interface PartialMyState {
name?: string;
age?: number;
}
And manually declaring MyComponent without extending the provided React Component class:
class MyComponent {
state: MyState;
setState(state:PartialMyState): void;
//...
}
Which means you'll have to duplicate these function definitions for every subclass of Component in your code. You may be able to avoid this by defining a correct Component class generalized by an additional type of partial state:
class CorrectComponent { // generalized over both state and partial state
state:S;
setState(state:P):void;
//...
}
class MyComponent extends CorrectComponent { }
You'll still have to write a partial version for every state type you have.
Alternatively, you can make setState non-typesafe by changing its argument's type to Object.