Property or signature expected error in TypeScript

avatar

Borislav Hadzhiev

Last updated: Mar 17, 2022

banner

Photo from Unsplash

Property or signature expected error in TypeScript #

The "Property or signature expected" error occurs when we have a syntax error when declaring a type or interface. To solve the error, make sure to separate the properties and types by a colon and wrap properties that contain hyphens or spaces in quotes.

Here are 3 examples of how the error occurs.

index.ts
interface Employee { // ⛔️ Error: Property or signature expected.ts(1131) first-name: string; } type Example = { // ⛔️ Error: Property or signature expected.ts(1131) two words: string; } type Person = { // ⛔️ Error: Property or signature expected.ts(1131) country = string; }

The cause of the error in the first example is that the property name in the interface contains a hyphen.

If a property name in an interface, type or object contains a hyphen, we have to wrap it in quotes.

index.ts
interface Employee { 'first-name': string; } const emp: Employee = { 'first-name': 'James', };

Note that we had to wrap the property in quotes both in the interface and when declaring an object of type Employee.

The same is the case with spaces (or most other separators).

If an object, type or interface property contains a space, wrap it in quotes.

index.ts
type Example = { 'two words': string; }; const e: Example = { 'two words': 'hello world', };

Another common cause of the error is mistakenly separating the property and the type by an equal sign or any other symbol that isn't a colon.

index.ts
type Person = { // ⛔️ Error: Property or signature expected.ts(1131) country = string; }

Always make sure to separate the property name and the type by a colon.

index.ts
type Person = { country: string; }; const person: Person = { country: 'Germany', };

Note that you can't provide default values in types and interfaces, because TypeScript only helps us catch bugs during development.

All of the types and interfaces you have defined will be removed when your code is compiled to JavaScript.

Conclusion #

The "Property or signature expected" error occurs when we have a syntax error when declaring a type or interface. To solve the error, make sure to separate the properties and types by a colon and wrap properties that contain hyphens or spaces in quotes.

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.