Repository navigation
Description
Search Terms
config
tsconfig.json
Suggestion
It would be very helpful if typescript followed the conventions of pretty much every other JS-stack tool and allowed the configuration to be done as a javascript file so they can more easily be manipulated.
For example all these tools (and many more) already accept configuration files to be javascript:
Prettier -> .prettierrc.js
Jest -> jest.config.js
Webpack -> webpack.config.js
eslint -> .eslintrc.js
styleling -> .stylelintrc.js
lint-staged -> lint-staged.config.js
My suggestion is for typescript tsconfig resolver to look for a tsconfig.js file if available, if so require() it and use the exported values as configuration. If that file is not available then follow the current resolver rules for tsconfig.json.
Use Cases
- Filesystem introspection
- Better environment variable toggling of options
- Easier extensibility of configurations
- Easier building of tools that include typescript
Examples
module.exports = {
"includes": [path.resolve(__dirname, process.env.SOURCE_DIR)],
...
}const config = { ... }
if (NODE_ENV === 'development') config.compilerOptions.noUnusedLocals = false
module.exports = configRight now it is impossible to merge arrays when using the "extends" option of tsconfig.json. If the configuration were javascript files this would be feasible:
const originalConfig = require('some-dep/config/tsconfig.json')
const path = require('path')
module.exports = {
...originalConfig,
includes: [...originalConfig.includes, path.resolve(__dirname, './mytypes/')]
} 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.
Activity
[-]Javascript config file (tsconfig.js)[/-][+]Javascript tsconfig file (tsconfig.js)[/+]MartinJohns commentedon Jul 6, 2020
Duplicate of #30400. Used search terms:
tsconfig.jsStJohn3D commentedon Feb 11, 2022
#30400 should have been marked a duplicate of this one. This issue does a much better job of explaining why we need this feature. one tsconfig.json simply doesn't cut it for more complicated projects.
qodesmith commentedon Feb 12, 2022
Why was this issue closed? This is a fantastic suggestion. I'm looking to dynamically set
compilerOptions.pathsand with json, that's just impossible.ftzi commentedon May 9, 2022
Up. Having all the setup json as js files is the future already happening.
faeb187 commentedon Mar 24, 2023
Aaron Cordova (@qodesmith) actually it's not impossible, it's very easy 👍
@SrBrahma maybe generating the JSON files is more futuristic 🙏
fs.readFileorfs.readFileSyncwith option{ encoding: "utf8" }JSON.parse()the string you getJSON.stringify()the final JSON before...fs.writeFileorfs.writeFileSyncI was looking if
.jsis allowed but it's easy to just generate your config files (all of them) then add a CLI or whatever and configure or set some default templates, etc.Tofandel commentedon Jun 20, 2023
Fabio Gartenmann (@faeb187) That would not work if your config includes non encodable content, like functions, which would be the use case for a
.jsfileqodesmith commentedon Jun 24, 2023
Adrien Foulon (@Tofandel) What Fabio Gartenmann (@faeb187) is suggesting is manipulating JSON data by reading, updating, and re-writing the data back to disk. Since the source of truth would be a JSON file, there wouldn't be any non-encodable content. It's essentially the same solution I came up with a while ago, but it's still a hack.