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

前端 未结 5 1076
陌清茗
陌清茗 2021-02-03 17:05

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;
}
         


        
5条回答
  •  南笙
    南笙 (楼主)
    2021-02-03 17:26

    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         
        }
     }
    

提交回复
热议问题