Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is PropTypes become obsolete in Typescript React? [duplicate]

I'm recently migrate to Typescript-react from javascript-based react. So this is new for me.

I notice that Typescript has its own props validation method using interface. My questions are:

  1. Does PropTypes (from prop-types package) still necessary to be used ?
  2. How to make "isRequired" for a prop? I want to make it error if a prop doesn't defined.
  3. How to make a default value for a prop? I want to use that default value if a prop wasn't defined.

Here is my simple code. This code below works:

import * as React from "react";

// [Question 1] Create an object PropTypes, is this still necessary ?
import * as PropTypes from "prop-types";

// [Question 2] how to use isRequired for this prop ?
interface Props {
  text: String;
  active: Boolean;
}

const NavBarMenu = (props: Props) => {
  // [Question 3] How to make a default value for prop?
  return (
    <li className="nav-item">
      <a className={props.active ? "nav-link active" : "nav-link"} data-toggle="tab" href="#" role="tab">
        {props.text}
      </a>
    </li>
  );
};

NavBarMenu.propTypes = {
  text: PropTypes.string.isRequired,
  active: PropTypes.bool
};

export default NavBarMenu;
like image 940
DennyHiu Avatar asked May 09 '19 09:05

DennyHiu


People also ask

Do you need PropTypes with TypeScript React?

Typescript and PropTypes serve different purposes. Typescript validates types at compile time, whereas PropTypes are checked at runtime. Typescript is useful when you are writing code: it will warn you if you pass an argument of the wrong type to your React components, give you autocomplete for function calls, etc.

Is PropTypes deprecated?

PropTypes is deprecated since React 15.5.

Is PropTypes required in TypeScript?

Props are required in TypeScript In the prop-types package, all props are optional by default. To make a prop required, you will have to use .

How are PropTypes used in functional components?

Firstly, npm install / yarn add the prop-types package if you haven't already. Then, add your propTypes (and defaultProps too if required) after the stateless functional component has been defined, before you export it.


3 Answers

  1. Does PropTypes (from prop-types package) still necessary to be used ?

Mostly no. If the props passed are not of the correct type, TypeScript will throw a compilation error. (which is better because PropTypes throws a runtime error, when statically typed like in TS you can't even run the code since it isn't compiled. So basically you can't push to production, hence no bugs. That's the whole point of using a statically-typed language)

  1. How to make "isRequired" for a prop? I want to make it error if a prop doesn't defined.

Properties are by default required when written in interfaces (make sure you have strict: true in tsconfig). You want active to be optional right? So the interface would look like following:

interface Props {
  text: string;
  active?: boolean;
}

(use string instead String same for all other primitive types, string and String are not same)

  1. How to make a default value for a prop? I want to use that default value if a prop wasn't defined.

In case of a functional component, it won't be different than how you define default values for parameters in any other normal function. Example:

const NavBarMenu = ({ text, active = false }: Props) => { ... }

Also TypeScript is not a full replacement for PropTypes, in most cases TS will be sufficient (and better).

like image 123
Devansh J Avatar answered Oct 31 '22 09:10

Devansh J


Typescript

  • Typescript validates the types at the time of compilation.
  • Typescript types can be more complex due to generic types provided by Typescript
  • Useful when you are sure that type of the data you are supplying to the component's props will not be changed
  • To set default value you can use static defaultProps = {...props} on the component class

PropTypes

  • React check the ProTypes at the runtime
  • Does not support generic type checking
  • Useful when your component needs strict type checking at the runtime, such as data from ajax request where you can't be sure about the type of the data.
like image 32
Shridhar Sharma Avatar answered Oct 31 '22 09:10

Shridhar Sharma


If you are using TypeScript in your project you don't need PropTypes package, you achieve the same with only using interfaces.

You can define required/not required prop with the interface, for example:

interface MyProp {
 text: String; // This value is required
 active?: Boolean; // By adding ? the value becomes optional
}

In order to make a prop having a default value, you can do that with spreading the prop value on the component, something like:

const NavBarMenu = ({text, active = false}: Props) => {
  // the value of active will default for false
  return (
    <li className="nav-item">
      <a className={active ? "nav-link active" : "nav-link"} data-toggle="tab" href="#" role="tab">
        {text}
      </a>
    </li>
  );
}; 
like image 22
Marco Daniel Avatar answered Oct 31 '22 09:10

Marco Daniel