What a name error is
Every variable, function and object in your program has a name, and TypeScript looks each name up when it reads your code. If it meets a name that was never declared, it has nothing to connect it to. That is a name error. The usual causes are small: a typo, the wrong capital letter, or a variable used before anyone created it.
TypeScript has no error called NameError. A name error shows up in two places, depending on when it is caught:
- At compile time, the compiler (
tsc) reports a message such aserror TS2552: Cannot find name 'consle'. Did you mean 'console'? - While the program runs, the JavaScript that
tscproduced throws aReferenceError, for exampleReferenceError: whether is not defined.
Both messages name the word that could not be found. That word is the first thing to look at.
Misspelled variables
Take a variable and a typo in its name:
let weather: string = "sunny";
console.log(whether);
The compiler reports Cannot find name 'whether'. Did you mean 'weather'? The suggestion is worth reading: when a similar name exists, TypeScript points straight at it. Names are case sensitive, so Weather is just as unknown as whether.
Misspelled functions and objects
The same thing happens when the misspelled name is the object that owns a function:
consle.log("Welcome!");
Here consle does not exist, so the compiler says Cannot find name 'consle'. Did you mean 'console'?
Notice what is different when only the method name is wrong, as in console.lg("Hi"). The object console exists, so there is no name error. The compiler reports a different problem: Property 'lg' does not exist on type 'Console'. Misspelling the part after the dot is a property error, and it is a close cousin of a name error. The fix is the same: compare the spelling with the real name.
What happens at run time
TypeScript's compiler reports its errors and, with default settings, still writes the JavaScript file. If you run that file anyway, the program stops at the failing line. Lines before it have already run:
console.log("first");
console.log(whether);
console.log("never printed");
The output of the compiled program is first, followed by ReferenceError: whether is not defined. The third line never runs, because an uncaught error ends the program.
Name errors compared with syntax errors
A syntax error is a different problem: the code is not valid TypeScript at all, like a sentence with a missing word. Both of these are syntax errors:
let greeting = 'Hey there!";
console.log"My name is Andy");
The first has a string opened with a single quote and closed with a double quote. The compiler reports Unterminated string literal. The second has a missing parenthesis and reports ';' expected. With a syntax error, TypeScript cannot even work out the structure of your code. With a name error, the structure is fine, and only one word fails to match anything.
The lesson's exercise
The starter code has two lines with unknown names, and the program has to print Welcome! and then sunny:
consle.log("Welcome!"); // this line produces an error
let weather: string = "sunny";
console.log(whether); // this line produces an error
- Read the first error. It points at line 1 and names
consle. The built-in object isconsole, so fix the spelling there. - Read the second error. It points at line 4 and names
whether. Line 3 declaredweather, so line 4 must use exactly that spelling. - Leave the declaration alone. The variable is correct. The two uses were wrong.
The fixed program looks like this, and it prints both lines:
console.log("Welcome!"); // this line produces an error
let weather: string = "sunny";
console.log(weather); // this line produces an error
The comments are left over from the starter code and no longer describe the lines. They do nothing, so you can delete them.
A checklist when you see "Cannot find name"
- Compare the reported name letter by letter with the declaration. Look for swapped letters, a missing letter and capital letters.
- Check that the variable is declared before the line that uses it, and in a place where that line can see it.
- If the compiler offers "Did you mean", try the suggestion first.
- Fix the first error in the list before the others. One typo can cause several messages.
Key takeaways
- A name error means TypeScript met a name it does not know.
- The compiler reports it as
Cannot find nameat compile time, and the compiled JavaScript throws aReferenceErrorwhen it runs. - Names are case sensitive, so
weatherandWeatherare different names. - A syntax error means the code is not valid TypeScript. A name error means valid code uses an unknown word.
- The error message names the culprit. Read it first.