# Turn On TypeScript Strict Mode and Fix the Errors

Yes. An agent enables strict mode in your tsconfig one flag at a time, fixes every error the compiler reports with real types, keeps the tests green, and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/enable-strict-mode/typescript/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads the compiler configuration and the shape of the code before it flips a flag, because `strict` is seven checks and a codebase written without them fails in predictable places. In an API laid out like javieraviles/node-typescript-koa-rest, `tsconfig.json` targets ES2017 with CommonJS modules, has `noImplicitAny` on, and keeps `strict`, `strictNullChecks`, `strictPropertyInitialization` and the other strict flags commented out; `experimentalDecorators` and `emitDecoratorMetadata` are on for TypeORM and `koa-swagger-decorator`, `tests/` is excluded, and `skipLibCheck` is on. `package.json` asks for TypeScript `^4.2.3`, which the lockfile resolves to 4.2.3, older than the 4.4 that introduced `useUnknownInCatchVariables`, so the agent's first change is `npm update typescript` within that range; it also pins `@types/koa` 2.13.1 and `@types/node` 14, and the `lint` script runs `tsc --noEmit` before ESLint, so a type error already fails `npm run build`. The code has the usual shapes: `src/entity/user.ts` declares `id`, `name` and `email` with no initialiser, which `strictPropertyInitialization` rejects; `src/controller/user.ts` types `findOne` results as `User | undefined` and reads `ctx.request.body.name` from an untyped body; `src/logger.ts` catches `err` and reads `err.status`, which is an `unknown` catch variable under `useUnknownInCatchVariables`; `src/server.ts` sets `connectionOptions.extra.ssl` on an `extra: {}` literal; and the tests mock `typeorm` with `jest.fn()` returning `any`.

The agent turns the flags on one at a time, in the order that keeps each diff readable: `strictNullChecks` first, since it produces most of the errors, then `strictPropertyInitialization`, `strictFunctionTypes`, `strictBindCallApply`, `noImplicitThis`, `alwaysStrict` and `useUnknownInCatchVariables`, and finally replaces the list with `strict: true`. After each flag it runs `npx tsc --noEmit`, reads every error, and fixes it with a real type rather than a cast: entity columns get the definite assignment marker, because TypeORM sets them, with a comment that says so; the controllers narrow `User | undefined` with the early return they already have; the request body gets a typed interface validated by the `class-validator` call that already runs; the catch block narrows `err` with a type guard before reading `status`; and the `extra` option gets its TypeORM type instead of `{}`.

Where a type comes from a library, the agent checks the pinned `@types` package before changing code: `@types/koa` 2.13.1 types `ctx.request.body` through `koa-bodyparser`'s augmentation only when that package's types are imported, so the fix is an import, not an `any`. It adds no `// @ts-ignore` and no `as any`; where a value is unknowable, the type says `unknown` and the code checks it. ESLint's `no-explicit-any` rule is already a warning in `.eslintrc`, and the pull request leaves the count of those warnings no higher than before.

To check it, the agent runs `npm run lint` (which is `tsc --noEmit` plus ESLint), `npm test` with Jest and `npm run build` after every flag and once more at the end, in its sandbox on the Node version `engines` names. It lists in the pull request how many errors each flag produced and how each class of error was fixed, so a reviewer reads one explanation per pattern rather than one per line.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Turn on TypeScript strict mode in this project and fix the errors. Read tsconfig.json, package.json, .eslintrc, src/server.ts, src/config.ts, src/logger.ts, src/entity/user.ts, src/controller/user.ts, src/controller/general.ts and the tests under tests/ first. If the installed TypeScript is older than 4.4, update it within the range package.json allows before any flag moves. Enable the strict flags one at a time, running tsc --noEmit after each: strictNullChecks, strictPropertyInitialization, strictFunctionTypes, strictBindCallApply, noImplicitThis, alwaysStrict, useUnknownInCatchVariables, then replace the list with strict: true. Fix every error with a real type and no ts-ignore or as-any casts: definite assignment on ORM-managed entity fields with a comment, narrowing instead of non-null assertions, a typed request body backed by the existing validation, a type guard in the catch block, and the library's own option types. Keep the number of no-explicit-any warnings from rising. Run lint, the Jest tests and the build after each flag and at the end, record the error count per flag and how each class of error was fixed, and open a pull request with the output.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

