Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Demo] Use AI to codemod this repo to TS #31

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

NickHeiner
Copy link
Owner

@NickHeiner NickHeiner commented Jan 12, 2023

I did this by running the following command:

jscodemod --prompt "// Convert the code above to TypeScript. Be sure to retain any variables imported via require. Use ESM instead of CommonJS for imports and exports. Remove the \"use strict\" directive. You can import the following global types from `my-global-types` and use them as you see fit: `GameState`, `Player`, `MovePart`, `Row`, `Col`, `Space`." ../camelot-engine/lib/**/*.js

Using jscodemod's AI system, powered by OpenAI.

Comparison to previous tools

Before AI codemods, we could use something like https://github.com/airbnb/ts-migrate/. However, useful as that tool is, it's limited to very coarse-grained types – it basically just inserts any everywhere.

With an AI codemod, it's actually able to infer types from the surrounding usage:

function f(a, gameState, b) {
  console.log(a.c, gameState);
  a.c(b || 'default-value');
}

// ts-migrate result
function f(a: any, gameState: any, b: any) {
  console.log(a.c, gameState);
  a.c(b || 'default-value');
}

// AI result
import type { GameState } from 'my-global-types';
function f(a: { c: (arg: string) => void }, gameState: GameState, b?: string): void {
  console.log(a.c);
  a.c(b || 'default-value');
}

Additionally, to use ts-migrate on my large production codebase at Netflix, I had to make a bunch of changes over 4 days. By contrast, the only setup time for the AI-powered TypeScript conversion was 4 minutes of tweaking the prompt.

Nick Heiner added 2 commits January 12, 2023 15:35
jscodemod --prompt "// Convert the code above to TypeScript. Be sure to retain any variables imported via require. Use ESM instead of CommonJS for imports and exports. Remove the \"use strict\" directive. You can import the following global types from `my-global-types` and use them as you see fit: `GameState`, `Player`, `MovePart`, `Row`, `Col`, `Space`." ../camelot-engine/lib/**/*.js
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

1 participant