Skip to content

Replace (_: any) with explicit this typing in src/templates.ts #709

Description

@jeremyckahn

Problem

src/templates.ts contains ~25 template string generator functions, all of which use (_: any, ...) as their first parameter. The any discards the this context that these functions receive when bound to a Farmhand instance. This is a type safety hole and the cause of ~25 any occurrences in non-test code.

Current Code

export const CROWS_DESTROYED = (_: any, numCropsDestroyed: number): string =>
  `Oh no! Crows destroyed ${numCropsDestroyed} crop${numCropsDestroyed > 1 ? "s" : ""}!`

export const COW_PEN_PURCHASED = (_: any, cows: number): string =>
  `Purchased a cow pen with capacity for ${cows} cows! ...`

export const MILKS_PRODUCED = (
  _: any,
  milksProduced: Record<string, number>
): string => { ... }

Every function in the file follows this pattern - the first parameter is (_: any, ...) and is never used.

Context

These template functions are called with a bound this context (the Farmhand instance). The (_: any) is a workaround to avoid TypeScript complaining about the implicit this parameter. However, this throws away type safety entirely.

Implementation Plan

Step 1: Define a TemplateContext interface

Create an interface that captures the fields these template functions actually need from the Farmhand instance. Based on the current usages in the file, the context likely includes:

interface TemplateContext {
  cowInventory: farmhand.cow[]
  items: Record<string, farmhand.item>
  // Add any other fields the template functions access via `this`
  // (currently none are used, but the `this` context should still be typed)
}

If the functions do not currently use this, the interface can be minimal. The goal is to type it properly so that if this is accessed in the future, TypeScript will catch it.

Step 2: Replace (_: any, ...) with typed this

For each function, replace the first parameter:

export const CROWS_DESTROYED = (
  this: TemplateContext,
  numCropsDestroyed: number
): string =>
  `Oh no! Crows destroyed ${numCropsDestroyed} crop${numCropsDestroyed > 1 ? "s" : ""}!`

Step 3: Run type checker

npm run check:types

Acceptance Criteria

  • All ~25 template functions in src/templates.ts have explicit this: TemplateContext instead of (_: any, ...)
  • Zero any occurrences remain in src/templates.ts
  • npm run check:types passes with no errors
  • npm test passes

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions