Add support for TS config files (#16828)

* feat: prettier.config.ts

* revert

* feat: add flag

* fix: remove experimental warning

* Revert changes

* Add tests

* Update docs

* Only test on Node.js v22

* Add changelog

* Update docs

* Update examples

* Fix changelog

* Fix command

* Name `loadJs` back

* Fix

---------

Co-authored-by: fisker <lionkay@gmail.com>
This commit is contained in:
Norbert Elterandfisker authored and GitHub committed 2024-12-21 15:39:45 +09:00
1 parent 66a77da2c5
commit 84de783ce6
15 files changed
+246 -4

No files matched your search

+47 -3
View File
@@ -8,17 +8,33 @@ You can configure Prettier via (in order of precedence):
- A `"prettier"` key in your `package.json`, or [`package.yaml`](https://github.com/pnpm/pnpm/pull/1799) file.
- A `.prettierrc` file written in JSON or YAML.
- A `.prettierrc.json`, `.prettierrc.yml`, `.prettierrc.yaml`, or `.prettierrc.json5` file.
- A `.prettierrc.js`, or `prettier.config.js` file that exports an object using `export default` or `module.exports` (depends on the [`type`](https://nodejs.org/api/packages.html#type) value in your `package.json`).
- A `.prettierrc.mjs`, or `prettier.config.mjs` file that exports an object using `export default`.
- A `.prettierrc.cjs`, or `prettier.config.cjs` file that exports an object using `module.exports`.
- A `.prettierrc.js`, `prettier.config.js`, `.prettierrc.ts`, or `prettier.config.ts` file that exports an object using `export default` or `module.exports` (depends on the [`type`](https://nodejs.org/api/packages.html#type) value in your `package.json`).
- A `.prettierrc.mjs`, `prettier.config.mjs`, `.prettierrc.mts`, or `prettier.config.mts` file that exports an object using `export default`.
- A `.prettierrc.cjs`, `prettier.config.cjs`, `.prettierrc.cts`, or `prettier.config.cts` file that exports an object using `module.exports`.
- A `.prettierrc.toml` file.
> TypeScript configuration files support requires [additional setup](#typescript-configuration-files)
The configuration file will be resolved starting from the location of the file being formatted, and searching up the file tree until a config file is (or isn’t) found.
Prettier intentionally doesn’t support any kind of global configuration. This is to make sure that when a project is copied to another computer, Prettier’s behavior stays the same. Otherwise, Prettier wouldn’t be able to guarantee that everybody in a team gets the same consistent results.
The options you can use in the configuration file are the same as the [API options](options.md).
### TypeScript Configuration Files
TypeScript support for Node.js is currently experimental, Node.js>=22.6.0 is required and `--experimental-strip-types` is required to run Node.js.
```sh
node --experimental-strip-types node_modules/prettier/bin/prettier.cjs . --write
```
or
```sh
NODE_OPTIONS="--experimental-strip-types" prettier . --write
```
## Basic Configuration
JSON:
@@ -70,6 +86,34 @@ const config = {
module.exports = config;
```
TypeScript (ES Modules):
```ts
// prettier.config.ts, .prettierrc.ts, prettier.config.mts, or .prettierrc.mts
import { type Config } from "prettier";
const config: Config = {
trailingComma: "none",
};
export default config;
```
TypeScript (CommonJS):
```ts
// prettier.config.ts, .prettierrc.ts, prettier.config.cts, or .prettierrc.cts
import { type Config } from "prettier";
const config: Config = {
trailingComma: "none",
};
module.exports = config;
```
YAML:
```yaml