diff --git a/types/create-react-class/create-react-class-tests.ts b/types/create-react-class/create-react-class-tests.ts index f8d8b132c7..3d7536d722 100644 --- a/types/create-react-class/create-react-class-tests.ts +++ b/types/create-react-class/create-react-class-tests.ts @@ -56,7 +56,7 @@ const ClassicComponent: React.ClassicComponentClass = createReactClass, nextProps, nextState) { const newFoo: string = nextProps.foo; const newBar: number = nextState.bar; - return newFoo !== this.props.foo && newBar !== this.state!.bar; + return newFoo !== this.props.foo && newBar !== this.state.bar; }, statics: { test: 1 diff --git a/types/react/index.d.ts b/types/react/index.d.ts index 42ef0401e1..332d3ac640 100644 --- a/types/react/index.d.ts +++ b/types/react/index.d.ts @@ -306,7 +306,7 @@ declare namespace React { // In the future, if we can define its call signature conditionally // on the existence of `children` in `P`, then we should remove this. readonly props: Readonly<{ children?: ReactNode }> & Readonly

; - state: null | Readonly; + state: Readonly; /** * @deprecated * https://reactjs.org/docs/legacy-context.html diff --git a/types/react/test/index.ts b/types/react/test/index.ts index 3a04a0ab79..3b7e5193fd 100644 --- a/types/react/test/index.ts +++ b/types/react/test/index.ts @@ -59,14 +59,7 @@ declare const container: Element; inputValue: string; seconds: number; } - /** - * This is a "pre EcmaScript class property era" style of setting state within constructor. - * This occurs also if you need to provide som logic before mounting your component. - * To mitigate this error you need to provide state property definition upfront. - */ class SettingStateFromCtorComponent extends React.Component { - // uncomenting this fixes the error :) - // state: State; constructor(props: Props) { super(props); // $ExpectError @@ -78,16 +71,17 @@ declare const container: Element; } class BadlyInitializedState extends React.Component { - // ExpectError -> this throws error on TS 2.6 + // $ExpectError -> this throws error on TS 2.6 uncomment once TS requirement is TS >= 2.7 // state = { // secondz: 0, // inputValuez: 'hello' // }; + render() { return null; } } class BetterPropsAndStateChecksComponent extends React.Component { render() { return null; } componentDidMount() { - // $ExpectError + // $ExpectError -> this will be true in next BC release where state is gonna be `null | Readonly` console.log(this.state.inputValue); } mutateState() {