• Stars
    star
    133
  • Rank 272,600 (Top 6 %)
  • Language
    TypeScript
  • License
    ISC License
  • Created over 4 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Codemod to convert React PropTypes to TypeScript types.

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://mskelton.dev/ratchet.ts GLOB

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

Try it Online!

Additionally, you can use Ratchet online at mskelton.dev/ratchet! 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 />
}

More Repositories

1

yarn-plugin-outdated

Yarn plugin to show outdated dependencies.
TypeScript
119
star
2

eslint-plugin-sort

Auto-fixable sort rules for ESLint.
TypeScript
24
star
3

vscode-npm-outdated

Displays a diagnostic message in package.json files for packages which have newer versions available.
TypeScript
17
star
4

dtsfmt

Auto formatter for device tree files.
Rust
12
star
5

termicons.nvim

Alternative set of icons for nvim-web-devicons using termicons.
Lua
12
star
6

setup-yarn

Reduce boilerplate when installing setting up Node.js and installing Yarn dependencies.
10
star
7

mql5

MQL5 core and standard library implemented in proper C++ for use with language servers and modern editors.
MQL5
8
star
8

local-yokel.nvim

Work with files close to home
Lua
7
star
9

tmux-last

Tmux plugin to open the last non-empty command output in a pager.
Shell
7
star
10

prettier-formatting-example

Auto formatting code using Prettier and GitHub Actions
JavaScript
7
star
11

wait-for-vercel-action

Wait for Vercel deployment to complete for a commit.
JavaScript
7
star
12

flutter.nvim

Neovim plugin for flutter development.
Lua
6
star
13

mskelton.dev

Welcome to my personal website!
MDX
6
star
14

tree-sitter-styled

This is an extension of tree-sitter-css to support styled-components interpolation syntax.
Rust
6
star
15

chatter

ChatGPT desktop app
TypeScript
6
star
16

hoverdown.nvim

Improves support for LSP hover documentation by properly parsing and highlighting markdown.
Lua
6
star
17

shexpand.lua

Expand a string into a table of strings using shell brace expansion.
Lua
5
star
18

changelog-reminder-action

Github action to remind users their pull request is missing a changelog.
JavaScript
5
star
19

tree-sitter-mql5

This is an extension of tree-sitter-cpp to support the mql5 language.
Rust
5
star
20

close-notion-tab

Automatically close Notion share link tabs after the desktop app opens.
JavaScript
5
star
21

cursed

Multiplayer live cursors
TypeScript
4
star
22

vscode-marie

MARIE language support for VS Code
4
star
23

termicons

Material style icons optimized for terminal rendering
Handlebars
4
star
24

live-reload.nvim

Live reload Neovim plugins with ease!
Lua
3
star
25

dotfiles

My personal settings and dotfiles.
Lua
3
star
26

macos-certs

GitHub action to create a keychain and import macOS certificates for publishing macOS apps.
Shell
3
star
27

eslint-config

Simple and sensible ESLint config.
JavaScript
3
star
28

tidi

Dead simple dependency injection
TypeScript
3
star
29

slides

Frontend 101 course.
TypeScript
3
star
30

flashlight

Find usages of imported symbols in your codebase.
Rust
3
star
31

jumper.nvim

A Telescope extension to quickly switch your working directory from a list of specified directories within a workspace.
Lua
2
star
32

fx-data

Historical Forex data collector.
MQL5
2
star
33

setup-bun

Reduce boilerplate when setting up Bun and installing dependencies.
2
star
34

semantic-release-config

Shared semantic-release configuration
JavaScript
2
star
35

analyzer

Forex trading analysis and sharing tool.
MQL5
2
star
36

cls

clsx + tailwind-merge
JavaScript
2
star
37

setup-pnpm

Reduce boilerplate when installing setting up Node.js and installing pnpm dependencies.
2
star
38

vscode-playwright-test-snippets

Useful snippets for the Playwright test runner.
2
star
39

yarn-constraints-example

Real world examples of when to use Yarn constraints.
Prolog
2
star
40

bandit.nvim

The fugitive's sidekick
Lua
2
star
41

vscode-react-emotion-snippets

Useful VS Code snippets for creating React Emotion styles.
2
star
42

bookmark-sync

Quickly update your saved bookmark URL with the current page URL.
TypeScript
2
star
43

icons

Material icons packaged as React components.
JavaScript
2
star
44

advent-of-code

