In real code

ts-capture watches your code run under its tests and writes the types it saw back into the source. These are annotations it wrote into real code: the ones you would have written yourself, and the ones a reviewer should change. What it left alone, and why, is on How it decides.

Nothing here was typed by hand. Each example is cut from the diff of an actual run; the highlighted parts are what apply added.

Parameters and a return type, from the calls the tests made

Good mini-app src/pricing.ts:10

Before
};

export function applyCoupon(amount, coupon) {
  if (!coupon) {
    return amount;
After ts-capture apply
};

export function applyCoupon(amount: number, coupon: { kind: string; value: number } | undefined): number {
  if (!coupon) {
    return amount;

Every test passed a number and got a number back. Some passed a coupon and some passed none, so the parameter is the coupon's shape or undefined.

The rule
Return type · Written →

A callback, typed from what it returned

Good mini-app src/orders.ts:10

Before
  nextId = 1;

  constructor(gateway, clock) {
    this.gateway = gateway;
    this.clock = clock;
After ts-capture apply
  nextId = 1;

  constructor(gateway, clock: () => Date) {
    this.gateway = gateway;
    this.clock = clock;

The tests hand the service a function that returns a Date. ts-capture saw it called and saw what came back. gateway stays as it was: what was observed of it carried nothing worth writing.

The rule
Callback · Written →

An element of a collection TypeScript knows nothing about

Good mini-app src/cart.ts:6

Before
}

export function addLine(cart, product, quantity) {
  const existing = cart.lines.find((line) => line.sku === product.sku);
  if (existing) {
    existing.quantity += quantity;
After ts-capture apply
}

export function addLine(cart, product: { name: string; price: number; sku: string; tags: string[]; weight: number }, quantity: number) {
  const existing: { quantity: number; sku: string; unitPrice: number } | undefined = cart.lines.find((line: { quantity: number; sku: string; unitPrice: number }) => line.sku === product.sku);
  if (existing) {
    existing.quantity += quantity;

cart is still any, so TypeScript has no type for line. The observed shape is all there is, and it is written on the callback and on the result.

The rule
Parameter · Written →

Keys that came from the test data

Needs review mini-app src/inventory.ts:1

Before
export function reserve(stock, sku, quantity) {
  const available = stock[sku] ?? 0;
  if (available < quantity) {
After ts-capture apply
export function reserve(stock, sku: string, quantity: number): { "sku-chair"?: number; "sku-lamp": number } {
  const available = stock[sku] ?? 0;
  if (available < quantity) {

The return type lists the two SKUs the tests used. The function accepts any SKU: it indexes by a variable. Nothing flags this, because the type describes the test run exactly.

A reviewer would write Record<string, number>

A lookup that can miss, typed as if it always hits

Needs review mini-app src/orders.ts:36

Before
  }

  find(id) {
    return this.orders.get(id);
  }
After ts-capture apply
  }

  find(id: string): { customer: { email: string; name: string; phone: string }; id: string; lines: { quantity: number; sku: string; unitPrice: number }[]; placedAt: Date; status: string; total: number; transactionId: string } {
    return this.orders.get(id);
  }

Map.get returns undefined for an unknown id. Every test asked for an order that existed, so undefined was never observed.

A reviewer would write … | undefined

A type that already has a name

Good nestjs/nest packages/microservices/server/server-kafka.ts:132

Before

  public async start(callback: () => void): Promise<void> {
    const consumerOptions = {
      ...(this.options.consumer || {}),
      groupId: this.groupId,
After ts-capture apply

  public async start(callback: () => void): Promise<void> {
    const consumerOptions: ConsumerConfig = {
      ...(this.options.consumer || {}),
      groupId: this.groupId,

The observed object matches a type that is in scope, so the annotation is that name and not the shape spelled out.

The rule
Parameter · Written →

A union of what actually flowed through

Good nestjs/nest packages/core/middleware/routes-mapper.ts:35

Before
      return this.getRouteInfoFromPath(controllerOrRoute);
    }
    const routePathOrPaths = this.getRoutePath(controllerOrRoute);
    if (this.isRouteInfo(routePathOrPaths, controllerOrRoute)) {
      return this.getRouteInfoFromObject(controllerOrRoute);
After ts-capture apply
      return this.getRouteInfoFromPath(controllerOrRoute);
    }
    const routePathOrPaths: string | string[] | undefined = this.getRoutePath(controllerOrRoute);
    if (this.isRouteInfo(routePathOrPaths, controllerOrRoute)) {
      return this.getRouteInfoFromObject(controllerOrRoute);

Across the tests this held a string, an array of strings, and undefined. The annotation is the three together.

Test data in a real project

Needs review nestjs/nest packages/core/services/reflector.service.ts:213

Before
      return metadataCollection as TResult;
    }
    return metadataCollection.reduce((a, b) => {
      if (Array.isArray(a)) {
        return a.concat(b);
After ts-capture apply
      return metadataCollection as TResult;
    }
    return metadataCollection.reduce((a, b: boolean[] | number | string | string[] | { both: string; only2: string }) => {
      if (Array.isArray(a)) {
        return a.concat(b);

The collection holds whatever metadata a caller attached. both and only2 are keys from nest's own tests, now written into the source as if they were the type.

A reviewer would write leave it unannotated, or type the collection where it is built

A callback's return type, by name

Good honojs/hono src/helper/dev/index.ts:28

Before

export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {
  return hono.routes.map(({ path, method, handler }: RouterRoute) => {
    const targetHandler = findTargetHandler(handler)
    return {
After ts-capture apply

export const inspectRoutes = <E extends Env>(hono: Hono<E>): RouteData[] => {
  return hono.routes.map(({ path, method, handler }: RouterRoute): RouteData => {
    const targetHandler = findTargetHandler(handler)
    return {

The parameter was typed already. What the callback returned matched RouteData, a type the file already uses, so that name is written and not the shape.

The wrapper object, not the primitive

Needs review honojs/hono src/jsx/components.ts:204

Before

            const callbacks = htmlArray
              .map((html) => (html as HtmlEscapedString).callbacks || [])
              .flat()
After ts-capture apply

            const callbacks = htmlArray
              .map((html: String) => (html as HtmlEscapedString).callbacks || [])
              .flat()

Hono's escaped strings are String objects, and that is what was observed. The annotation is accurate about the value and is not the type the code means.

A reviewer would write HtmlEscapedString

Where these come from

A type is only as good as what the tests passed in. The examples marked “needs review” are not bugs: they describe the test run exactly, which is why nothing flags them. See what it cannot infer and review and apply safely.