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

JSX/TSX: Proper way to use State types #3868

Closed
mikemorton opened this issue Jul 15, 2015 · 2 comments
Closed

JSX/TSX: Proper way to use State types #3868

mikemorton opened this issue Jul 15, 2015 · 2 comments
Labels
Duplicate An existing issue was already created

Comments

@mikemorton
Copy link

Consider this example:

class ToggleState {
    open:boolean
}
class AnimationDemo extends React.Component<{}, ToggleState>
{
  state:ToggleState;
  constructor () {
    super()
    this.state = {open : false, fdsa: 'asdf'};
  }
  getInitialState():ToggleState {
    return {open: false};
  }
  handleMouseDown() {
    this.setState({open: !this.state.open, fdsa: 'asdf'});
  }
  render() {
    return (
      <div>something</div>
    );
  }
};

There doesn't seem to be any guarding against assigning bad values in the state property or passing them into setState.

Note that if I attempt to return something invalid from getInitialState it fails correctly, however React doesn't respect getInitialState unless the component is created with React.createClass(). (See the example here: https://facebook.github.io/react/blog/2015/01/27/react-v0.13.0-beta-1.html#es6-classes)

So is it possible for me ensure that the state property and setState are guarded correctly?

@danquirk
Copy link
Member

tagging @RyanCavanaugh

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Jul 15, 2015
@RyanCavanaugh
Copy link
Member

This is just a manifestation of the general problem of surplus properties in object literals (#391). Pull request #3823 (in progress) will cause the fsda properties to error.

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

3 participants