Skip to content

tsc --init created tsconfig.json is not compatible with JSON.parse() #62286

@beenotung

Description

@beenotung

🔎 Search Terms

tailing comma

🕗 Version & Regression Information

⏯ Playground Link

No response

💻 Code

npx tsc --init
trim-tsconfig

Where the content of the trim-tsconfig script is as below:

#!/usr/bin/env node
let fs = require('fs')
let file = process.argv[2] || 'tsconfig.json'
let content = fs
  .readFileSync(file, 'utf8')

  .split('\n')
  .filter(line => !line.trim().startsWith('//'))
  .join('\n')

  .replace(/\/\*.*\*\//g, '')

  .split('\n')
  .filter(line => line.trim())
  .map(line => line.trimEnd())
  .join('\n')

fs.writeFileSync(file, content)

🙁 Actual behavior

The produced tsconfig.json contains tailing comma, which is not compatible with JSON.parse()

🙂 Expected behavior

The produced tsconfig.json should be compatible with JSON.parse()

Additional information about the issue

In versions before 5.8, the tsconfig.json created by tsc --init contains comments, which is not a compatible format of JSON that could not be parsed by JSON.parse(). It was trivial to remove the comments with // ... and /* ... */.

However since version 5.9, the tsconfig.json created by tsc --init contains tailing comma, which is hard to be preprocessed, for passing to JSON.parse().

The current version of tsc --init support wide range of arguments, e.g. --module commonjs and --outDir dist.

Could we enhance tsc --init to also support disabling tailing comma (e.g. --omitTailingComma), or even omitting the comments (e.g. --omitComments)?

Workaround is to use the old version before the fix is released, as raised in #58420 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions