Sub topic 14
Fix the Code, the Type, or the Call Site
Assume you know basic JavaScript and TypeScript, including annotations, interfaces, and union types. This chapter focuses on choosing the right fix after you understand a diagnostic. A compiler error usually points to one of three places:
- The value-producing code is wrong.
- The type declaration is too narrow or inaccurate.
- The call site is using a valid value incorrectly.
The best fix preserves the program’s real behavior and keeps the type system honest.
Start with the boundary that is wrong
Suppose a function requires a number, but input from a form or command line is always a string:
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);The call site is not the real problem. The input has the wrong runtime representation. Convert and validate it before calling the function:
function double(n: number): number {
return n * 2;
}
const input: string = "21";
const parsed = Number(input);
if (Number.isNaN(parsed)) {
throw new Error(`Not a number: ${input}`);
}
console.log(double(parsed));Do not solve this with input as number. A type assertion changes what TypeScript believes, but it does not convert the string at runtime.
Choose among common fixes
| Diagnostic situation | Prefer this fix | Avoid this shortcut |
|---|---|---|
| A value can be absent | Narrow it with a check | `value!` without proof |
| A literal widened too far | Add an annotation or `as const` | Changing the function to accept any string |
| The function contract is wrong | Correct the parameter or return type | Casting every caller |
| One caller has the wrong value | Fix that caller | Weakening the shared contract |
For example, find can return undefined. Narrow the result before using it:
const names: string[] = ["ada", "alan"];
const first = names.find((name) => name.startsWith("a"));
if (first !== undefined) {
console.log(first.length);
} else {
console.log("no match");
}The check is part of the program’s behavior. It handles the case that the type warned you about.
Fix widened literal types
This object is inferred with mode: string, which is broader than the function’s accepted union:
type Config = {
retries: number;
mode: "fast" | "safe";
};
function run(config: Config): void {
console.log(config.retries, config.mode);
}
const config: Config = { retries: 3, mode: "fast" };
run(config);Annotating the variable keeps the intended contract visible. You could also use as const for a fixed value, but that makes every property readonly. Choose it only when that immutability is appropriate.
Review the fix at the right layer
Trace the value from its source to its use:
flowchart LR
A[External input] --> B[Parse and validate]
B --> C[Typed value]
C --> D[Function contract]
D --> E[Call site]caption: Where to repair a type error alt: A flow from external input through parsing and validation to a typed value, function contract, and call site.
Repair the earliest incorrect assumption. If data is invalid, validate it. If the type declaration is inaccurate, change the declaration. If only one call is incorrect, change that call. Widening a type or adding an assertion should be the last reasonable choice.
For intentional negative tests, prefer @ts-expect-error because it fails if the expected error disappears:
function double(n: number): number {
return n * 2;
}
// @ts-expect-error: this test verifies that strings are rejected
double("21");Exercise
A function accepts number, but a value comes from input.value as a string. Should you assert the string as number or parse it?
Show answer
Parse and validate it at the input boundary. An assertion would hide the runtime mismatch.