Skip to content

Instantly share code, notes, and snippets.

@emmanueltouzery
Created December 16, 2017 10:39
Show Gist options
  • Save emmanueltouzery/bfd684fea04c942b46f5a538ececf22c to your computer and use it in GitHub Desktop.
Save emmanueltouzery/bfd684fea04c942b46f5a538ececf22c to your computer and use it in GitHub Desktop.
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