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
break Because it is a Reserved keyword
emp name spaces 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

empName emp_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
break case catch
class const continue
debugger default delete
do else enum
export extends false
finally for function
If import in
istanceOf new null
return super switch
this throw true
try typeOf var
void while with

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.
as implements interface
let package private
protected public static
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.

any boolean constructor
declare get module
require numberset
string symbol type
from of

Leave a Comment

Your email address will not be published.

Scroll to Top