question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Typescript 4.4+ prop-type to types not converting

See original GitHub issue

As an FYI, these issue only appears to occur in version 4.4+, I’ve tested and the codemod works on 4.2, so there is a work around.

For context, at this point I’ve already run the … command, which changed the files to be typescript correctly

npx ts-migrate rename . \
    --sources "./ConfirmationModal/index.jsx"

When following up with …

npx ts-migrate migrate . \
    --sources "./ConfirmationModal/index.tsx" \
    --sources "node_modules/**/*.d.ts" \
    --aliases tsfixme

The command would not be able to generate types from a “simple” react component that had prop types. On further testing this was because of the typescript version (4.6), debugging the react-props plugin, I found this interesting behaviour

Here …, we’re trying to find if there is an import declaration for prop-types, funnily the isImportDeclaration doesn’t return true in v4.4+ - I believe this is the cause of the bug.

https://github.com/airbnb/ts-migrate/blob/master/packages/ts-migrate-plugins/src/plugins/react-props.ts#L54

The codemod doesn’t outright error and it adds most types correctly, but without prop-types being changed to types in results in significantly more type errors than required.

Test file ConfirmationModal.tsx

import React from "react";
import pt from "prop-types";

const Modal = () => <div>Fake Modal</div>;
const CONFIRMATION_MODAL_SUCCESS = "CONFIRMATION_MODAL_SUCCESS";
const CONFIRMATION_MODAL_ERROR = "CONFIRMATION_MODAL_ERROR";

const ConfirmationModal = (props) => {
  const {
    loading,
    result,
    title,
    successTitle,
    errorTitle,
    bodyDescription,
    bodyItems,
    bodyOnConfirmDescription,
    successBody,
    errorBody,
    cancelText,
    confirmText,
    onConfirm,
    onCancel,
    secondaryText,
    secondaryAction,
  } = props;

  let modalTitle;
  let modalBody;

  switch (result) {
    case CONFIRMATION_MODAL_SUCCESS:
      modalTitle = successTitle || title;
      modalBody = <div>{successBody}</div>;
      break;
    case CONFIRMATION_MODAL_ERROR:
      modalTitle = errorTitle || title;
      modalBody = <div>{errorBody}</div>;
      break;
    default:
      modalTitle = title;
      modalBody = (
        <div>
          <p>{bodyDescription}</p>
          {bodyItems.map((item, index) => (
            <p key={index}>
              <p inline bold>
                {item.boldText}
              </p>{" "}
              {item.text}
            </p>
          ))}
          <p>{bodyOnConfirmDescription}</p>
        </div>
      );
  }
  return (
    <Modal
      isOpen
      isLoading={loading}
      modalTitle={modalTitle}
      cancelText={cancelText || "Cancel"}
      secondaryText={secondaryText}
      submitText={confirmText || "Confirm"}
      onSubmit={onConfirm}
      onCancel={onCancel}
      onSecondaryAction={secondaryAction}
      isSubmitable={result !== CONFIRMATION_MODAL_ERROR}
      hasSecondaryAction={
        !!secondaryAction && result === CONFIRMATION_MODAL_SUCCESS
      }
    >
      {modalBody}
    </Modal>
  );
};

ConfirmationModal.propTypes = {
  title: pt.string.isRequired,
  successTitle: pt.string,
  errorTitle: pt.string,
  bodyDescription: pt.string,
  bodyItems: pt.arrayOf(
    pt.shape({ boldText: pt.string, text: pt.string.isRequired })
  ),
  bodyOnConfirmDescription: pt.string,
  successBody: pt.string.isRequired,
  errorBody: pt.string.isRequired,
  cancelText: pt.string,
  confirmText: pt.string,
  onCancel: pt.func,
  onConfirm: pt.func,
  secondaryAction: pt.func,
  secondaryText: pt.string,
};

export default ConfirmationModal;

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:2
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dlemburgcommented, Aug 4, 2022

Can confirm that latest version of typescript and ts-migrate fixes this issue.

0reactions
Rudegcommented, Jun 8, 2022

could you please try the latest version?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - Advanced Types - TypeScript
This page lists some of the more advanced ways in which you can model types, it works in tandem with the Utility Types...
Read more >
Support setting React PropTypes using TypeScript types #4833
This might help someone, if you want to make a props to be required and force TypeScript to throw an error about it...
Read more >
React eslint error missing in props validation - Stack Overflow
If you are not sure what your variable type or suffix is for your PropType (ex: PropTypes.number), refer to this npm reference. To...
Read more >
Number - JavaScript - MDN Web Docs - Mozilla
Chrome Edge Number Full support. Chrome1. Toggle history Full support. Edge12. Toggl... EPSILON Full support. Chrome34. Toggle history Full support. Edge12. Toggl... MAX_SAFE_INTEGER Full support. Chrome34....
Read more >
The Definitive TypeScript 4.8 Guide - SitePen
Unlike other compile-to-JavaScript languages, TypeScript does not try ... error (“Cannot convert string to number”) since the two types are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found