Skip to content

Instantly share code, notes, and snippets.

@ngot
Created May 24, 2020 13:19

Revisions

  1. ngot created this gist May 24, 2020.
    34 changes: 34 additions & 0 deletions polymorphism.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    // 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);