Support setting React PropTypes using TypeScript types
I'm not sure how practical this would be, but since TypeScript is now supporting React's JSX syntax, it could perhaps provide more syntactic sugar for a common React pattern? Namely, that of setting PropTypes, requirements for the structure of property arguments for React components. Sounds like TypeScript's syntax should be able to remove a lot of the boilerplate there.
PropTypes is used for type checking in developement mode. TypeScript already does type checking at compile time, i don't think that you need PropTypes if you use TypeScript.
Hmm, that's true, I guess :)
Although, then again (sorry for the change-of-mind), props aren't passed as an argument to the component, right? Thus, there isn't really a place in the component to define what its given properties should look like...
//cc: @RyanCavanaugh
Your props should be defined in an interface which is passed as a type parameter to React.Component<P, S> when you define a component. I don't see any real use for React's runtime type checking of props when using TypeScript.
Ah, that sounds good - sorry, I only have experience with React's non-ES6 syntax since I'm using mixins, so if you can define the interface for your props using the class syntax, that should cover it :)
no worries :)
as some additional advice, you'll want your props to extend React.Props to automatically get things like this.props.children:
interface IFooProps extends React.Props<Foo> {
// ...
}
interface IFooState {
// ...
}
class Foo extends React.Component<IFooProps, IFooState> {
// ...
}
I might be missing something, but without proptypes support, how do I write the equivalent of
var MyComponent = React.createClass({
propTypes: {
children: React.PropTypes.element.isRequired
},
render: function() {
return (
<div>
{this.props.children} // This must be exactly one element or it will throw.
</div>
);
}
});
interface IMyComponentProps extends React.Props<MyComponent> {
}
interface IFooState {
}
class MyComponent extends React.Component<IMyComponentProps, IMyComponentState> {
render() {
return (
<div>
{this.props.children}
</div>
);
}
}
React.render(
<MyComponent>Body</MyComponent>,
body
);
makes the children optional, but
interface IMyComponentProps extends React.Props<MyComponent> {
children: React.ReactChild | {} | any[] | boolean;
}
yields the compilation error error TS2324: Property 'children' is missing in type 'IMyComponentProps'.
There's currently no type system enforcement of child elements.
We looked into this but a) there doesn't seem to be a lot of code that has meaningful constraints on its children and b) we would have had to encode a ton of very React-specific semantics into the type system, which we weren't comfortable doing at this point.
What about the case when building a react library to be used by others. Others may not be using typescript. It would be nice to have a way to generate PropTypes when creating the distributed lib so other developers will be able to get PropType validation during development when using the library. React then strips theses away when building production.
as the @TheSharpieOne said, this is stopping us from porting main libraries to typescript since we have multiple consumers which in turn stops us from migrating the consumers to typescript (since it feels to be rather error prone to manually add both and keep them in sync)
what @TheSharpieOne and @TobiasBales said. We kinda need support for it.
I came up with an idea, that satisfies both typescript and react runtime prop validation, but it has flaws.
Say we have a component named Icon, it has two props name and size.
const iconPropTypes = {
name: pt.string.isRequired as any as string,
size: pt.number as any as number
}
type TProps = typeof iconPropTypes
class Icon extends Component<TProps, {}> {
...component content
}
Icon['propTypes'] = iconPropTypes
The problem is obvious in typescript size is required when it's not supposed to, and I have not found a solution to work this around.
The effective difference between React PropTypes and TypeScript types is that, while TypeScript type annotations can describe what JSON schema the server should deliver, React detects what is actually delivered. If the wrong data is delivered to the client, React PropTypes will give us good error messages while the transpired TypeScript will die without explanation.
While the two systems clearly overlap, this feature can be justified in serving to automatically check server data at runtime.
Also, note that React PropTypes are fairly limited ("this property must be an object") while TypeScript could generate a more complete custom validator ("this property must be an object with an integer id and a string name")
React PropTypes can describe objects in more detail, e.g. PropTypes.shape
@TobiasBales - thanks for that; you're right. PropTypes.shape is darn close, conceptually, to a straightfoward bit of runtime typechecking.
I wonder whether it is possible to write a PropTypes object for each type using the TypeScript compiler API and type checker. Notice how the example lets you find type and and member info, build a complex object describing the type, and output structured content. You could do something like;
- use the compiler API to get type and member info
- use it to generate a validation routine per type, and the PropTypes object for each component,
- call it from your main TypeScript files.
Not as easy as full language support, and it's just an idea, but possibly something you could get working with a bit of hacking about.
Hey all, would prop-types-ts get you close to what you want? Instead of creating proptypes out of TypeScript types, it allows you to make TypeScript types from proptypes.
Some libraries written by TS, compiled to JavaScript and published on npm registry, like https://github.com/ant-design/ant-design-mobile, is there a way to use its propTypes in JavaScript?
@omril1 what ?
This might help someone, if you want to make a props to be required and force TypeScript to throw an error about it you can accomplish it by using an interface for the Generic Props of the React.Component (I know this is already explain in the thread, but no one pointed out the optional operator that It's explained later in this comment).
interface Props {
name: string
}
export class Person extends React.Component<Props, {}>
If you later try <Person/> without providing the name attribute it will throw a compile error, the counterpart of this would be to use the optional operator ?
interface Props {
name?: string
}
With the above interface TypeScript won't complain whenever we do <Person/>, because the name is optional according to the use of the optional operator in the interface attribute definition.