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

🐛 Bug: Migrating to typescript - user story #1561

Open
3 tasks done
rubiesonthesky opened this issue Apr 11, 2024 · 0 comments
Open
3 tasks done

🐛 Bug: Migrating to typescript - user story #1561

rubiesonthesky opened this issue Apr 11, 2024 · 0 comments
Labels
type: bug Something isn't working :( 🐛

Comments

@rubiesonthesky
Copy link
Collaborator

Bug Report Checklist

  • I have tried restarting my IDE and the issue persists.
  • I have pulled the latest main branch of the repository.
  • I have searched for related issues and found none that matched my issue.

Expected

// omit

Actual

// omit

Additional Info

This issue describes the whole process ,because I think it's easier to understand that way instead of filing multiple small issues.

Initial state

I have one project where I all my javascript files are in src and they user extension mjs. My project also has jsconfig.json. This project does not have any dependencies. It's just run with command node src/index.mjs to do some dev tasks. It may not be very representative of common project that would be converted to Typescript like this.

{
  "compilerOptions": {
    "allowJs": true,
    "checkJs": true,
    "moduleResolution": "nodenext",
    "module": "nodenext"
  },
  "typeAcquisition": {
    "enable": true,
    "include": ["node"]
  }
}

Running first time

Testing TypeStat for typescript migration. These were my answers

✔ What are you trying to accomplish? · Convert my JavaScript files to TypeScript
✔ Where is your tsconfig.json? · I don't have one yet
✔ Should TypeScript output .js files from .ts sources? · yes
✔ Does your code run in browser? · no
✔ Does your project use JSX? · no
✔ What minimum runtime does your code run on? · es2022
✔ Would you like to enable TypeScript's strict compiler options? (recommended) · yes
✔ Which glob matches files you'd like to convert? · other
✔ Which files would you like to convert? · src/**/*.mjs
✔ How would you like .js files to be renamed? · Rename all files to .ts
✔ Would you like imports without extensions to have those extensions added in? · No
✔ Would you like to suppress remaining type errors with // @ts-expect-error comments? · Yes

the generated tsconfig.json looks like this

{
    "compilerOptions": {
        "declaration": true,
        "declarationMap": true,
        "esModuleInterop": true,
        "lib": [
            "es2022"
        ],
        "sourceMap": true,
        "moduleResolution": "node",
        "module": "nodenext",
        "skipLibCheck": true,
        "strict": true,
        "target": "es2022"
    }
}

typestat.json

{
  "files": {
    "renameExtensions": "ts"
  },
  "fixes": {
    "importExtensions": false,
    "incompleteTypes": true,
    "missingProperties": true,
    "noImplicitAny": true
  },
  "cleanups": {
    "suppressTypeErrors": true
  },
  "include": [
    "src/**/*.mjs"
  ],
  "project": "./tsconfig.json"
}

Changes made

The only change that this did, is that it change filenames from ".mjs" to ".ts". This broke the code, because my imports were using ".mjs" endings 😬 And probably because of this, it couldn't fix incomplete types? It's though also possible that the incomplete types failed, because I had not installed typescript beforehand.

Feedback / questions / ideas

  • Should I have installed typescript first?
  • Could jsconfig.json be used to import some settings to tsconfig.json?
  • I'm not sure what "Should TypeScript output .js files from .ts sources?" means. I first thought it means renaming files, but I guess it means that should it emit js files. Now it feels silly that I did not understand it when reading first time.
  • The tool should handle project that uses .mjs extensions. Though I guess the imports should use ".js" and this is also user error?
  • Generated tsconfig.json has warning / error: "Option 'moduleResolution' must be set to 'NodeNext' (or left unspecified) when option 'module' is set to 'NodeNext'."

That said, I think I should try this again with next release to see, how it works when doing that.

(Annoyingly, I lost a file that had some secrets that were not saved anywhere due to user error. They were in another .mjs file that was not committed to git. And when I reverted the changes this tool did, I lost that file also. I guess lesson learned to not do that in future.)

@rubiesonthesky rubiesonthesky added the type: bug Something isn't working :( 🐛 label Apr 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug Something isn't working :( 🐛
Projects
None yet
Development

No branches or pull requests

1 participant