My submissions for advent of code. Never stop learning!
Zig
2
star
45

biblens

A simple Android/iOS app that recognizes bible verses in text.
Dart
2
star
46

lazy-context

A thin layer on top of React context that supports "lazy subscription" to only re-render when the data you care about changes.
TypeScript
2
star
47

tree-sitter-applescript

AppleScript grammar for tree-sitter
Rust
1
star
48

use-strict-params

Strictly typed params for React router.
TypeScript
1
star
49

ray

My personal Raycast extensions
TypeScript
1
star
50

react-medium-image

A medium-style image viewer.
JavaScript
1
star
51

ev

CLI to manage your Apple calendar events
1
star
52

super-reactor

Respond to reaction requests with a bit too much fervor 🙂
JavaScript
1
star
53

lorem

CLI to generate lorem ipsum text
Go
1
star
54

versly

Read the Bible, every day, one chapter each day
Svelte
1
star
55

pomo-menu

macOS menu bar app to accompany the pomo command.
Swift
1
star
56

gobble

Command line RSS feed viewer
Go
1
star
57

todo

Go
1
star
58

pkg

Fish plugin for parsing package.json files
Shell
1
star
59

bk

Command line tool for interacting with Buildkite pipelines, builds, and more.
Go
1
star
60

url

Parse URLs from a string.
Shell
1
star
61

ama

Ask me anything!
1
star
62

sandbox

TypeScript
1
star
63

speak

Speak your mind, without your voice.
TypeScript
1
star
64

bytes

The source repository for my website's "bytes" (mini posts).
1
star
65

jira

Parse Jira issues from text.
Go
1
star
66

yarn-completion

Fish shell autocompletion rules for Yarn berry
1
star
67

togglr

An open source feature flag platform built for performance, stability, and feature richness.
TypeScript
1
star
68

webpack-config

Shared webpack config for my React projects.
TypeScript
1
star
69

setup

AR experience for playing the card game Set.
Rust
1
star
70

rust-template

Template for new Rust CLI projects.
Shell
1
star
71

graphite-to-github

Redirects Graphite pull request URLs to GitHub
1
star
72

hlx-browser-extension

Helpful utilities to manage your Higher Level FX VPS.
1
star
73

pr

Interactively create a pull request.
Shell
1
star
74

oldies.nvim

Navigate your oldfiles without losing your mind.
Lua
1
star
75

pomo

Simple pomodoro timer CLI.
Rust
1
star
76

mskelton

1
star
77

tsk

Tasks for the terminal dweller
Go
1
star
78

req

Simple data fetching library built on top of the fetch API
TypeScript
1
star
79

browserslist-config

Supported browsers in my projects.
JavaScript
1
star
80

byte

CLI tool to create bytes (mini posts) for my website.
Go
1
star
81

veritas

Assert facts against your data sources
TypeScript
1
star
82

lineup

Baseball lineup generator
TypeScript
1
star
83

ui

UI library built on modern web platform features.
JavaScript
1
star
84

npm-template

Project generator for my personal projects.
TypeScript
1
star
85

hexterm.lua

Get the xterm closest color to a hexadecimal color
Lua
1
star
86

time-ago

Formats a date using the Intl RelativeTimeFormat API with automatic unit selection.
TypeScript
1
star
87

bible-plan-generator

Bible reading plan generator
Python
1
star
88

observer

A super simple and performant way to observer specific selectors in the DOM.
TypeScript
1
star
89

pomo-go

Simple pomodoro timer CLI
Go
1
star
90

dottie

Dead simple dotfile management with the full power of bash.
Shell
1
star
91

go-template

Template for new Go CLI projects.
Shell
1
star
92

melodies

Song book and instrument tabs when you are at home or on the go.
TypeScript
1
star
93

tsconfig

Shared TypeScript config for my projects.
1
star
94

dprint-config

My personal dprint config.
1
star
95

isomorphic-resolve

Isomorphic `require.resolve` for ESM and CommonJS
JavaScript
1
star
96

go.mskelton.dev

Short links for mskelton.dev
HTML
1
star
97

bm

Open bookmarks from the Arc browser in your terminal.
Shell
1
star
98

tailwind-plugins

My collection of tailwind plugins.
JavaScript
1
star
99

pngpaste

Paste images from your clipboard into a PNG file.
Shell
1
star
100

unified-plugins

My collection of unified plugins.
JavaScript
1
star