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

React DOM (e.g <div />) not type checked #3773

Open
aldendaniels opened this issue Apr 19, 2017 · 6 comments
Open

React DOM (e.g <div />) not type checked #3773

aldendaniels opened this issue Apr 19, 2017 · 6 comments
Labels
bug Library definitions Issues or pull requests about core library definitions react

Comments

@aldendaniels
Copy link

Flow allows this without complaint:

import React from 'react';

class Test extends React.Component {
  render() {
    return <div style={1} bogus="allowed">Test</div>;
  }
}

Try

Should we add a disclaimer to this effect to the Flow + React until this level of type-safety is supported?

@mwalkerwells
Copy link
Contributor

You mean add a disclaimer that the default return type of render is ?React$Element<any>?
https://github.com/facebook/flow/blob/master/lib/react.js#L32

Not sure how useful that would be. If you need more type safety, you can easily add it:

import React from 'react';

class Test extends React.Component {
	render(): React$Element<{ style: number, bogus: string}> {
    	return <div style={1} bogus="allowed">Test</div>;
    }
}

@aldendaniels
Copy link
Author

@mwalkerwells Yeah, that's what I mean - there are not type defs for the React primitives and it's not clear how to provide any. By contrast, Typescript documents how to provide your own types for JSX intrinsics.

Regardless, I'd (incorrectly) expected types for these - and thought a disclaimer to this effect might be helpful to others. Feel free to close if you feel otherwise. Otherwise I'm happy to submit a PR.

I expect that the simplest way to get strongly typed primitives is to write wrapping components for the various DOM primitives that are strongly typed - in the spirit of react-primitives, but sticking to DOM spec - this is something I've been looking into for use in my own projects.

@Peeja
Copy link

Peeja commented Jan 30, 2018

We would find it extremely useful to type check props on intrinsics.

That appears to be what the issue title is about, but not what @mwalkerwells's clarification points to, unless I'm misunderstanding. As a simpler example, I'd expect this to fail:

<div style={1} bogus="allowed">Test</div>;

We sometimes spread props into intrinsic components, and it would be valuable to know when we've spread something that includes props React doesn't expect there, or of the wrong type.

@Peeja
Copy link

Peeja commented Jan 30, 2018

Also, it would be extra nice to have, for instance, <button>'s onClick prop automatically typed as (SyntheticEvent<HTMLButtonElement>) => void. We've passed handlers expecting to be attached to the wrong kind of DOM element before, and this would catch that.

@motiz88
Copy link
Contributor

motiz88 commented Aug 6, 2018

I'd like to claim this issue please 😃 Will start working on it later this week.

@cprodescu
Copy link

@motiz88 thank you for taking this on!

@SamChou19815 SamChou19815 added the Library definitions Issues or pull requests about core library definitions label Aug 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Library definitions Issues or pull requests about core library definitions react
Projects
None yet
Development

Successfully merging a pull request may close this issue.

7 participants