Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Require state initialization #152

Closed
alecmev opened this issue Apr 8, 2018 · 2 comments
Closed

Require state initialization #152

alecmev opened this issue Apr 8, 2018 · 2 comments

Comments

@alecmev
Copy link

alecmev commented Apr 8, 2018

Right now, the following is possible:

class A extends Component<{}, { foo: string }> {
  render() {
    return <div>{this.state.foo}</div>; // Runtime error, `state` is null
  }
}

I haven't done a thorough research, but doesn't seem like there's a way to address this through typings. So maybe a rule could be added, requiring state initialization, at least when it's explicitly typed:

class A extends Component<{}, { foo: string }> {
  state = null // Compile-time error, can't be null

  render() {
    return <div>{this.state.foo}</div>;
  }
}

Or require the state type to be nullable: This doesn't work with current typings:

class A extends Component<{}, { foo: string } | null> {
  render() {
    return <div>{this.state.foo}</div>; // Compile-time error, `state` may be null
  }
}

Thoughts?

@johnwiseheart
Copy link

I think this is a good idea - definitely see this issue pop up now and again and it would be good to check against it. The TSLint team doesn't have a lot of bandwidth to write new rules at the moment, but will accept pull requests for this.

@adidahiya
Copy link
Contributor

Closing due to deprecation timeline, see #210

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants