An identifier or keyword cannot immediately follow numeric literal

avatar

Borislav Hadzhiev

Last updated: Mar 8, 2022

banner

Photo from Unsplash

An identifier or keyword cannot immediately follow numeric literal #

The TypeScript error "An identifier or keyword cannot immediately follow numeric literal" occurs when a variable starts with a digit or we try to call a method directly on a digit. To solve the error, rename your variables to not start with digits.

Here are the 2 most common causes of the error.

index.ts
// 👇️ Because we call method directly on digit // ⛔️ Error: An identifier or keyword cannot // immediately follow a numeric literal.ts(1351) const result1 = 7.toFixed(2); const result2 = 7.toString() // 👇️ Because variable (or identifier) starts with digit // ⛔️ Error: An identifier or keyword cannot // immediately follow a numeric literal.ts(1351) const 100hello = 'world'

In the first example, we call a method directly on a numeric literal and the type checker throws an error.

To get around this, we have to wrap the numeric literal in parentheses.

index.ts
const result1 = (7).toFixed(2); console.log(result1); // 👉️ 7.00 const result2 = (7).toString(); console.log(result2); // 👉️ "7"

TypeScript won't allow us to call a method directly on a numeric literal, so we had to wrap it in parentheses to solve the error.

When you think about it, it makes sense because if the number has a decimal, it is kind of confusing which dot is used to separate the integer from the decimal and which is the method invocation.

An alternative solution is to extract the number into a variable.

index.ts
const num = 7; const result1 = num.toFixed(2); console.log(result1); // 👉️ 7.00

You might also get the error if you have declared a variable whose name starts with a digit.

index.ts
// ⛔️ Error: An identifier or keyword cannot // immediately follow a numeric literal.ts(1351) const 100hello = 'world'
In JavaScript (and TypeScript) all variable names must start with a letter, an underscore _ or a dollar sign $. This means you aren't allowed to start a variable name with a digit.

However, you can use digits for the subsequent characters of a variable name.

index.ts
const hello100 = 'world'; console.log(hello100); // 👉️ "world"

This rule also applies to object properties and any other identifiers.

index.ts
const obj = { // ⛔️ Error: An identifier or keyword // cannot immediately follow a numeric literal.ts(1351) 100hello: 'world' }

Make sure to start the names of your identifiers with a letter, an underscore _ or a dollar sign $ to avoid this error.

index.ts
const obj = { hello100: 'world', }; console.log(obj.hello100); // 👉️ 'world'

Conclusion #

The TypeScript error "An identifier or keyword cannot immediately follow numeric literal" occurs when a variable starts with a digit or we try to call a method directly on a digit. To solve the error, rename your variables to not start with digits.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.