# TypeScript

Type-check every package with a tsconfig.json, and build those whose build script runs tsc, cached until a source file or tsconfig changes.

The TypeScript plugin gives every package with a `tsconfig.json` a `typecheck` task, so type errors are found in every package, in parallel, and a package whose files haven't changed is skipped.

## Add it

```sh
axle add typescript
```

This writes it into the root `axle.toml`:

```toml
[plugins.typescript]
source = "npm:@axle-rs/plugin-typescript@0.1.0"
hash = "sha256-…"
```

## Tasks

A package or project with a `tsconfig.json` in its folder gets:

| Task        | Command        | Settings                                                                               |
| ----------- | -------------- | -------------------------------------------------------------------------------------- |
| `typecheck` | `tsc --noEmit` | `inputs` the package's files and every `tsconfig*.json` at the root, `outputs` nothing |
| `build`     | `tsc`          | the same `inputs`, `outputs` the `outDir` and `*.tsbuildinfo`                          |

A library built with the TypeScript compiler alone gets `build` only when its `build` script is `tsc`, `tsc -p .`, `tsc --project .` or `tsc -p tsconfig.json`, and its `tsconfig.json` sets `outDir` to a plain string. The plugin then takes the script over, so the build is cached and its output restored. A build script that runs anything else, or a `tsconfig.json` that gets `outDir` from the config it extends, keeps the script as it is.

Each command runs through the repository's package manager, such as `pnpm exec tsc --noEmit`, so it uses the version the package installs.

The root's `tsconfig*.json` files are inputs since packages usually extend a shared one, such as `tsconfig.base.json`. The task waits for `typecheck` in the packages it depends on, as every task does by default, so their errors show first.

## Scripts the repository already has

A package's own script wins. When its `package.json` has a script with a task's name that runs something else, such as `"typecheck": "vue-tsc --noEmit"`, the plugin leaves that task alone and Axle runs the script. A script that only runs the same command, `tsc --noEmit`, `tsc --noEmit -p .` or `tsc -p . --noEmit`, is replaced by the plugin's task, which runs the same command and adds its settings. The repository's `axle.toml` changes a task as it would any other, and wins over the plugin.

## Settings

| Setting     | Default       | Meaning                                          |
| ----------- | ------------- | ------------------------------------------------ |
| `typecheck` | `"typecheck"` | the name of the task, or `false` to leave it out |
| `build`     | `"build"`     | the name of the build task, or `false`           |

```toml
[plugins.typescript]
source = "npm:@axle-rs/plugin-typescript@0.1.0"
hash = "sha256-…"
typecheck = "types"
```

The plugin's answers are kept until a `tsconfig.json`, a `package.json`, `pnpm-workspace.yaml` or a lockfile changes.
