Types have separate declarations of a private property in TS

avatar

Borislav Hadzhiev

Last updated: Mar 24, 2022

banner

Photo from Unsplash

Types have separate declarations of a private property in TS #

To solve the error "Types have separate declarations of a private property", remove the private visibility modifier from members that have already been declared in the parent class and make sure you don't have multiple versions of the same package installed.

types have separate declarations for private property

Here is an example of how the error occurs.

index.ts
class Person { constructor(private name: string, private age: number) {} } // ⛔️ Class 'Developer' incorrectly extends base class 'Person'. // Types have separate declarations of a private property 'name'.ts(2415) class Developer extends Person { constructor(private name: string, private age: number) { super(name, age); } }

The Person class declares the name and age properties using the private visibility modifier.

The issue here is that the Developer class that extends from Person also declares the properties name and age.

To solve the error, remove the private visibility modifier from the properties in the child class.

index.ts
class Person { constructor(private name: string, private age: number) {} } class Developer extends Person { // ✅ removed private modifiers constructor(name: string, age: number) { super(name, age); } }

When class properties have private visibility, they can only be accessed from within the class that declared the properties (Person in the example).

If you need to access the properties from subclasses of Person, declare them using protected visibility.

index.ts
class Person { // 👇️ protected (accessible from subclasses) constructor(protected name: string, protected age: number) {} } class Developer extends Person { constructor(name: string, age: number) { super(name, age); // 👇️ can access protected members console.log(this.name); console.log(this.age); } } const dev = new Developer('James', 30);

Protected members are accessible inside of the class that declared them and its subclasses.

The error "Types have separate declarations of a private property" also occurs if we use different versions of the same package.

If you have multiple versions of the same package, your emitted code might contain the same class multiple times.

To resolve this, you have to update your packages and make sure they don't depend on different versions of the same package.

If the error is not resolved, try to delete your node_modules and package-lock.json files, re-run npm install and restart your IDE.

shell
rm -rf node_modules package-lock.json npm install

Make sure to restart your IDE if the error persists. VSCode glitches often and a reboot solves things sometimes.

Conclusion #

To solve the error "Types have separate declarations of a private property", remove the private visibility modifier from members that have already been declared in the parent class and make sure you don't have multiple versions of the same package installed.

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.