9

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;
DennyHiu
  • 4,861
  • 8
  • 48
  • 80
  • 2
    Please try to focus on a single issue per question. It keeps the site nice and searchable. Questions with "subquestions" will mean that there's content that can't be easily found by other users. I've marked as duplicate on the main issue. Issues 2&3 should form separate questions. – spender May 09 '19 at 09:55
  • https://meta.stackexchange.com/questions/222735/can-i-ask-only-one-question-per-post – spender May 09 '19 at 10:04
  • @spender My bad....sorry. I think all the answer here are great, I also read the link that you posted, and found them useful. Thank you for all of your efforts. It's clear for me now. – DennyHiu May 09 '19 at 12:19

3 Answers3

12
  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).

Devansh J
  • 4,006
  • 11
  • 23
  • 2
    How can TypeScript tell you that the data coming back from your async request is not going to break your component? Prop-types does this for you as it does run-time checks. – JoeTidee Dec 04 '20 at 12:05
  • @JoeTidee I'm well aware hence I wrote "TypeScript is not a full replacement for PropTypes". Just didn't mention it explicitly as you did to keep the answer short and not over-nuanced. – Devansh J Dec 04 '20 at 12:09
3

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.
Shridhar Sharma
  • 2,337
  • 1
  • 9
  • 13
2

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>
  );
}; 
Marco Daniel
  • 5,467
  • 5
  • 28
  • 36
  • If you have a components getting async data, populating a redux store, which then updates other components, how can TypeScript check for this? – JoeTidee Mar 11 '21 at 18:20
  • @JoeTidee this question is marked as duplicate, and the proper answer can be found here: https://stackoverflow.com/a/43187969/3708153 – Marco Daniel Mar 11 '21 at 18:26