Declaration or statement expected error in TypeScript

avatar
Borislav Hadzhiev

Last updated: Feb 28, 2024
2 min

banner

# Declaration or statement expected error in TypeScript

The "Declaration or statement expected" error occurs when we have a syntax error in our code, e.g. when destructuring, exporting, or when we have a missing or inconsistent bracket.

To solve the error, make sure to correct any syntax errors in your code.

declaration or statement expected

Here are 3 examples of how the error occurs.

index.ts
let one: number; const obj = { one: 1, }; // 1. โ›”๏ธ Parsing error: Declaration or statement expected. { one } = obj; // ๐Ÿ‘ˆ๏ธ this must be wrapped in parentheses const sum = (a: number, b: number) => a + b; // ------------------------------------------- // 2. โ›”๏ธ Error: Parsing error: Declaration or statement expected.eslint export sum // ๐Ÿ‘ˆ๏ธ should be export {sum} // ------------------------------------------- // 3. Make sure you're not using reserved words const case = 'bobbyhadz.com' // ๐Ÿ‘ˆ๏ธ case is reserved word

# Solve the error when destructuring

The first example shows how the error occurs when destructuring.

To be able to destructure and reassign an already-declared variable, wrap the statement in parentheses.

index.ts
let one: number; const obj = { one: 1, }; // โœ… OK ({ one } = obj); // ๐Ÿ‘ˆ๏ธ this must be wrapped in parentheses console.log(one); // ๐Ÿ‘‰๏ธ 1

solve error when destructuring

The code for this article is available on GitHub

I've also written a detailed tutorial on destructuring object parameters in functions.

# Solve the error when exporting

You might also get the error when exporting something that you previously declared.

When you declare something and export it on another line, wrap the export in curly braces.

index.ts
const sum = (a: number, b: number) => a + b; // โœ… OK export { sum };

solve error when exporting

The code for this article is available on GitHub

We are basically exporting an object that contains one or more named exports.

You don't have to use curly braces if you export and declare the variable in a single statement.

index.ts
// โœ… OK export const sum = (a: number, b: number) => a + b;

# Make sure you don't have missing brackets

Another common cause of the error is having a missing bracket somewhere in your code.

Most code editors have extensions that group parentheses and curly braces in different colors to make it more obvious when one is missing. An example is the "Bracket Pair Colorizer" extension in VSCode.

Check your code for a missing curly brace, parenthesis, square bracket, etc.

If you use IntelliJ as your IDE and are getting the "Declaration or statement expected" error, try closing and re-opening the file or try restarting your IDE as it sometimes glitches.

# Don't use variable names that clash with reserved words

Make sure you aren't using any reserved words when declaring variables.

index.ts
const case = 'bobbyhadz.com' // ๐Ÿ‘ˆ๏ธ case is reserved word

dont use variable names that clash with reserved words

The code for this article is available on GitHub

Words like case, class, Error, etc, are reserved, so we aren't allowed to use them as variable names.

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.

Copyright ยฉ 2024 Borislav Hadzhiev