Sub topic 3
Using error codes to look things up
This chapter assumes you can run tsc and read the location part of a diagnostic, which the previous chapter covered. Here you will learn what the TS number means and how to use it to find explanations, group errors, and avoid wrong fixes.
What the code identifies
Every diagnostic the compiler produces has a number, written as TS followed by digits. The number identifies a message template, not a specific cause. In the compiler's own message table, the text for TS2345 is Argument of type '{0}' is not assignable to parameter of type '{1}'. The compiler fills {0} and {1} with your types when it prints the error.
Take this program:
function double(n: number): number {
return n * 2;
}
const input: string = "21";
double(input);Running the compiler without colors and without writing output gives:
$ npx tsc --noEmit --pretty false
src/index.ts(6,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.The part you copy into a search is TS2345. The words string and number came from your program. Another project with different types would show the same code with different words.
Two consequences follow:
- Many different mistakes share one code. TS2322 is reported for assigning a wrong value to a variable, returning a wrong value from a function, and many other cases. The code tells you the kind of complaint, and you still have to read the message to find the cause.
- The code stays the same when the wording changes. TypeScript releases sometimes reword a message, but the number normally stays attached to the same kind of problem. This makes the code a better search key than the sentence.
Where the code appears
The code shows up in several tools, written slightly differently.
| Where | Form | Example |
|---|---|---|
| `tsc` output | `error TS` + number | `error TS2345` |
| VS Code hover and Problems panel | `ts(` + number + `)` | `ts(2345)` |
| CI logs | Same as `tsc` | `error TS2741` |
| Compiler API | A numeric `code` field | `2345` |
The forms differ, but the number is the same. If a teammate pastes ts(2741) from their editor, you can search for TS2741 and find the same thing.
Reading the number range
The leading digits give a rough category. This is a loose pattern, not a documented contract, so use it as a hint only.
| Range | Usually means | Examples |
|---|---|---|
| 1xxx | Syntax problems found while parsing | Missing brace or unexpected token |
| 2xxx | Semantic problems found while type checking | TS2322, TS2339, TS2345, TS2741, TS2307 |
| 5xxx | Compiler option and `tsconfig.json` problems | Unknown option or bad option value |
| 7xxx | Errors caused by `noImplicitAny` | TS7006 |
| 18xxx | Newer diagnostics added later | TS18048 |
A 1xxx error means the file could not even be parsed, so type errors in the same file may be missing or misleading. A 5xxx error points at your configuration, not at your code. Knowing this saves time because you look in tsconfig.json instead of the source file.
A lookup workflow
The steps below turn a code into a fix without guessing.
Search with the template, not your names
A search for Argument of type 'UserProfileDto' is not assignable to parameter of type 'SaveOptions' finds almost nothing, because nobody else has those type names. Replace your names with placeholders, or drop them:
- Good:
TS2345 argument of type is not assignable to parameter of type - Good:
TS2741 property is missing in type but required in type - Weak: your full message pasted as-is
Adding the code to the search narrows results to the right family. Adding a few of the template's words keeps the search from drifting to unrelated posts.
Use the compiler's own message table
The TypeScript repository contains a file named diagnosticMessages.json under src/compiler. It lists each message template, its code, and its category. Searching that file for a code shows the exact template, including the {0} placeholders. This helps in two cases: when a message in your terminal is truncated, and when you want to know which other messages are close relatives of the one you got. It is a reference table, not a tutorial, so it will not explain how to fix anything.
Check that the explanation matches your version
Search results include posts written for older compilers. Check your version before trusting an answer:
$ npx tsc --versionIf an answer says the error appears only with a certain option, confirm that option in your tsconfig.json. For example, TS18048 ('first' is possibly 'undefined') only appears when strict null checking is on. If your configuration has it off, an answer about TS18048 does not describe your project.
Using codes to triage a long list
When tsc prints dozens of errors, count them by code before reading any one in detail:
$ npx tsc --noEmit --pretty false | grep -oE 'error TS[0-9]+' | sort | uniq -c | sort -rnThe output is a list of counts, highest first. A typical result might show many TS2307 lines and a few of everything else. Many identical codes often mean one shared cause, such as a missing package or a changed type used in many places. Fixing the cause once removes all of those lines together. Later chapters cover this triage in more detail.
You can also filter for one code while you work on it:
$ npx tsc --noEmit --pretty false | grep TS2741Codes and suppression
Codes appear in tool configuration, so it helps to know what they can and cannot do.
- A comment such as
// @ts-expect-error TS2345does not restrict the suppression to TS2345. The compiler treats text after the directive as a plain comment. The line is suppressed for any error. Write the code in the text as documentation for readers, but do not rely on it for protection. - Some wrappers around the compiler let you ignore codes by number. For example, ts-jest has a
diagnostics.ignoreCodesoption. This hides every occurrence of that code in your whole test suite, which is much broader than fixing one line.
A worked example
Here is a missing property, which produces TS2741:
interface User {
id: number;
name: string;
}
const user: User = { id: 1 };The message is Property 'name' is missing in type '{ id: number; }' but required in type 'User'. The steps are:
- Note the code, TS2741, and the template
Property '{0}' is missing in type '{1}' but required in type '{2}'. - Read the placeholders:
nameis the missing property,{ id: number; }is what you wrote, andUseris what was required. - Choose a fix. You can add
nameto the object, or, if the property truly is optional, change the interface toname?: string.
The code told you what kind of mistake this was. The placeholders told you exactly where to look. Neither required a search, because the template was clear. Search is for the codes whose templates you have not met before.
Common pitfalls
- Treating the code as the diagnosis. The code names a family of problems. Your types and the full message decide the cause.
- Searching with your own identifiers, which finds nothing useful.
- Applying a fix from an answer for an older or newer compiler without checking your version and options.
- Silencing a code project-wide when one line needed attention.
- Reading only the first line of a multi-line message. The code is on the first line, but the specific reason often comes in the indented lines after it. The next chapters cover how to read these.
Exercise
Your build prints 40 errors. Running the counting pipeline shows 36 lines of TS2307 and 4 other lines with different codes. What does this suggest, and what should you check first?
Show answer
Many identical codes usually mean one shared cause. TS2307 is "Cannot find module", so a package is probably not installed, a path or moduleResolution setting is wrong, or a file was renamed. Check the module name in a few of the TS2307 messages and look for what they share, such as the same package name. Fix that once, rerun tsc, and then look at the remaining four errors.
Exercise
A teammate adds // @ts-expect-error TS2345 above a call, hoping it will only hide that one kind of error. A different mistake on the same line, with code TS2554 (wrong number of arguments), is later introduced. Is it reported?
Show answer
No. The text after @ts-expect-error is only a comment, so the directive suppresses any error on the next line, including TS2554. The suppression would only become an error if the line stopped producing any error at all. This is why the comment should explain the reason in words and why a broad suppression should be avoided when the line can be fixed.