Identifiers & Keywords in TypeScript

We must give a name to variables, functions, arrays, classes, interfaces, etc so as to identify them. Hence, typescripts call them as an Identifiers. We then use the identifier to refer to the variable, functions, etc elsewhere in the program. There are certain rules & restrictions that need to be followed when we name and identifier. Since the Typescript compiles to javascript. it follows all of the javascript naming rules. Also, the reserved keywords must not be used as an identifier name.

For Example

In the example above, the message is the name given to the variable, which will hold the string “hello world”. Hence the message is Identifier.

Typescript Identifiers

When you name any identifier, you need to follow these rules.

  1. The identifier name must be unique within the scope.
  2. The first letter of an identifier should be a
    1. upper case letter
    2. Lower case letter
    3. underscore
    4. dollar sign
  3. The subsequent letters of an identifier can have
    1. upper case letter
    2. Lower case letter
    3. underscore
    4. dollar sign
    5. numeric digit
  4. We cannot use reserved keywords. The list of keywords is listed below
  5. Identifiers are case-sensitive.
  6. cannot use spaces in a identifier name.

Example of Invalid Identifiers

identifierReason
breakBecause it is a Reserved keyword
emp namespaces are not allowed
emp-name– not allowed
1resultcannot begin with a digit
[email protected]@ not allowed. only underscore
and dollar allowed

Example of Valid Identifiers

empNameemp_name_empName
result1$result

Typescript Keywords

Keywords have a special meaning in the context of a language. The following table lists some keywords in TypeScript.

Reserved words

The following are the reserved words and cannot be used as the identifier names. using them will result in a compile error

compile error when using the reserved keyword
compile error when using the reserved keyword
breakcasecatch
classconstcontinue
debuggerdefaultdelete
doelseenum
exportextendsfalse
finallyforfunction
Ifimportin
istanceOfnewnull
returnsuperswitch
thisthrowtrue
trytypeOfvar
voidwhilewith

Strict Mode Reserved Words

The following code is restricted only in strict mode. else allowed. You can enable the strict mode by inserting the "strict": true in the tsconfig.json file.

Compile error when using the strict mode reserved keywords only if the strict mode is chosen.
Compile error when using the strict mode reserved keywords only if the strict mode is chosen.
asimplementsinterface
letpackageprivate
protectedpublicstatic
yield

Contextual keywords

The following keywords are restricted based on the context otherwise allowed.

For Example, The following is allowed. You can name the variable as the number.

But, you cannot name the interface as the number. because the number itself is a type.

anybooleanconstructor
declaregetmodule
requirenumberset
stringsymboltype
fromof

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top