GitXplorerGitXplorer
m

ratchet

public
139 stars
14 forks
5 issues

Commits

List of commits on branch main.
Verified
b3c23e5e7b9216999285ba74e33a02086bbb2615

Update domain

mmskelton committed 9 months ago
Unverified
e93c293e2ff84e57699d19b61ac99144c8e4302d

Fix lockfile

mmskelton committed 10 months ago
Unverified
ebf143b0dc7b96b05f7c8d5a262532d2e36ae2b1

Update Yarn

mmskelton committed 10 months ago
Unverified
5c31b60438e16f91e9404b6af9fdbc4e815a971e

Switch function syntax

mmskelton committed 10 months ago
Unverified
36bf18841814069643b53d59d7f4bcd4682ad278

Fix lockfile

mmskelton committed 10 months ago
Verified
0bbc62b4788524ad945e9fb132927d7771893a34

Update readme (#80)

mmohab-sameh committed a year ago

README

The README file for this repository.

Ratchet

Test GitHub deployments

Codemod to convert React PropTypes to TypeScript types.

Key Features

  • Supports function and class components
  • Supports static propTypes declarations on class components
  • Supports forwardRefs
  • Supports files with multiple components
  • Copies JSDoc comments to the generated TypeScript types
  • Option to remove or preserve PropTypes after converting to TS

Usage

Thanks to my friends at Codemod, you can easily run Ratchet on your project using the Codemod VS Code extension:

Run in Codemod.com

To learn more about using the Codemod platform, read the docs here. If you encounter any issues, please reach out to my good friends at Codemod.

Or run the following command with a file glob that matches the files you want to convert.

npx jscodeshift -t https://go.mskelton.dev/ratchet.ts GLOB

# Example
npx jscodeshift -t https://go.mskelton.dev/ratchet.ts src/**/*.{js,jsx}

Try it Online!

Additionally, you can use Ratchet online at ratchet.mskelton.dev! Simply paste your input on the left and instantly see the output on the right!

Screenshot

Example: Function Component

Input:

// Input
import PropTypes from "prop-types"
import React from "react"

export function MyComponent(props) {
  return <span />
}

MyComponent.propTypes = {
  bar: PropTypes.string.isRequired,
  foo: PropTypes.number,
}

Output:

import React from "react"

interface MyComponentProps {
  bar: string
  foo?: number
}

export function MyComponent(props: MyComponentProps) {
  return <span />
}

Options

--preserve-prop-types

Preserves prop types after converting to TS. There are two available modes: all and unconverted.

--preserve-prop-types=all

CLI alias: --preserve-prop-types

This option will preserve all PropTypes. This is useful for component libraries where you support both TypeScript declarations and PropTypes.

Input:

import PropTypes from "prop-types"
import React from "react"

export function MyComponent(props) {
  return <span />
}

MyComponent.propTypes = {
  foo: PropTypes.number,
}

Output:

import PropTypes from "prop-types"
import React from "react"

interface MyComponentProps {
  foo?: number
}

export function MyComponent(props: MyComponentProps) {
  return <span />
}

MyComponent.propTypes = {
  foo: PropTypes.number,
}

--preserve-prop-types=unconverted

This option will preserve prop types which could not be fully converted. For example, spread expressions are not converted, and custom validators are converted to unknown. This option is useful to preserve these expressions so you can manually review and convert to their TypeScript equivalent.

Input:

import PropTypes from "prop-types"
import React from "react"

export function MyComponent(props) {
  return <span />
}

MyComponent.propTypes = {
  ...OtherComponent.propTypes,
  foo: PropTypes.number,
  bar(props, propName, componentName) {
    return new Error("Invalid prop")
  },
}

Output:

import PropTypes from "prop-types"
import React from "react"

interface MyComponentProps {
  foo?: number
  bar: unknown
}

export function MyComponent(props: MyComponentProps) {
  return <span />
}

MyComponent.propTypes = {
  ...OtherComponent.propTypes,
  bar(props, propName, componentName) {
    return new Error("Invalid prop")
  },
}

--declaration-style

Allow to choose between interfaces & type aliases. Default is interface.

--declaration-style=type

Create type alias instead of interface.

Input:

// Input
import PropTypes from "prop-types"
import React from "react"

export function MyComponent(props) {
  return <span />
}

MyComponent.propTypes = {
  bar: PropTypes.string.isRequired,
  foo: PropTypes.number,
}

Output:

import React from "react"

type MyComponentProps = {
  bar: string
  foo?: number
}

export function MyComponent(props: MyComponentProps) {
  return <span />
}