I've been building my app so far with PropTypes, but I realised that the components themselves already have Typescript prop checkers _(is this what we refer to them as? 🤔 )_ in place. For my example, lets look at the controlled input props,
/** Interface for a controlled input. */
export interface IControlledProps {
/** Initial value of the input, for uncontrolled usage. */
defaultValue?: string;
/** Change event handler. Use `event.target.value` for new value. */
onChange?: React.FormEventHandler<HTMLElement>;
/** Form value of the input, for controlled usage. */
value?: string;
}
It seems to me that I'm doing double work to define PropTypes that __can__ already be checked by IControlledProps
SignUpForm.propTypes = {
email: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
}
```jsx
const SignUpForm = props => {
return (
onChange={props.onChange}
name="email"
type="text"
placeholder="Enter e-mail address"
large="true"
/>
)
}
____
### What I think can be done? 🤷♂️
I could probably do something like:
```tsx
import { IControlledProps } from "@blueprintjs/core/src/common/props.ts"
const SignUpForm = (props:IControlledProps) => {
return (
<InputGroup
value={props.email}
onChange={props.onChange}
name="email"
type="text"
placeholder="Enter e-mail address"
large="true"
/>
)
}
As such, my questions are:
props.ts the proper way to do this if I want to make use of TypeScript?@havesomeleeway great question! you can definitely rely on our types for prop validation ✅
you should be able to import all these interfaces from the package itself, rather than reaching into submodules -- we're very deliberate about exporting public components _and_ their props interfaces _for exactly this purpose!_
import { IControlledProps } from "@blueprintjs/core";
the primary difference is that all typescript checking happens at _compile time_ while prop-types run at _run time_ (in the browser). this means that you can get immediate editor feedback from typescript but _not from prop-types_. prop-types requires running additional code in the browser, although in practice this is negligible.
here are some hopefully helpeful resources from a quick google: https://dbushell.com/2017/04/19/typescript-instead-of-react-proptypes/, https://www.reddit.com/r/reactjs/comments/8zzb4p/why_do_you_use_typescript_if_proptypes_is/
Thanks for the response as usual. In that case, how should I best proceed from here? I've mainly got components and containers set up for now. Should I refactor my app with a react/typescript starter as indicated by this?
https://github.com/Microsoft/TypeScript-React-Starter/blob/master/README.md
I've never used TypeScript in my projects before so i'm unsure on how to progress from here, especially when I already have some config files setup.
@havesomeleeway yes you'll need to integrate typescript into your project in order to leverage its benefits. that microsoft starter is a great resource, and it's also worth noting that create-react-app supports typescript directly now: https://facebook.github.io/create-react-app/docs/adding-typescript.
for further learning, there are plenty of amazing typescript resources just a google search away, not least of which is their own documentation.