admin管理员组

文章数量:1313769

If I use typescript in my project, would that make the usage of prop-types in React obsolete? With prop-types I would have to go through the grunt of defining types but with typescript, this step would be negated. AM I correct in my thinking?

If I use typescript in my project, would that make the usage of prop-types in React obsolete? With prop-types I would have to go through the grunt of defining types but with typescript, this step would be negated. AM I correct in my thinking?

Share Improve this question asked Sep 20, 2017 at 14:12 Jahil KhalfeJahil Khalfe 3003 silver badges12 bronze badges 2
  • 2 You might be interested in stackoverflow./questions/36065185/react-proptypes-vs-flow – Pavlo Commented Sep 20, 2017 at 14:26
  • 4 You still need to "define types" with TypeScript, e.g. use an interface to specify the types of your ponent props. The difference is that when you do the wrong thing, you should end up with a pile error (good) rather than a runtime error (bad). I don't think you need both. – Tom Fenech Commented Sep 20, 2017 at 14:26
Add a ment  | 

2 Answers 2

Reset to default 7

Sounds about right. When you use TypeScript you can define the props via an interface.

interface ButtonProps {
  text: string,
  shadow?: boolean
}

const Button: React.FunctionComponent<ButtonProps> = props => {
  return ( /* [...] */ );
};

See here

I think you don't need prop types if you use typewriting. Typescript is a good solution for strong typing, but for this you need to write more code. Props Types provide you efficient typization and small code blocks for you classes or functional ponents

本文标签: javascriptReact Prop Types vs TypescriptStack Overflow