Start learning free

LESSON 6 OF 675 · WELCOME TO TYPESCRIPT

Name Errors in TypeScript

Lesson

TL ; DR:

  • A ReferenceError occurs when TypeScript sees a name it does not recognize. Some examples:

  • Misspelling a function name:

    console.lg("Heyooo!"); // TypeError: console.lg is not a function
    

  • Misspelling a variable name:

    let animal = "parrot";
    console.log(anmal); // ReferenceError: anmal is not defined
    

  • The compiler reports these mistakes (Cannot find name 'anmal'), and when the code runs it immediately stops and throws an error instead.




Full lesson:

Humans are prone to making mistakes. Humans are also typically in charge of creating computer programs. To compensate, programming languages attempt to understand and explain mistakes made in their programs.

Here are two common errors that we encounter while writing TypeScript code:


The SyntaxError:

Syntax errors are mistakes in the use of the TypeScript language, and are analogous to spelling or grammar mistakes in a language like English. For example, the sentence "Would you some tea?" does not make sense – it is missing a verb.

SyntaxError means there is something wrong with the way your program is written. Some examples:

1. Misuse of commas when declaring a string:

let greeting = 'Hey there!";

The compiler reports error TS1002: Unterminated string literal., and when the resulting JavaScript runs we get a SyntaxError: Invalid or unexpected token.

2. Leaving out a parenthesis:

console.log"My name is Andy");

The compiler reports error TS1005: ';' expected..


The ReferenceError:

A ReferenceError occurs when TypeScript sees a word it does not recognize. Some examples:

1. Misspelling a class / function name:

consle.log("My name is Andy");

The compiler reports Cannot find name 'consle', and when we run this code we'll get a ReferenceError: consle is not defined.

2. Using a non-existent variable / Misspelling a variable's name:

let animal = "parrot";
console.log(aniimal);

The compiler reports Cannot find name 'aniimal', and when we run this code we'll get a ReferenceError: aniimal is not defined.


Assignment
Follow the Coding Tutorial and let's practice with errors!


Hint
Look at the examples above if you get stuck.


STEP 1 OF 1
    Ctrl + Enter to check