# Build a TypeScript robot

Write and test a TypeScript robot in Clunkyard. The SDK includes type
declarations for robot sensors and controls, so your editor can check the
names and values you use.

## Build, try and debug

### TypeScript starter

In the workbench, create a TypeScript robot and edit `bot.ts`. Save, wait for the build and queue [hosted practice](https://clunkyard.com/manual#practice).

See the [local player kit](https://clunkyard.com/sdk/player-kit) for your platform and toolchain setup.

```sh
npx @clunkyard/cli new typescript my-robot
cd my-robot
npx @clunkyard/cli doctor
npx @clunkyard/cli build
npx @clunkyard/cli match
npx @clunkyard/cli replay
```

If the build fails, start with the first error in `bot.ts` in the terminal output or the [hosted build output](https://clunkyard.com/manual#builds). If it builds but acts differently than expected, open the [turn trace](https://clunkyard.com/manual#history) and compare what the robot sensed, sent and had accepted.
To record a decision in the replay, use the binding’s [debug log](https://clunkyard.com/sdk/javascript#debug).

## Use the typed SDK

Import from `clunkyard` in `bot.ts`. The starter installs the
`@clunkyard/sdk` package under that name so the same import works locally and
in the workbench.

```typescript
import * as cy from 'clunkyard';

class Scout extends cy.Robot {
  onTurn() {
    this.scanner.set({ heading: this.me.heading, arc: 40 });
    this.drive.set({ throttle: 0.5, turn: 0.33 });
  }
}

cy.run(new Scout());
```

For a first change, lower the throttle and replay the match to see how that
changes the search pattern. To add tracking, read `this.sensors.nearest`.
Check for `null` before using the contact's bearing.

The [JavaScript and TypeScript API reference](https://clunkyard.com/sdk/javascript#reference)
lists the types of results and control arguments. If your editor cannot find
`clunkyard`, run the starter build from the project directory to install its
dependencies.
