Typescript: Configure from package.json

Created on 13 Aug 2019  ·  11Comments  ·  Source: microsoft/TypeScript

Search Terms

package.json, config, configuration, tsconfig

Suggestion

Allow package.json as an alternate source for tsconfig.json options. To be clear, I am requesting that you please re-evaluate #6590 - it has been 2 years since that issue was posted, so interests may have changed and I believe this feature holds good value.

Use Cases

What do you want to use this for?

Provide a broader configuration support and decluttering the project root of configuration files that can easily be moved to package.json.

What shortcomings exist with current approaches?

Some users prefer to store their all their configuration files in 1 larger file. Right now, that is not an option.

Examples

Example of what the package.json would look like.

{
    "name": "example",
    "version": "1.0.0",
    "description": "This is an example",
    "license": "MIT",
    "tsconfig": {
        "compilerOptions": {
            "module": "commonjs",
            "moduleResolution": "node",
            "outDir": ".build",
            "pretty": true,
            "rootDir": "./",
            "sourceMap": true,
            "target": "ES5",
            "strict": true
        },
        "exclude": [
            "node_modules"
        ],
        "include": [
            "index.ts",
            "src/**/*"
        ]
    }
}

Checklist

My suggestion meets these guidelines:

  • ✅ This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • ✅ This wouldn't change the runtime behavior of existing JavaScript code
  • ✅ This could be implemented without emitting different JS based on the types of the expressions
  • ✅ This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • ✅ This feature would agree with the rest of TypeScript's Design Goals.
Awaiting More Feedback Suggestion

Most helpful comment

A few people in my organization have been somewhat offended by the number of configuration files required in a skeleton project, compared to other language ecosystems. Being able to get rid of a physical tsconfig.json file would help us out a little bit.

All 11 comments

Actually tsconfig is one of the last to require a specific file.
Many other projects made it possible to pull configuration from package.json.

I feel like this feature is pretty standard already. Babel, ESLint, Vue and Browserslist support it, so that's 4 files that I can save from cluttering my repo. Would be great to be able to do that with tsconfig.json as well.

Definitely, cosmiconfig is usually a good choice to simplify config resolution for tools.

Don't wanna add too much noise here. It was kinda mentioned by @dmnsgn with cosmiconfig, but I would love to see support for CommonJS configuration file support (ex. tsconfig.js with a module.exports definition).

I see the value in separating out these configurations away from the package.json so it doesn't get too bloated. Also, adding support for adding comments to the configuration file would be super valuable!!


Would be more than happy to help contribute to making this happen if at all possible (would need a bit of guidance maybe since I haven't contributed to TS before).

Yes, this would be perfect for small projects, online env's or boilerplates/templates.

I've tried to this through, webpack plugins or ts-loader, but they all look for a specific file to read from and send into tsc. Only solution i found is to use dev build (webpack / node), read from package.json, create a file somewhere, read from it, and delete it on completion.

Maybe it is possible to mock file so tsc would think it reads a file, while its actually not doing that, but that requires some sort of prebuild node env and it loses all point of small config's.

Sorry for duplicate issue

Cleaned out half a dozen config files from my project... the only one remaining is tsconfig. Hope someone makes this dream a reality!

With shareable configs, this will be awesome ❤️

@RyanCavanaugh are we still awaiting more feedback or is that tag no longer accurate?

As far as implementation, it looks like this might be straightforward with cosmiconfig and it looks like some people may be willing to contribute code to get this feature if it is accepted. (Myself included)

it looks like this might be straightforward with cosmiconfig

You should note that the TypeScript compiler has no dependencies. :-) So using something like cosmiconfig is pretty surely a no-go.

@MartinJohns That's a good point!

I can't speak for anyone else, but I would still be willing to contribute to get this feature.

A few people in my organization have been somewhat offended by the number of configuration files required in a skeleton project, compared to other language ecosystems. Being able to get rid of a physical tsconfig.json file would help us out a little bit.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

CyrusNajmabadi picture CyrusNajmabadi  ·  3Comments

DanielRosenwasser picture DanielRosenwasser  ·  3Comments

Antony-Jones picture Antony-Jones  ·  3Comments

jbondc picture jbondc  ·  3Comments

bgrieder picture bgrieder  ·  3Comments