Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define a private property when implementing an interface in Typescript?

I'm using TypeScript in my project and I have come across an issue. I'm defining an interface like this:

interface IModuleMenuItem {     name: string; } 

I want to create a class that implements from this interface but I want the name to be a private property like this:

class ModuleMenuItem implements IModuleMenuItem {     private name: string; } 

I'm getting the following error:

Class ModuleMenuItem incorrectly implements interface IModuleMenuItem. Property name is private in type ModuleMenuItem but not in type IModuleMenuItem.

How can I define a property as private or protected when implementing an interface?

like image 807
Golan Kiviti Avatar asked Jun 13 '16 14:06

Golan Kiviti


People also ask

What is a private property in TypeScript?

TypeScript Private Properties Using TypeScript, we can add private functionality into our classes. What are private properties or methods? A private property of method can only be accessed or called from the class instance itself.

How do you define a function in interface TypeScript?

TypeScript Interface can be used to define a function type by ensuring a function signature. We use the optional property using a question mark before the property name colon. This optional property indicates that objects belonging to the Interface may or may not have to define these properties.

How do I create an instance of an interface in TypeScript?

To create an object based on an interface, declare the object's type to be the interface, e.g. const obj1: Employee = {} . The object has to conform to the property names and the type of the values in the interface, otherwise the type checker throws an error.

Can we have an interface with optional and default properties in TypeScript?

If you want to set the properties of an interface to have a default value of undefined , you can simply make the properties optional. Copied!


2 Answers

Interfaces define "public" contracts and as such it doesn't make sense to have protected or private access modifier on interfaces, which are more of a, let's call it, implementation detail. For that reason you can't do what you want with an interface.

If you want to make the property read-only to consumers, but overridable in a subclass then you can do something like this:

interface IModuleMenuItem {      getName(): string; }  class ModuleMenuItem implements IModuleMenuItem {     private name;      public getName() {         return name;         }      protected setName(newName : string) {         name = newName;     } } 

I think in TypeScript 2.0 (not out yet) you will be able to use the readonly access modifier if you were after initialization-time readonly field - https://basarat.gitbooks.io/typescript/content/docs/types/readonly.html

interface IModuleMenuItem {      readonly name : string; }  class ModuleMenuItem implements IModuleMenuItem {     public readonly name : string;      constructor() {         name = "name";     } } 
like image 183
Ivan Zlatev Avatar answered Oct 21 '22 17:10

Ivan Zlatev


I think you may do it like this

interface IModuleMenuItem{     name: string }  class ModuleMenuItem implements IModuleMenuItem {     private _name: string;     constructor() {     _name = "name";     }      get name(){     // your implementation to expose name     }      set name(value){     // your implementation to set name              }  } 
like image 43
Abdelrahman Hossam Avatar answered Oct 21 '22 17:10

Abdelrahman Hossam