Created
December 16, 2017 10:39
-
-
Save emmanueltouzery/bfd684fea04c942b46f5a538ececf22c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
export abstract class A { | |
static make(input: boolean):A { | |
return input ? new B() : new C(); | |
} | |
abstract isB(): this is B; | |
abstract isC(): this is C; | |
abstract get(): number; | |
} | |
export class B extends A { | |
readonly className: "B" // add this line | |
isB(): this is B { | |
return true; | |
} | |
isC(): this is C { | |
return false; | |
} | |
get() { | |
return 5; | |
} | |
} | |
export class C extends A { | |
readonly className: "C" // add this line | |
isB(): this is B { | |
return false; | |
} | |
isC(): this is C { | |
return true; | |
} | |
get() { | |
return 6; | |
} | |
} | |
const x = A.make(true); // new C(); | |
if (x.isB()) { | |
console.log("B!") | |
} else { | |
console.log(x.get()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment