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
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
Problem
src/templates.tscontains ~25 template string generator functions, all of which use(_: any, ...)as their first parameter. Theanydiscards thethiscontext that these functions receive when bound to a Farmhand instance. This is a type safety hole and the cause of ~25anyoccurrences in non-test code.Current Code
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
thiscontext (the Farmhand instance). The(_: any)is a workaround to avoid TypeScript complaining about the implicitthisparameter. However, this throws away type safety entirely.Implementation Plan
Step 1: Define a
TemplateContextinterfaceCreate 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:
If the functions do not currently use
this, the interface can be minimal. The goal is to type it properly so that ifthisis accessed in the future, TypeScript will catch it.Step 2: Replace
(_: any, ...)with typedthisFor each function, replace the first parameter:
Step 3: Run type checker
Acceptance Criteria
src/templates.tshave explicitthis: TemplateContextinstead of(_: any, ...)anyoccurrences remain insrc/templates.tsnpm run check:typespasses with no errorsnpm testpasses