The pull request changes `tsconfig.json`, `package.json` and `package-lock.json` for the TypeScript update, and the source files that needed a type, with the tests touched only where a mock's type had to be named. Its description has a table of the strict flags in the order they were enabled, the number of errors each one surfaced, and the fix applied to each class of error, followed by the final `lint`, `test` and `build` output.

`tsc --noEmit` passes with `strict: true`, so the `lint` script and the `CI` workflow's build step now enforce it on every push. Runtime behaviour is unchanged: no logic moved, the entity columns, routes and middleware order are as they were, and the emitted JavaScript under `dist/` differs only where a type guard was added.

Before merging, a reviewer reads the per-pattern explanations, checks the definite assignment markers are limited to fields the ORM really sets, and decides whether to add `noUnusedLocals` and `noImplicitReturns`, which the pull request leaves off because they are not part of `strict`.

## Run it on every repository with strict mode off

A team that owns several TypeScript services sends this body once per repository to `POST /o/{org}/sessions`, each with its own repository in `resources` and a `request_id` that is a UUID v5 of the repository's full name, so a retried call returns the run already started instead of opening a second pull request. Each run reads its own `tsconfig.json`: a project with `strict` already on but `noUncheckedIndexedAccess` off gets that flag instead, a monorepo with a shared base config gets the flags in the base and the fixes in every package that extends it, and a project whose range holds TypeScript under 4.4 is told `useUnknownInCatchVariables` is unavailable and gets the rest.

A `webhook_url` on the key reports each `run.completed` with the pull request in `result`. A repository where the fix would touch hundreds of files is better split: the run reports the per-flag counts first, and the team sends a follow-up on the session with `POST /o/{org}/sessions/{id}/messages` naming which flags to land now.

## Why run it on Rush?

Strict mode is one line to enable and a long afternoon to make pass, and the temptation on a laptop is to reach for casts so the build goes green. The agent does the flags one at a time in its own sandbox, fixes each error with a type the compiler can check, and keeps a record of what each flag found, so the pull request explains itself.

The compiler output after every flag, the test runs and the build stay with the run and are pasted into the pull request, so the review is about the types chosen rather than whether the build passes. Several services can be moved to strict at once, each with its own list of patterns.

## Frequently asked questions

### Will this change what the code does at runtime?

No. Strict mode changes what the compiler accepts, not what it emits, and the fixes are types, narrowing and guards. Where a guard is added, such as in the catch block, the pull request shows the before and after so a reviewer can see the behaviour is the same for the values that occur.

### Why not just set strict: true and fix everything?

Because a single diff with every error from seven checks is hard to review. Enabling one flag at a time gives a count and a pattern per flag, and the final config is still `strict: true`, so nothing is left half-enabled.

### What about the definite assignment markers on entity fields?

An ORM entity's columns are set by the ORM, not a constructor, so `strictPropertyInitialization` cannot see them being assigned. The `!` marker says that to the compiler; the pull request uses it only on `@Column` fields and explains why in a comment, which is the convention TypeORM's own documentation uses.

### Does it touch the tests?

`tests/` is excluded from `tsconfig.json`, so the compiler does not check them, and `ts-jest` runs them with the same config. They change only where a mock returned a value whose type now has to be named, and `npm test` passes after every flag.

## More TypeScript jobs

- [Write Release Notes for a TypeScript Library](https://getrush.ai/run/explain-a-pull-request-history/typescript/)
- [Fix a Failing CI Build in a TypeScript Project](https://getrush.ai/run/fix-failing-ci/typescript/)
- [Fix "error TS5102: Option 'importsNotUsedAsValues' has been removed" in tsconfig.json](https://getrush.ai/run/fix-importsnotusedasvalues-removed/typescript/)

## The same job on other stacks

- [Enable TypeScript Strict Mode in a discord.js Bot](https://getrush.ai/run/enable-strict-mode/discord-js/)
- [Enable mypy Strict Mode in an Existing Python Library](https://getrush.ai/run/enable-strict-mode/mypy/)

## More jobs like this: Hand off a long task

- [Turn On oxlint's Type-Aware Rules and Fix What They Flag](https://getrush.ai/run/enable-type-aware-lint-rules/oxlint/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
