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

前端 未结 5 1068
陌清茗
陌清茗 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:42

    Use abstract classes instead.

    Composition over inheritance.

    interface AppInterface {
       app: express.Application
       port: string | number
    }
    
    abstract class AbstractApp implements AppInterface {
        app: express.Application
        port: string | number
        constructor(){
            this.app=express()
            this.port=8080
        }
        
        protected defaultMiddlewares(): void {}
    }     
    
    class App extends AbstractApp {
        constructor() {
            super()
        }
    
        protected defaultMiddlewares(): void {
            this.app.use(express.json())
        }
    }
    

提交回复
热议问题