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

Consult package.json contents when creating tsconfig.json under tsc --init to determine better defaults #51207

Open
DetachHead opened this issue Oct 18, 2022 · 2 comments
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript

Comments

@DetachHead
Copy link
Contributor

Bug Report

πŸ”Ž Search Terms

tsc init module commonjs

πŸ•— Version & Regression Information

4.9.0-dev.20221017

⏯ Playground Link

n/a

πŸ’» Code

package.json

{
  "name": "asdf",
  "version": "1.0.0",
  "description": "",
  "author": "",
  "license": "ISC",
  "type": "module",
  "devDependencies": {
    "typescript": "^4.9.0-dev.20221017"
  }
}

πŸ™ Actual behavior

> npx tsc --init

Created a new tsconfig.json with:                                                                                       
                                                                                                                     TS 
  target: es2016
  module: commonjs
  strict: true
  esModuleInterop: true
  skipLibCheck: true
  forceConsistentCasingInFileNames: true


You can learn more at https://aka.ms/tsconfig

πŸ™‚ Expected behavior

it should check the package.json to determine that module should be nodenext or something

@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature labels Oct 18, 2022
@RyanCavanaugh RyanCavanaugh changed the title tsc --init sets module to "commonjs" when type is "module" in package.json Consult package.json contents when creating tsconfig.json under tsc --init to determine better defaults Oct 18, 2022
@DetachHead
Copy link
Contributor Author

DetachHead commented Apr 14, 2023

moduleResolution should also default to nodenext or node16 when "type": "module" is in package.json.

also, maybe it would be better to not even include these options when running tsc --init and instead just infer the default values from package.json when they are not present in tsconfig.json

@akwodkiewicz
Copy link

I would argue that module+moduleResolution should be set to nodenext+nodenext/node16+node16 for both CommonJS and ES Module projects, not only for the latter.

After all, node16+node16 are the settings that allows you to correctly transpile dynamic import().

The current default of commonjs+node does not let you use ESM packages inside a CommonJS project, even if a dynamic import was used, it fails quietly (#45125).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Awaiting More Feedback This means we'd like to hear from more people who would be helped by this feature Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants