Sub topic 3
Using error codes to look things up
Assume you know basic JavaScript, TypeScript annotations, interfaces, union types, and how to run npm commands. This chapter focuses on using a diagnostic's error code to find the right explanation instead of searching the entire error message blindly.
Why error codes help
A TypeScript diagnostic usually includes:
- A location, such as
src/index.ts(6,8). - An error code, such as
TS2345. - A message describing the failed check.
The code identifies the general rule that failed. The message supplies the specific types, properties, or values involved. Search both together when you need more context.
src/index.ts(6,8): error TS2345: Argument of type 'string' is not assignable to parameter of type 'number'.TS2345 tells you that an argument passed to a function is not assignable to the function parameter's type. The message tells you which argument and parameter caused the problem.
A practical lookup workflow
When the compiler reports an error:
- Read the file, line, and column. Open the source at that location.
- Copy the error code, including the
TSprefix. - Search the TypeScript documentation or a trusted reference for that code.
- Return to the complete message and identify the source type and expected type.
- Check nearby code before changing anything. The error may be a symptom of an earlier mistake.
Common codes form useful groups:
| Code | General meaning | First question to ask |
|---|---|---|
| TS2322 | Assignment type mismatch | What type does the variable require? |
| TS2345 | Function argument mismatch | What parameter type does the function require? |
| TS2339 | Property does not exist | Is the property misspelled or missing from the type? |
| TS2741 | Required property is missing | Which required property was omitted? |
| TS18048 | A value may be `undefined` | Did the code handle the missing case? |
| TS7006 | Parameter has implicit `any` | What type should this parameter have? |
| TS2307 | Module cannot be found | Is the path, package, or declaration available? |
Error codes are more reliable search terms than phrases such as “type error.” However, the exact wording can change between TypeScript versions. The code usually remains the best starting point.
Read the code and message together
Consider this diagnostic:
error TS2339: Property 'email' does not exist on type 'User'.The lookup tells you that TypeScript found a property access that the User type does not declare. Possible causes include a spelling mistake, an incomplete interface, or a value that has a different type than expected. Do not immediately add email to the interface. First verify the data model and the property name.
Some diagnostics contain a message chain. Read the outer claim first, then follow each nested type comparison until you reach the concrete source value. The last comparison often explains the fix.
flowchart TD
A[Compiler diagnostic] --> B[Read location]
B --> C[Identify TS error code]
C --> D[Look up rule]
D --> E[Compare message with source]
E --> F[Fix the underlying cause]
F --> G[Run the compiler again]Look up errors from the command line
Run the compiler without writing JavaScript output when you are investigating:
npx tsc --noEmit --pretty falseThe plain output makes locations and codes easy to search. If a code is unfamiliar, search for the exact code first, then inspect the TypeScript version used by the project. A project may report newer diagnostics than examples written for an older compiler.
Exercise
A call reports TS2345: Argument of type 'string' is not assignable to parameter of type 'number'. What should you inspect first?
Show answer
Inspect the function parameter type and the argument's actual type. Convert or validate the string at the input boundary if the function truly requires a number.