Created
May 24, 2020 13:19
A simple polymorphism demo in TypeScript
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
// 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