Identifiers & Keywords in TypeScript

We must give a name to variables, functions, arrays, classes, interfaces, etc so as to identify them. Hence, TypeScript calls them 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 an 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
emp@name@ 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 reserved words and cannot be used as 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

2 thoughts on “Identifiers & Keywords in TypeScript”

  1. One of the best typescript tutorials… no tutorial explains basic concepts in very detailed fashion like this website… its literally enjoyable to read all of this content

Leave a Comment

Your email address will not be published. Required fields are marked *

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

Scroll to Top