Initial TypeScript setup and conversion (CJS to ESM, ES5 class functions to ES6 classes)#707
Initial TypeScript setup and conversion (CJS to ESM, ES5 class functions to ES6 classes)#707ericyhwang wants to merge 7 commits intotypescriptfrom
Conversation
…ttached to the class functions for better codemod porting
b9d6b87 to
a4afeec
Compare
| "ignoreDeprecations": "6.0", | ||
| "noImplicitAny": false, | ||
| "noImplicitThis": false, | ||
| "removeComments": false, |
There was a problem hiding this comment.
Why? JSDoc or something?
| "compilerOptions": { | ||
| "rootDir": "./src", | ||
| "outDir": "./lib", | ||
| "target": "ES5", |
There was a problem hiding this comment.
How did you decide this?
| "rootDir": "./src", | ||
| "outDir": "./lib", | ||
| "target": "ES5", | ||
| "module": "CommonJS", |
There was a problem hiding this comment.
(Switching to ESM at some point is going to be fun 🙄 )
| "target": "ES5", | ||
| "module": "CommonJS", | ||
| "types": [ | ||
| "node" |
There was a problem hiding this comment.
I'm not sure I like this. On a selfish note, I think this will pollute our own code's types, because we run backend.js even in the browser 😅
But from a less selfish perspective, this shouldn't apply to client/ which is designed for browser consumption. I think we possibly want 2 different tsconfig.json files? One for backend stuff, and one for client?
In an ideal world I think backend and client would be 2 separate packages in a monorepo, but that's obviously out of scope of this change.
| ], | ||
| "strict": true, | ||
| "esModuleInterop": true, | ||
| "skipLibCheck": true, |
Summary
Set up TypeScript, do file renames from *.js to *.ts, then run codemods to convert CommonJS to ES modules and ES5 class functions to ES6 classes.
This is a redo of #705 without the dev dependency upgrades, which I moved out into #706 and are already merged into master.
Couple bigger codemod changes here on top of the older PR:
Backend.prototype.MIDDLEWARE_ACTIONS = ..., are declared as fields, with the assignment moved intostaticblocks to reduce the diff size.//comments, they are converted to JSDoc-style/** */comments.Goals
It targets the
typescriptbranch, which we can use to stage the TypeScript changes until they're ready to merge into main. Making smaller intermediate PRs into that branch will make things easier to review.For now, my goal with the
typescriptstaging branch is to end up with TS source code that compiles into JS output compatible with the current JS source, so we could publish it a minor version. In a future major version, we can choose to update the compile target to drop ES5 support, especially if we want to start using things like async/await without the extra verboseness of downleveling.We should probably major version soon anyways, since Node 18's been EoL for a year, and Node 20's about to reach EoL.
Reviewer tips and notes
typescript-initialbranch down and locally view the commit's diff, e.g. in your editor.typescript-cjs-class-codemods-js-outputsbranch, in commit a4afeec. Similarly, it's better to pull the branch and compare locally.Let me know if you want to schedule time to go through this on a call!
Changes
lib/**/*.jsfiles tosrc/**/*.tswith no changesgit blameand other history spelunking tools on the "new" TS files.module.exportsstatements up. Some class JSDoc comments were attached to themodule.exportslines. Moving them so the comments are attached to the class functions means the codemod (next step) can do a better job porting the comments over to the ES6 class.Next steps
First thing after this PR is to go through the many TS compilation errors and fix them. Most fall into these categories:
extendsThen it'll be adding stronger types, likely based on our DefinitelyTyped definitions; doing more cleanup as needed; and converting test files to TS.