Skip to content

Instantly share code, notes, and snippets.

@ngot
Created May 24, 2020 13:19
Show Gist options
  • Save ngot/20154f8d1593ba19f7ee93ac038a8c63 to your computer and use it in GitHub Desktop.
Save ngot/20154f8d1593ba19f7ee93ac038a8c63 to your computer and use it in GitHub Desktop.
A simple polymorphism demo in TypeScript
// Package A
interface JContext {
j: string;
}
function fake1 (ctx: JContext) {
// type 2
console.log(ctx);
}
// Package B
interface TContext{
t: string;
}
function fake2 (ctx: TContext) {
// type 3
console.log(ctx);
}
class ContextImpl implements JContext, TContext {
j: string;
t: string;
constructor(j: string, t: string){
this.j = j;
this.t = t;
}
}
// type 1
const a1 = new ContextImpl("a", "b");
fake1(a1);
fake2(a1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment