mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2026-08-11 12:30:18 +00:00
Merge branch 'cleanup-react-readme' of https://github.com/ericanderson/DefinitelyTyped into ericanderson-cleanup-react-readme
This commit is contained in:
@@ -1,31 +1,5 @@
|
||||
## Known Problems & Workarounds
|
||||
|
||||
### **The type of `setState` is overly strict prior to TS 2.1.5**
|
||||
Starting with TypeScript 2.1, its more correct than it used to be, with a caveat: optional parameters on state interfaces are no longer valid. This issue is corrected in 2.1.5 (and beyond) and thus `Pick<S, K>` becomes ideal.
|
||||
|
||||
```ts
|
||||
interface FooState {
|
||||
bar: string;
|
||||
foo?: string;
|
||||
}
|
||||
|
||||
const defaultFooState: FooState = {
|
||||
bar: "Hi",
|
||||
foo: undefined,
|
||||
};
|
||||
|
||||
class Foo extends React.Component<{}, FooState> {
|
||||
public doStuff() {
|
||||
this.setState(defaultFooState);
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
Prior to 2.1.5, this code will produce an error. The other way the types could be written (using `Partial<>` instead of `Pick<>`) would allow for you to set `undefined` to a parameter that is not allowed
|
||||
to be `undefined`, which could lead to bugs. Users who want to keep optional state parameters should continue to use the hack of `setState({...} as any);` in versions prior to 2.1.5.
|
||||
|
||||
(This caveat should be dropped after no later than March 2017, maybe sooner.)
|
||||
|
||||
### **The type of `cloneElement` is incorrect.**
|
||||
This is similar to the `setState` problem, in that `cloneElement(element, props)` should should accept a `props` object with a subset of the properties on `element.props`. There is an additional complication, however—React attributes, such as `key` and `ref`, should also be accepted in `props`, but should not exist on `element.props`. The "correct" way to model this, then, is with
|
||||
```ts
|
||||
|
||||
Vendored
+1
-1
@@ -1,6 +1,6 @@
|
||||
// Type definitions for React v15.0
|
||||
// Project: http://facebook.github.io/react/
|
||||
// Definitions by: Asana <https://asana.com>, AssureSign <http://www.assuresign.com>, Microsoft <https://microsoft.com>, John Reilly <https://github.com/johnnyreilly/>, Benoit Benezech <https://github.com/bbenezech>, Patricio Zavolinsky <https://github.com/pzavolinsky>, Digiguru <https://github.com/digiguru>
|
||||
// Definitions by: Asana <https://asana.com>, AssureSign <http://www.assuresign.com>, Microsoft <https://microsoft.com>, John Reilly <https://github.com/johnnyreilly/>, Benoit Benezech <https://github.com/bbenezech>, Patricio Zavolinsky <https://github.com/pzavolinsky>, Digiguru <https://github.com/digiguru>, Eric Anderson <https://github.com/ericanderson>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.2
|
||||
|
||||
|
||||
Reference in New Issue
Block a user