Skip to content

Instantly share code, notes, and snippets.

@JustinMorgan
Created September 30, 2019 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JustinMorgan/05394c8c08e0b6c85abdb53773869d64 to your computer and use it in GitHub Desktop.
Save JustinMorgan/05394c8c08e0b6c85abdb53773869d64 to your computer and use it in GitHub Desktop.
Typescript parent class/child class interaction and precedence
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