Created
May 24, 2020 13:19
-
-
Save ngot/20154f8d1593ba19f7ee93ac038a8c63 to your computer and use it in GitHub Desktop.
A simple polymorphism demo in TypeScript
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
// 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