From JS
Automated JS to TS Conversion
- TypeStat (used by Codecademy)
- TypeWiz
- js-to-ts-converter
- TS-migrate used in Airbnb's conversion
- dts-gen -
dts-gen
is a tool that generates TypeScript definition files (.d.ts) from any JavaScript object.
for JSON - http://json2ts.com/ generate TypeScript interfaces from JSON
Manual JS to TS Conversion
the "Just Renaming" strategy
- OSX/Linux:
find src -name "*.js" -exec sh -c 'mv "$0" "${0%.js}.tsx"' {} \;
You can either load typescript files with webpack, or use the tsc
compiler to compile your TS files to JS side by side. The basic tsconfig.json
is:
{
"compilerOptions": {
"allowJs": true
}
}
Then you will want to enable it to check JS:
{
"compilerOptions": {
"allowJs": true,
"checkJs": true
}
}
If you have a large codebase and this throws too many errors at once, you can opt out problematic files with //@ts-nocheck
, or instead turn off checkJs
and add a //@ts-check
directive at the top of each regular JS file.
TypeScript should throw up some egregious errors here which should be easy to fix.
Once you are done, swallow the red pill by turning off implicit any
's:
{
"compilerOptions": {
"allowJs": true,
"checkJs": true,
"noImplicitAny": true // or "strict": true
}
}
This will raise a bunch of type errors and you can start converting files to TS or (optionally) use JSDoc annotations in your JS.
A common practice here is using an ambient TODO type alias for any
so you can keep track of what you need to come back to:
type TODO_TYPEME = any;
export function myFunc(foo: TODO_TYPEME, bar: TODO_TYPEME): number {
// ...
}
Gradually add more strict
mode flags like noImplicitThis
, strictNullChecks
, and so on until you can eventually just run in full strict mode with no js files left:
{
"compilerOptions": {
"strict": true
}
}
3 Step Process
https://speakerdeck.com/amhinson/convert-a-react-native-project-to-typescript-in-10-minutes?slide=23
More resources
- Adopting TypeScript at Scale - AirBnB's conversion story and strategy - their ts-migrate tool here
- Scaling TypeScript lessons from Bloomberg
- Migrating a
create-react-app
/react-scripts
app to TypeScript - don't usereact-scripts-ts
- Migrating an EJECTED CRA app to TS
- Lyft's JS to TS migration tool (includes PropTypes migration)
- Hootsuite
- Etsy's Journey to TypeScript
- Storybook's migration (PR)
- How we migrated a 200K+ LOC project to TypeScript and survived to tell the story - Coherent Labs - using
grunt-ts
, jQuery and Kendo UI - incrementally adding strict null checks https://code.visualstudio.com/blogs/2019/05/23/strict-null
Old content that is possibly out of date