Created
September 30, 2019 17:16
-
-
Save JustinMorgan/05394c8c08e0b6c85abdb53773869d64 to your computer and use it in GitHub Desktop.
Typescript parent class/child class interaction and precedence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
abstract class A { | |
p: string = 'A.p'; | |
q: string; | |
r: string; | |
s = 'A.s' | |
c: C; | |
constructor(dto: any) { | |
console.log('A.constructor') | |
this.q = 'A.q' | |
this.r = 'A.r' | |
this.c = dto.c | |
this.setOver(); | |
if (!this.c || !(this.c instanceof C)) { | |
this.c = new C(this.c) | |
} | |
} | |
abstract setOver(): void; | |
} | |
class B extends A { | |
p = 'B.p' | |
q = 'B.q' | |
s = 'B.s' | |
constructor(dto: any) { | |
super(dto); | |
console.log('B.constructor') | |
this.r = 'B.r' | |
this.c = new D(dto.c); | |
} | |
setOver() { | |
console.log('B.setOver') | |
this.c.w = 'B.c.w' | |
} | |
} | |
class C { | |
t = 'C.t' | |
u: string | |
v: any; | |
w: any | |
constructor(dtoC: any) { | |
console.log('C.constructor') | |
this.u = 'C.u' | |
this.v = dtoC.v; | |
this.w = dtoC.w | |
} | |
} | |
class D extends C { | |
constructor(dtoC: any) { | |
super(dtoC); | |
console.log('D.constructor') | |
this.t = 'D.t' | |
this.u = 'D.u' | |
} | |
} | |
var b = new B({c:{v:'b.dto.c.v'}}) | |
console.log(b) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment